程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了是否可以使用 AppScript 下载 Google 幻灯片中的当前幻灯片?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决是否可以使用 AppScript 下载 Google 幻灯片中的当前幻灯片??

开发过程中遇到是否可以使用 AppScript 下载 Google 幻灯片中的当前幻灯片?的问题如何解决?下面主要结合日常开发的经验,给出你关于是否可以使用 AppScript 下载 Google 幻灯片中的当前幻灯片?的解决方法建议,希望对你解决是否可以使用 AppScript 下载 Google 幻灯片中的当前幻灯片?有所启发或帮助;

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

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

export const getCurrentPage = () => {
  return SlIDesApp.getActivePresentation()
    .getSELEction()
    .getCurrentPage();
};

此函数返回一个 Page Class。

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

解决方法

@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',您可以在浏览器中查看幻灯片 url 中的对象 ID#slide=id.<object id>

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

下载文件:

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

大佬总结

以上是大佬教程为你收集整理的是否可以使用 AppScript 下载 Google 幻灯片中的当前幻灯片?全部内容,希望文章能够帮你解决是否可以使用 AppScript 下载 Google 幻灯片中的当前幻灯片?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。