Skip to content

Instantly share code, notes, and snippets.

@bacalj
Last active December 18, 2020 15:06
Show Gist options
  • Save bacalj/2c879d4671d76c658dce1e803d7414e9 to your computer and use it in GitHub Desktop.
Save bacalj/2c879d4671d76c658dce1e803d7414e9 to your computer and use it in GitHub Desktop.
A basic way to create docs based on google sheet data
/* grab objects for data, template, and target folder */
MY_VALUES = SpreadsheetApp.getActiveSpreadsheet().getRange('replace_this_string_with_range_in_A1_notation').getValues();
MY_TEMPLATE = DriveApp.getFileById('replace_this_string_with_fileid');
MY_FOLDER = DriveApp.getFolderById('replace_this_string_with_folderid');
/* timestamp function */
function niceStamp(){
return Utilities.formatDate(new Date(), "GMT-4", "MM-dd-yy ' at ' HH:mm:ss ");
}
/* render */
function generateKReports(){
/*
build an array of empty-field-free-records
I need to do this because I have inconsistent columns populated accross rows
*/
var my_records = [];
MY_VALUES.forEach((row) => {
var nice_record = row.filter(x => x.length > 1);
my_records.push(nice_record);
});
/* build the docs*/
my_records.forEach(function(my_arr){
/* get field values into semantic vars - as many as needed */
var foo = my_arr[0];
var bar = my_arr[1];
/* prep the doc */
var docTitle = 'Whatever This Is: ' + niceStamp();
var theDoc = MY_TEMPLATE.makeCopy(docTitle, MY_FOLDER);
var theId = theDoc.getId();
var docObj = DocumentApp.openById(theId);
var docBody = docObj.getBody();
var docText = docBody.editAsText();
/* replace placeholders with data */
docText.replaceText('<<foo>>', foo);
docText.replaceText('<<bar>>', bar);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment