I am trying to send Google sheet data to a single slide, then convert it into pdf and send it as an attachment via email, all this using Google Apps Script. But the issue is when I attach this slide as a pdf, it does not get an updated slide, which means it gets a template rather than a slide with the updated data from Google sheet, here is the code snippet:
function createCertificate(e) {
var sheet = e.source.getActiveSheet();
var range = e.range;
if (sheet.getName() === 'SheetName' && range.getColumn() == 9 && e.value == "TRUE") { // Column I (9) is the checkbox column
var row = range.getRow();
var data = sheet.getRange(row, 1, 1, 8).getDisplayValues()[0];
var destinationFolderId = "###";
var sourceSlideFile = DriveApp.getFileById("###");
var copiedSlideFile = sourceSlideFile.makeCopy(data[1]+" pdf", DriveApp.getFolderById(destinationFolderId));
var newSlide = SlidesApp.openById(copiedSlideFile.getId()).getSlides()[0];
newSlide.replaceAllText('{{Full Name}}', data[1]);
newSlide.replaceAllText('{{Name of Program}}', data[2]);
newSlide.replaceAllText('{{Days}}', data[7]);
newSlide.replaceAllText('{{Date of Completion}}', data[6]);
newSlide.replaceAllText('{{Date}}', data[0]);
SlidesApp.openById(copiedSlideFile.getId());
var pdf = copiedSlideFile.getBlob().getAs("application/pdf");;
GmailApp.sendEmail('mailAddress', 'Requested pdf', 'Hi there, \n Please find the pdf in the attachment', {attachments:[pdf]});
sheet.getRange(range.getRow(),range.getColumn()+1).setValue("Sent");
}
}
I apologize if the code is not optimized, Any guidance to resolve this issue and optimized version would be much appreciated.
I thought that in your situation, saveAndClose()
is required to be used. But, in the case of your showing script, when the modified script is proposed, it might be useful. So, I show you the modified script using saveAndClose()
as follows.
function createCertificate(e) {
var sheet = e.source.getActiveSheet();
var range = e.range;
if (sheet.getName() === 'SheetName' && range.getColumn() == 9 && e.value == "TRUE") { // Column I (9) is the checkbox column
var row = range.getRow();
var data = sheet.getRange(row, 1, 1, 8).getDisplayValues()[0];
var destinationFolderId = "###";
var sourceSlideFile = DriveApp.getFileById("###");
var copiedSlideFile = sourceSlideFile.makeCopy(data[1] + " pdf", DriveApp.getFolderById(destinationFolderId));
// --- I modified the below script.
var slide = SlidesApp.openById(copiedSlideFile.getId());
var newSlide = slide.getSlides()[0];
newSlide.replaceAllText('{{Full Name}}', data[1]);
newSlide.replaceAllText('{{Name of Program}}', data[2]);
newSlide.replaceAllText('{{Days}}', data[7]);
newSlide.replaceAllText('{{Date of Completion}}', data[6]);
newSlide.replaceAllText('{{Date}}', data[0]);
slide.saveAndClose();
var pdf = copiedSlideFile.getBlob();
// ---
GmailApp.sendEmail('mailAddress', 'Requested pdf', 'Hi there, \n Please find the pdf in the attachment', { attachments: [pdf] });
sheet.getRange(range.getRow(), range.getColumn() + 1).setValue("Sent");
}
}
application/pdf
.