0

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++;
  } 
}
4

2 回答 2

3

根据 Apps 脚本SlidesApp文档,您可以Presentation通过使用saveAndClose(). 脚本执行结束时会自动关闭打开的演示文稿,但对于您这样的用例,此自动刷新发生得太晚而无用:

saveAndClose():

保存当前Presentation. 导致刷新和应用挂起的更新。

saveAndClose()方法会在每个 openPresentation的脚本执行结束时自动调用,即使脚本执行因错误而终止。

已关闭的Presentation无法编辑。使用打开方法之一SlidesApp重新打开给定的演示文稿进行编辑。

您的代码使用的演示文稿绑定在您的变量slide中,因此在您的代码之前更改循环结构以关闭DriveApp它将确保Blob数据反映最新的更改。

while (i < rows.length) {
  if (rows[i][3] != 'Y') {
    var file = template.makeCopy(rows[i][0] + ' ' + rows[i][1], destination),
        id = file.getId(),
        presentation = SlidesApp.openById(id),
        slide = presentation.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);

    // Flush the changes we've made to the instantiated template presentation.
    presentation.saveAndClose();

    // Reopen the file with DriveApp -- could perhaps use the existing `file` reference, e.g.:
    // var theBlob = file.getAs(MimeType.PDF).setName('test');
    var theBlob = DriveApp.getFileById(id).getAs(MimeType.PDF).setName('test');
    // Create the PDF in the desired Drive folder.
    DriveApp.getFolderById("some folder id").createFile(theBlob);
  }

  sheet.getRange(i + 1, 4).setValue('Y');
  i++;
}
于 2018-07-11T16:36:28.330 回答
1

正如评论中所说,您在进行更改后不会保存和关闭文件。

换行:

slide = SlidesApp.openById(id).getSlides()[0],

file = SlidesApp.openById(id),
slide = file.getSlides()[0],

线后

 paragraphStyle.setParagraphAlignment(SlidesApp.ParagraphAlignment.CENTER);

添加行

 file.saveAndClose();
于 2018-07-11T16:35:18.510 回答