Created
April 20, 2016 04:58
-
-
Save pastcompute/6deeb54c33de654585149b59dc3e5a4a to your computer and use it in GitHub Desktop.
Another GDrive mail merge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myFunction() { | |
var startRow = 2; // First row of data to process | |
var numRows = 16; // Number of rows to process | |
//startRow = 13; | |
//numRows = 1; | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
// Columns expected: Name, Extended Price, Email1, Email2, Purchase Order | |
var files = DriveApp.getFilesByName("EXAMPLE TAX INVOICE.docx"); | |
var template_id = ""; | |
var template_file = null; | |
while (files.hasNext()) { | |
// should only be one! | |
var file = files.next(); | |
template_id = file.getId(); | |
template_file = file; | |
break; | |
} | |
var fileParents = file.getParents(); | |
var fileFolder = null; | |
while ( fileParents.hasNext() ) { | |
fileFolder = fileParents.next(); | |
break; | |
} | |
Logger.log("Sheet=" + sheet.getName() + " template_id=" + template_id + " folder=" + fileFolder.getName()); | |
var dataRange = sheet.getRange(startRow, 1, numRows, 7) // r, c, NR, NC | |
var data = dataRange.getValues(); | |
if (true) { | |
for (i in data) { | |
var row = data[i]; | |
var personName = row[0]; | |
var priceInc = row[1]; | |
var gst = Math.round((priceInc / 11) * 100) / 100; | |
var priceEx = Math.round( (priceInc - gst) * 100) / 100; | |
var email1 = row[2]; | |
var email2 = row[3]; | |
var purchaseOrder = row[4]; | |
var attention = row[5]; | |
var company = row[6]; | |
if (attention === undefined) attention = personName; | |
if (attention === "") attention = personName; | |
if (company === undefined) company = ""; | |
if (purchaseOrder === undefined) purchaseOrder = ""; | |
if (!email1) { email1 = ""; } | |
if (!email2) { email2 = ""; } | |
if (email1 == email2) { email2 = ""; } | |
Logger.log("name=" + personName + ", inc=" + priceInc + ", ex=" + priceEx + ", gst=" + gst + | |
", email1=" + email1 + ", email2=" + email2 + ", po=" + purchaseOrder + ", attention=" + attention + ", co=" + company); | |
var invoiceFilename = "Invoice__" + personName.replace(" ", "_").replace(",",""); | |
var invoice_file = template_file.makeCopy(invoiceFilename); | |
var invoice_id = invoice_file.getId(); | |
var docu = DocumentApp.openById(invoice_id); | |
var body = docu.getBody(); | |
body.replaceText("{{company}}", company); | |
body.replaceText("{{attendee_name}}", personName); | |
body.replaceText("{{attention}}", attention); | |
body.replaceText("{{amount_ex}}", priceEx); | |
body.replaceText("{{amount_inc}}", priceInc); | |
body.replaceText("{{amount_gst}}", gst); | |
body.replaceText("{{purchase_order}}", purchaseOrder); | |
docu.saveAndClose(); | |
var docu = DocumentApp.openById(invoice_id); | |
docblob = docu.getAs('application/pdf'); | |
docblob.setName(docu.getName() + ".pdf"); | |
fileFolder.createFile(docblob); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment