已导出 Google 幻灯片演示文稿不包括最近的更改

Exported Google Slides presentation does not include recent changes

我已经编写了一些代码来自动将用户名添加到模板 Google 幻灯片文件中。这个模板本质上是一个证书,我正在尝试为每个用户自动生成所述证书。

不幸的是,当我尝试将幻灯片文件转换为 PDF 时,它转换的幻灯片没有用户名 - 例如基本上只导出模板文件。它肯定会提取正确的文件 ID,但仍然无法正常工作。我能想到的唯一问题是,当代码从 DriveApp 获取 blob 数据时,演示文稿尚未保存更改。

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

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

替换行:

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

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

行后

 paragraphStyle.setParagraphAlignment(SlidesApp.ParagraphAlignment.CENTER);

添加行

 file.saveAndClose();

根据 Apps 脚本 SlidesApp 文档,您可以通过使用 saveAndClose() 关闭它来强制对幻灯片 Presentation 进行任何未决写入。脚本执行结束时会自动关闭打开的演示文稿,但对于像您这样的用例,这种自动刷新发生得太晚,没有用处:

saveAndClose():

saves the current Presentation. Causes pending updates to be flushed and applied.

The saveAndClose() method is automatically invoked at the end of script execution for each open Presentation, even if the script execution terminated with an error.

A closed Presentation cannot be edited. Use one of the open methods on SlidesApp to reopen a given presentation for editing.

您的代码使用的演示文稿绑定在您的变量 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++;
}