无论如何可以使用 AppScript 下载 Google 幻灯片中的当前幻灯片?

Is there anyway to download current slide in Google Slides with the AppScript?

我想将在 google 幻灯片中打开的当前页面下载为 pdf。 Google 幻灯片本身允许 UI 将整个演示文稿下载为 PDF,但我有兴趣一张一张地下载演示文稿中的幻灯片。

现在我可以select当前页面,

export const getCurrentPage = () => {
  return SlidesApp.getActivePresentation()
    .getSelection()
    .getCurrentPage();
};

这个函数returns一个Page Class.

问题是我不知道如何将此 'Page Class' 转换为 PDF 并下载。我已经检查了可用的方法,但还没有找到任何有用的方法。有什么建议吗?

@Luke 提到的

将整个幻灯片文件转换为 PDF 并将其保存到您的 Google 驱动器。如果你想在本地计算机上下载特定的幻灯片,你可以参考示例代码。

示例代码:

function onOpen() {
  // get the UI for the Spreadsheet
  const ui = SlidesApp.getUi(); 
  
  // add the menu
  const menu = ui.createMenu("Download")
                 .addItem("Download Current Slide",'downloadModal')
                 .addToUi();
}

function downloadModal() {

  var pdfFile = convertToPdf();
  
  //Get the pdf file's download url
  var html = "<script>window.open('" + pdfFile.getDownloadUrl() + "');google.script.host.close();</script>";
  var userInterface = HtmlService.createHtmlOutput(html)
  .setHeight(10)
  .setWidth(100);
  SlidesApp.getUi().showModalDialog(userInterface, 'Downloading PDF ... ');

  //Delete pdf file in the drive
  pdfFile.setTrashed(true);
}

function convertToPdf() {

  //Get current slide
  var presentation = SlidesApp.getActivePresentation();
  var slide = presentation.getSelection().getCurrentPage().asSlide();
  Logger.log(slide.getObjectId());
  
  //Create temporary slide file
  var tmpPresentation = SlidesApp.create("tmpFile");
  //Delete default initial slide
  tmpPresentation.getSlideById("p").remove();
  //Add current slide to the temporary presentation
  tmpPresentation.insertSlide(0,slide);
  tmpPresentation.saveAndClose();

  //Create a temporary pdf file from the temporary slide file
  var tmpFile = DriveApp.getFileById(tmpPresentation.getId());
  var pdfFile = DriveApp.createFile(tmpFile.getBlob().setName("slide.pdf"))
  
  //delete temporary slide file
  tmpFile.setTrashed(true);

  return pdfFile;
}

它有什么作用?

将当前幻灯片转换为 PDF 文件:

  1. 使用 Page.asSlide() 获取当前幻灯片。
  2. 创建临时幻灯片文件。 Select 初始幻灯片并使用 Slide.remove() 将其删除。

Note:

First slide in the file has an object id = 'q', you can check the object id in the slide url in your browser#slide=id.<object id>

  1. 使用 Presentation.insertSlide(insertionIndex, slide) 将步骤 1 中的当前幻灯片插入我们的临时幻灯片文件。保存并关闭
  2. 从我们的临时幻灯片文件创建一个 pdf 文件。获取 File object of our temporary slide using DriveApp.getFileById(id), get its blob using File.getBlob(). Set the name of the blob to pdf file using Blob.setName(name).Create a pdf file from the blob using DriveApp.createFile(blob)

下载文件:

  1. 创建一个 custom menu 将调用 downloadModal() 以下载本地计算机中的当前幻灯片
  2. 调用convertToPdf()获取pdf文件的File object. Get the download url using File.getDownloadUrl()
  3. 使用 custom dialog to display HTML service 将使用步骤 2 中获得的下载 url 下载文件。
  4. 使用 File.setTrashed(trashed)
  5. 删除 Google 驱动器中的 pdf 文件