RemcoE33

u/icwiener69420 - Mail merge

Jul 13th, 2021 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function myFunction() {
  2.  
  3.   var docTemplateID = "big long hex value here - removed for reddit post";
  4.   var docFinalID = "big long hex value here - removed for reddit post";
  5.   var docTemplate = DocumentApp.openById(docTemplateID);
  6.   var docFinal = DocumentApp.openById(docFinalID);
  7.   var templateParagraphs = docTemplate.getBody().getParagraphs();
  8.  
  9.   var wsID = "big long hex value here - removed for reddit post";
  10.   var ws = SpreadsheetApp.openById(wsID).getSheetByName("waterfalls");
  11.   var data = ws.getRange(2, 1, ws.getLastRow() - 1, 20).getValues();
  12.  
  13.   docFinal.getBody().clear();
  14.  
  15.   var counter = 0
  16.   var steps = 140
  17.  
  18.   do {
  19.     for (i = counter; i < (steps + counter); i++) {
  20.       if (i == data.length) { break; }
  21.       let r = data[i]
  22.       createMailMerge(r[0], r[1], r[2], r[18], r[17], r[5], r[6], r[12], r[11], r[19], templateParagraphs, docFinal);
  23.     }
  24.     docFinal.saveAndClose()
  25.     counter += steps
  26.   } while (counter < data.length)
  27.  
  28. }
  29.  
  30. function createMailMerge(fallsName, fallsRegion, fallsCounty, fallsTown, fallsWaterBody, fallsLat, fallsLong, fallsDrones, fallsBobText, fallsNotes, templateParagraphs, docFinal) {
  31.  
  32.   templateParagraphs.forEach(function (p) {
  33.     docFinal.getBody().appendParagraph(
  34.       p.copy()
  35.         .replaceText("{waterfall_name}", fallsName)
  36.         .replaceText("{region}", fallsRegion)
  37.         .replaceText("{county}", fallsCounty)
  38.         .replaceText("{town}", fallsTown)
  39.         .replaceText("{body_of_water}", fallsWaterBody)
  40.         .replaceText("{lat}", fallsLat)
  41.         .replaceText("{long}", fallsLong)
  42.         .replaceText("{drones}", fallsDrones)
  43.         .replaceText("{bob_fulltext}", fallsBobText)
  44.         .replaceText("{notes}", fallsNotes)
  45.     );
  46.   });
  47.  
  48.   docFinal.save
  49. }
Add Comment
Please, Sign In to add comment