Hi, I am trying to write a script that will perform a mail merge from google sheets to a pre-built template in google docs.
When I run the script, I get an error or line 45 (see screen shot) and can't figure out why. The error is ReferenceError: template is not defined
Can anyone help me. I am new to scripting....
function generateLeaseOptionLetters() {
// Replace with your Google Sheet and Google Docs template IDs
const spreadsheetId = '1pa3MCju0qvxcFWqE8A4JDId-60XPc6qtpN_FUaK1hjA';
const templateId = '1HyzfDRMpqs3T7b8LmEOlTnVf-NVHhppVjLHr6Ee4fLc';
//var templateId = 'HyzfDRMpqs3T7b8LmEOlTnVf-NVHhppVjLHr6Ee4fLc';
// Replace with the name of the sheet that contains your data
const sheetName = 'CF REI Prospects';
// Replace with the index of the first row of data in your sheet
const startRow = 2;
// Get the Google Sheet and the Google Docs template
const spreadsheet = SpreadsheetApp.openById(spreadsheetId);
const sheet = spreadsheet.getSheetByName(sheetName);
// const template = DriveApp.getFileById(templateId).makeCopy().setName('Lease Option Offer Template');
var templateFile = DriveApp.getFileById('1HyzfDRMpqs3T7b8LmEOlTnVf-NVHhppVjLHr6Ee4fLc');
// Get the data from the sheet
const dataRange = sheet.getDataRange();
const dataValues = dataRange.getValues();
// Get the headers from the sheet
const headers = dataValues[0];
// Loop through each row of data and merge the fields with the template
for (let i = startRow - 1; i < dataValues.length; i++) {
const rowData = dataValues[i];
const firstName = rowData[headers.indexOf('First Name')];
const lastName = rowData[headers.indexOf('Last Name')];
const phone = rowData[headers.indexOf('Phone')];
const email = rowData[headers.indexOf('email')];
const tags = rowData[headers.indexOf('Tags')];
const address = rowData[headers.indexOf('address')];
const city = rowData[headers.indexOf('city')];
const state = rowData[headers.indexOf('state')];
const postalCode = rowData[headers.indexOf('postal code')];
const price = rowData[headers.indexOf('Price')];
const rentZestimate = rowData[headers.indexOf('Rent Zestimate')];
const downPayment = rowData[headers.indexOf('Down Payment')];
const zestimate = rowData[headers.indexOf('Zestimate')];
const notes = rowData[headers.indexOf('Notes')];
// Replace the fields in the template with the data from the sheet
const templateFile = DocumentApp.openById(template.getId());
//var templateFile = DocumentApp.openById(template.getId());
const body = templateFile.getBody();
body.replaceText('{{First Name}}', firstName);
body.replaceText('{{Last Name}}', lastName);
body.replaceText('{{Phone}}', phone);
body.replaceText('{{email}}', email);
body.replaceText('{{Tags}}', tags);
body.replaceText('{{address}}', address);
body.replaceText('{{city}}', city);
body.replaceText('{{state}}', state);
body.replaceText('{{postal code}}', postalCode);
body.replaceText('{{Price}}', price);
body.replaceText('{{Rent Zestimate}}', rentZestimate);
body.replaceText('{{Down Payment}}', downPayment);
body.replaceText('{{Zestimate}}', zestimate);
body.replaceText('{{Notes}}', notes);
// const newDocFile = templateFile.makeCopy();
var newFile = templateFile.makeCopy('NEW_FILE_NAME');
const newDocId = newDocFile.getId();
const newDocUrl = newDocFile.getUrl();
GmailApp.sendEmail('em...@gmail.com', 'Lease Option Offer', '', {attachments:[newDocFile.getAs(MimeType.PDF)]});
}
}