Created
May 9, 2019 12:44
-
-
Save nathanfletcher/db4d1f9ac30212f2a7002741940aebdc to your computer and use it in GitHub Desktop.
This App scripts code take a list of details in a google sheet and replaces the details in a Google doc.
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 onOpen() { | |
var menuEntries = [ {name: "Create Autofilled Template", functionName: "AutofillDocFromTemplate"}]; | |
var ss = SpreadsheetApp.getActiveSpreadsheet();; | |
ss.addMenu("My Menu", menuEntries); | |
} | |
function AutofillDocFromTemplate(){ | |
var templateid = "GOOGLE-DOCS-TEMPLATE-ID-GOES-HERE"; // this is the template file id. You can find this in the URL of the google document template. For example, if your URL Looks like this: https://docs.google.com/document/d/1SDTSW2JCItWMGkA8cDZGwZdAQa13sSpiYhiH-Kla6VA/edit, THEN the ID would be 1SDTSW2JCItWMKkA8cDZGwZdAQa13sSpiYhiH-Kla6VA | |
var FOLDER_NAME = "GOOGLE-DRIVE-FOLDER-NAME"; // Enter the name of the folder where you want to save your new documents. for example, this could be "Output Folder". | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getActiveSheet(); | |
var data = sheet.getRange(2, 1, sheet.getLastRow()-1, sheet.getLastColumn()).getValues(); | |
var usernamefordoctitle = "Oceanspring" //sheet.getRange(2, 1, 1, 1).getValues(); | |
var newDoc = DocumentApp.create("Template for " +usernamefordoctitle); | |
var file = DriveApp.getFileById(newDoc.getId()); | |
var folder = DriveApp.getFolderById("FOLDER-KEY-GOES-HERE"); // INSERT the destination folder ID. Once again, this can be found in the URL once you have the folder openned in your browser | |
folder.addFile(file) | |
for (var i in data){ | |
var row = data[i]; | |
Logger.log(row) | |
var docid = DriveApp.getFileById(templateid).makeCopy().getId(); | |
var doc = DocumentApp.openById(docid); | |
var body = doc.getActiveSection(); | |
if(row[0].length<=0 || row[2].length>10 || row[3].length<11){} | |
else{ | |
body.replaceText("%NAME%", row[0]); | |
body.replaceText("%TELEPHONE%", row[2]); | |
body.replaceText("%LOYALTYID%", row[3]); // To add more auto gen fields, add them below along with the column number | |
//body.replaceText("%ENDTEMPLATE%", "\n") | |
body.appendPageBreak(); //Add Page break | |
appendToDoc(doc, newDoc) | |
//appendElementToDoc(doc, newDoc) | |
} | |
} | |
doc.saveAndClose() | |
newDoc.saveAndClose() | |
var message = "Attached is an auto generated template for loyalty customers!"; // Customize message | |
var emailTo = "[email protected]" // replace with your email | |
var subject = "Loyalty Customers auto generated template"; // customize subject | |
var pdf = DriveApp.getFileById(newDoc.getId()).getAs('application/pdf').getBytes(); | |
var attach = {fileName:'Autogenerated template.pdf',content:pdf, mimeType:'application/pdf'}; // customize file name: "Autogenerated template" | |
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]}); | |
//DriveApp.getFileById(docid).setTrashed(true); | |
ss.toast("Template has been complied and emailed to you as PDF. Please check your mail"); | |
} | |
function appendToDoc(src, dst) { | |
var srcBody = src.getActiveSection().getParagraphs(); | |
var dstBody = dst.getActiveSection(); | |
var srcBodyLength = srcBody.length | |
for(i=0; i<srcBody.length; i++){ | |
if (i==srcBodyLength-1){ | |
dstBody.appendPageBreak() | |
} | |
else{ | |
var p = srcBody[i].removeFromParent() | |
dstBody.appendParagraph(p) | |
} | |
} | |
Logger.log("appended oo") | |
} | |
function appendElementToDoc(srcDoc, destDoc) { | |
var docChildren = srcDoc.getNumChildren(); | |
for(i=0; i<srcDoc.getNumChildren()-1; i++){ | |
var child = srcDoc.getChild(i).removeFromParent() | |
var type = child.getType(); | |
Logger.log("Element "+i+" type is "+type); | |
if (type == "PARAGRAPH") { | |
destDoc.getActiveSection().appendParagraph(child); | |
} | |
else{ | |
destDoc.appendPageBreak() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment