I have written some code to automatically add in a user's name to a template Google Slides file. This template is essentially a certificate, and I'm trying to automate the generation of said certificate for each user.
Unfortunately, when I try to convert the Slides file to a PDF, it converts the slide without the user's name - e.g. basically just the template file is exported. It's definitely pulling in the correct file id, but still not working. The only problem I can think of is that the presentation hasn't saved the changes by the time the code gets the blob data from DriveApp
.
function buildCertificate() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'),
rows = sheet.getDataRange().getValues(),
template = DriveApp.getFileById('some file id'),
destination = DriveApp.getFolderById('some folder id'),
i = 1;
while (i < rows.length) {
if (rows[i][3] != 'Y') {
var file = template.makeCopy(rows[i][0] + ' ' + rows[i][1], destination),
id = file.getId(),
slide = SlidesApp.openById(id).getSlides()[0],
shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 60, 207, 600, 110),
textRange = shape.getText(),
textStyle = textRange.getTextStyle();
textRange.setText([rows[i][0] + ' ' + rows[i][1]]);
textStyle.setFontSize(40);
textStyle.setForegroundColor('#F9B300');
var paragraphStyle = textRange.getParagraphs()[0].getRange().getParagraphStyle();
paragraphStyle.setParagraphAlignment(SlidesApp.ParagraphAlignment.CENTER);
var theBlob = DriveApp.getFileById(id).getBlob().getAs('application/pdf').setName('test'),
newFile = DriveApp.getFolderById("some folder id").createFile(theBlob);
}
sheet.getRange(i + 1, 4).setValue('Y');
i++;
}
}