Maker and Sender
Maker and Sender
Get the data from each row within the sheet data 4
Add to the UI 10
Add Data to the spreadsheet with column headings to match the fields to be
updated in the template.
const SHEETID =
'1wZLJEsUkuuU0e4ACdvwtwRQTxuYOj9m9Nih8DV65fm4';
function sender() {
const sheet =
SpreadsheetApp.openById(SHEETID).getSheetByName('d
ata');
}
Laurence Svekis https://basescripts.com/
3
Get the data from each row within the sheet
data
The getDataRange() will create a range of rows and columns that have
contents. This will select all the contents for the first 5 rows and up to
column E from our example.
To get the data into a readable array use the getValues() method. Will
return the sheet data into an array with each row as a nested array.
Using the slice() method remove the first row since this contains headings
and not the data we want to populate with.
Loop through each row of data and output it into the logger.
You will need to accept permissions to access files and run the
script.
const sheet =
SpreadsheetApp.openById(SHEETID).getSheetByName('d
ata');
rows.forEach((row,index)=>{
Logger.log(row);
})
Using the DriveApp service, select the template Doc by its id value as a file
object. To place the files into a folder on drive you will need to select the
folder object also using the DriveApp.
On each row of data, create a new document copy from the template, then
select the text in the document using the getBody(). To make a
replacement of the text use the replaceText() method on the body. The first
argument is the value that will be replaced and the second is the
replacement text. To use the second column data from the spreadsheet as
the replacement text use the row data and index value of 1.
rows.forEach((row,index)=>{
body.replaceText('{FIRST}',row[1]);
Logger.log(row);
})
data[0].forEach((heading,i)=>{
body.replaceText(`{${header1}}`,row[i]);
})
doc.setName(row[0]+row[1]);
Close the Doc and save the changes. This will finalize the replace text to
ensure its updated from the replace method.
doc.saveAndClose();
To Create a PDF version of the file you can use the createFile() method to
create a PDF file from the blob object.
f');
The Doc file can be moved into the trash if it's not needed within the folder.
file.setTrashed(true);
To send an email with the file as the attachment you can use the MailApp
Service and sendEmail() method. Add the attachments property and include
the blob as a PDF mime type within the attachments array. Create the
subject and html message body to add into the sendEmail properties.
created';
MailApp.sendEmail({
to:email,
subject:subject,
htmlBody: messageBody,
attachments: [blob.getAs(MimeType.PDF)]
});
This will send the PDF from the updated Doc file to the email address listed
in the 3rd column from the spreadsheet.
To track already sent emails, use the column with the heading sent. We can
add a condition that if the cell for sent in the row has a value then we skip
sending a new email. If it's blank then we can send and create the PDF.
After this is done, update the column value with the current date. Select the
range that should be updated and then set the value of the cell to the
current date with new Date().
tempo.setValue(new Date());
Skip over already sent emails, add a condition to the row to skip the email if
the column 5 cell has a value in the current row.
rows.forEach((row,index)=>{
if(row[4] ==''){
Laurence Svekis https://basescripts.com/
9
Add to the UI
This is only available for bound scripts and will not work on standalone
scripts. Add an item that can run a Google Apps Script function.
function onOpen(){
SpreadsheetApp.getUi()
.createMenu('Email Out')
.addItem('Send Emails','sender')
.addToUi();
function onOpen() {
.createMenu('Email Out')
.addToUi();
function sender() {
const sheet =
SpreadsheetApp.openById(SHEETID).getSheetByName('d
ata');
if (row[4] == '') {
const doc =
DocumentApp.openById(file.getId());
data[0].forEach((heading, i) => {
body.replaceText(`{${header1}}`, row[i]);
})
doc.setName(row[0] + row[1]);
doc.saveAndClose();
const pdf =
folder.createFile(blob).setName(row[0] + row[1] +
'.pdf');
created';
MailApp.sendEmail({
to: email,
htmlBody: messageBody,
attachments: [blob.getAs(MimeType.PDF)]
});
1);
tempo.setValue(new Date());
Logger.log(row);
file.setTrashed(true);
})