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)
})
}