Autofill docs

56 views
Skip to first unread message

Ania Mika

unread,
Oct 26, 2021, 11:00:52 AM10/26/21
to Google Apps Script Community
Hi, 
I've got a problem with this code. I want it to autofill two different templates depending on information in row e.g. 28, if there is "Yes" to use template number 1 if "No" template number two. I'm not sure where put "IF" in this code. 


function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('AutoFill Docs');
menu.addItem('Create New Docs', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('xxxxxxx');
const destinationFolder = DriveApp.getFolderById('fffffffff')
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Form Responses 1')
const rows = sheet.getDataRange().getValues();
rows.forEach(function(row, index){
if (index === 0) return;
if (row[20]) return;
if (row[21]) return;
const copy = googleDocTemplate.makeCopy(`${row[2]}, Agreement` , destinationFolder)
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();
const friendlyDate = new Date(row[5]).toLocaleDateString();
body.replaceText('{{CLIENT NAME}}', row[18]);
body.replaceText('{{DATE}}', friendlyDate);
body.replaceText('{{Client/Company name}}', row[1]);
body.replaceText('{{minimum term of 3 months}}', row[16]);
body.replaceText('{{CLIENT SIGNATORY}}', row[15]);
body.replaceText('{{INSERT FROM SCOPING DOC}}', row[17]);
body.replaceText('{{BUDGET}}', row[19]);
body.replaceText('{{CLIENT / COMPANY NAME & ADDRESS}}', row[9]);
doc.saveAndClose();
const url = doc.getUrl();
sheet.getRange(index + 1, 21).setValue(url)
})
}



Thanks

Clark Lind

unread,
Oct 27, 2021, 8:55:55 PM10/27/21
to Google Apps Script Community
Maybe something like this?
 
function createNewGoogleDocs() {
const googleDocTemplateYes = DriveApp.getFileById('xxxxxxx');
    const googleDocTemplateNo = DriveApp.getFileById('nnnnnnn'); 
const destinationFolder = DriveApp.getFolderById('fffffffff')
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Form Responses 1')
const rows = sheet.getDataRange().getValues();
rows.forEach(function(row, index){
if (index === 0) return;
if (row[20]) return;
if (row[21]) return;
    let copy; 
    if (row[xx] === 'Yes') {
copy = googleDocTemplateYes.makeCopy(`${row[2]}, Agreement` , destinationFolder)
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();
const friendlyDate = new Date(row[5]).toLocaleDateString();
body.replaceText('{{CLIENT NAME}}', row[18]);
body.replaceText('{{DATE}}', friendlyDate);
body.replaceText('{{Client/Company name}}', row[1]);
body.replaceText('{{minimum term of 3 months}}', row[16]);
body.replaceText('{{CLIENT SIGNATORY}}', row[15]);
body.replaceText('{{INSERT FROM SCOPING DOC}}', row[17]);
body.replaceText('{{BUDGET}}', row[19]);
body.replaceText('{{CLIENT / COMPANY NAME & ADDRESS}}', row[9]);
doc.saveAndClose();
const url = doc.getUrl();
sheet.getRange(index + 1, 21).setValue(url)
}) 
    }
     if (row[xx] === 'No') {    //or use "else"
         copy = googleDocTemplateNo.makeCopy(`${row[2]}, Agreement` , destinationFolder)
         ...etc etc    fill out "no" template ...
     }
}

Ania Mika

unread,
Oct 28, 2021, 6:44:55 AM10/28/21
to Google Apps Script Community
Hi, 
Thank you so much! It works perfectly!

Reply all
Reply to author
Forward
0 new messages