I wrote a mail merge script that works great, but execution transcripts revealed that the .makeCopy
task alone consumed 60% of the 6 minute run time. I am trying to re-write the script in a way that enables me to:
- Open a document template
- populate the body of the template with spreadsheet data
- create a new document
- copy the body of the populated template to the new document
- save the new document as a PDF, attach it to an email and send it
- delete the new document (I don't need to retain a copy)
At present, I am receiving a "TypeError" appendtoDoc is not a function, it is undefined." Error in line 72.
//Creates the custom menu in the spreadsheet "Run Script"
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Run Script')
.addItem('Create Certs', 'menuItem1')
.addToUi();
}
//Nest the createDocument function within the menuItem1 function for execution
function menuItem1() {
function createDocFromSheet() {
}
//Defines the start row and calculates the number of rows to be processed
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = Browser.inputBox("Enter Start Row");
var endRow = Browser.inputBox("Enter End Row");
var numRows = (endRow - startRow) + 1;
var dataRange = sheet.getRange(startRow, 1, numRows, 7);
//defines the variables and the email body
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var date = row[0];
var nic = row[1];
var course = row[2];
var lastname = row[3];
var firstname = row[4];
var middle = row[5]
var email = row[6];
var docname = lastname+" "+nic+" PME Cert";
var subjectTxt = "NWC "+ course +" Online PME Course Certificate";
var fullBody = "PME COURSE COMPLETION CERTIFICATE" + "\n\n";
fullBody += "Your " + course + " course completion certificate is attached." + "\n\n";
fullBody += "Regards," + "\n\n";
fullBody += "Professor Steve Pierce" + "\n";
fullBody += "U.S. Naval War College "+ "\n";
fullBody += "Online PME Program Team" + "\n\n";
fullBody += "Learn more about NWC's Online PME Program at the link below:" + "\n";
fullBody += "http://www.usnwc.edu/Academics/College-of-Distance-Education/PME-(1).aspx" + "\n";
// The old makeCopy code
// var docId = DriveApp
// .getFileById("1CjdoldpJmPskkqStpmBk3dRznFyURgY5mMsfVHfIGz4")
// .makeCopy(docname).getId();
// Open the document template
//function createDocFromSheet(){
var templateid = "1CjdoldpJmPskkqStpmBk3dRznFyURgY5mMsfVHfIGz4"
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
//create the new document
var newDoc = DocumentApp.create(lastname+" "+nic+" PME Cert");
var newDocId = newDoc.getId()
var file = DriveApp.getFileById(newDocId)
// fill in the template with data
for (var i in data){
var row = data[i];
// opens the template and populates it with data from the sheet
var docid = DriveApp.getFileById(templateid).getId();
var doc = DocumentApp.openById(docid);
var body = doc.getActiveSection();
body.replaceText('fname', firstname);
body.replaceText('lname', lastname);
body.replaceText('midname', middle);
body.replaceText('course', course);
body.replaceText('date', date);
doc.saveAndClose();
// appends data from the template to the new document
var body = doc.getActiveSection();
var newBody = newDoc.getActiveSection();
appendToDoc(body, newBody);
DocsList.getFileById(docid).setTrashed(true); //deletes the temp file
}
}
function appendToDoc(src, dst) {
for (var i = 0; i < src.getNumChildren(); i++) {
appendElementToDoc (dst, src.getChild(i));
}
}
function appendElementToDoc (doc, object) {
var type = object.getType();
var element = object.copy();
if (type == DocumentApp.ElementType.PARAGRAPH) {
if (element.asParagraph().getNumChildren() != 0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_IMAGE) {
var blob = element.asParagraph().getChild(0).asInlineImage().getBlob();
doc.appendImage(blob);
}
else doc.appendParagraph(element.asParagraph());
}
MailApp.sendEmail(email, subjectTxt, fullBody, {attachments: Newdoc.getAs("application/pdf")});
SpreadsheetApp.flush ();
DriveApp.getFileById(docId).setTrashed(true);
}}}