【问题标题】:Is there anyway to download current slide in Google Slides with the AppScript?无论如何可以使用 AppScript 在 Google 幻灯片中下载当前幻灯片吗?
【发布时间】:2021-03-19 14:58:33
【问题描述】:

我想将在谷歌幻灯片中打开的当前页面下载为 pdf。 Google Slides 允许在他们自己的 UI 中以 PDF 格式下载整个演示文稿,但我有兴趣一张一张下载演示文稿中的幻灯片。

现在我可以选择当前页面,

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

这个函数返回一个Page Class

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

【问题讨论】:

标签: google-apps-script google-slides-api google-slides


【解决方案1】:

@Luke 提到的Save Google Slide as PDF using Google Apps Script 将整个幻灯片文件转换为 PDF 并将其保存到您的 Google Drive。如果您想在本地电脑上下载特定的幻灯片,您可以参考示例代码。

示例代码:

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. 创建一个临时幻灯片文件。选择初始幻灯片并使用 Slide.remove() 将其删除。

注意:

文件中的第一张幻灯片有一个object id = 'q',您可以在浏览器中查看幻灯片网址中的对象ID#slide=id.&lt;object id&gt;

  1. 使用Presentation.insertSlide(insertionIndex, slide) 将步骤1 中的当前幻灯片插入到我们的临时幻灯片文件中。保存并关闭
  2. 从我们的临时幻灯片文件创建一个 pdf 文件。使用DriveApp.getFileById(id) 获取我们临时幻灯片的File 对象,使用File.getBlob() 获取它的blob。使用 Blob.setName(name) 将 blob 的名称设置为 pdf 文件。使用 DriveApp.createFile(blob) 从 blob 创建一个 pdf 文件

下载文件:

  1. 创建一个custom menu,它将调用downloadModal() 以将当前幻灯片下载到本地计算机中
  2. 调用convertToPdf()获取pdf文件的File对象。使用File.getDownloadUrl()获取下载地址
  3. 使用custom dialog显示HTML service,将使用步骤2中获得的下载url下载文件。
  4. 使用File.setTrashed(trashed) 删除 Google Drive 中的 pdf 文件

【讨论】:

  • 不确定您是否喜欢在本地计算机上下载转换后的 pdf 文件,但如果您只想在 Google 云端硬盘中创建 pdf 文件,请告诉我,以便我更新答案跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 2014-07-14
  • 2019-01-23
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
相关资源
最近更新 更多