Untitled
unknown
plain_text
a year ago
2.9 kB
8
Indexable
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('AutoFill Docs');
menu.addItem('Create New Docs', 'createNewGoogleDocs');
menu.addToUi();
}
function createNewGoogleDocs() {
//This value should be the id of your document template that we created in the last step
const googleDocTemplate = DriveApp.getFileById('1QYr--9yOj5NMJOrrE8uIXvqIGtEZN31GN8VBo3LevAE');
//This value should be the id of the folder where you want your completed documents stored
const destinationFolder = DriveApp.getFolderById('1PKa18VDqERAPggBv8p2Sj4gLwokUFuH_')
//Here we store the sheet as a variable
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('_Results')
//Now we get all of the values as a 2D array
const rows = sheet.getDataRange().getValues();
//Start processing each spreadsheet row
rows.forEach(function(row, index){
//Here we check if this row is the headers, if so we skip it
if (index === 0) return;
//Here we check if a document has already been generated by looking at 'Document Link', if so we skip it
if (row[19]) return;
//Using the row data in a template literal, we make a copy of our template document in our destinationFolder
const copy = googleDocTemplate.makeCopy(`${row[20]}, ${row[1]} MOA` , destinationFolder)
//Once we have the copy, we then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//All of the content lives in the body, so we get that for editing
const body = doc.getBody();
//In these lines, we replace our replacement tokens with values from our spreadsheet row
body.replaceText('{{Court File Number}}', row[0]);
body.replaceText('{{Applicant Name}}', row[1]);
body.replaceText('{{Para 1}}', row[2]);
body.replaceText('{{Para 2}}', row[3]);
body.replaceText('{{Para 8}}', row[4]);
body.replaceText('{{Para 9}}', row[5]);
body.replaceText('{{Para 10}}', row[6]);
body.replaceText('{{Para 11}}', row[7]);
body.replaceText('{{Para 13}}', row[8]);
body.replaceText('{{Para 14}}', row[9]);
body.replaceText('{{Para 15}}', row[10]);
body.replaceText('{{Para 16}}', row[11]);
body.replaceText('{{Para 33}}', row[12]);
body.replaceText('{{Para 34}}', row[13]);
body.replaceText('{{Para 35}}', row[14]);
body.replaceText('{{Para 37}}', row[15]);
body.replaceText('{{Para 43}}', row[16]);
body.replaceText('{{Para 100}}', row[17]);
body.replaceText('{{Style of Cause}}', row[18]);
//We make our changes permanent by saving and closing the document
doc.saveAndClose();
//Store the url of our new document in a variable
const url = doc.getUrl();
//Write that value back to the 'Document Link' column in the spreadsheet.
sheet.getRange(index + 1, 20).setValue(url)
})
}Editor is loading...
Leave a Comment