首页 > 解决方案 > 将幻灯片 pdf 保存到由谷歌表格单元格定义的文件夹中

问题描述

总体目标:将我的 pdf 保存在我在 google 表格单元格中定义的文件夹中

我在这里找到了这个脚本,在那里我可以将我的谷歌幻灯片组保存到一个很好用的 pdf 中。

function pdf(){
var blob = DriveApp.getFileById("### Slide file ID ###").getBlob();
DriveApp.createFile(blob);
}

我想通过引用电子表格中的单元格来定义目标文件夹 ID。我想这将是我在此处找到的以下代码的变体How to reference a cell as the file or folder id for DriveApp?但我现在真的迷路了。

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const file_id = ss.getRange("Settings!B9").getValue(); // get value in "Settings!B9"
  const folder_id = ss.getRange("Settings!C9").getValue(); // get value in "Settings!C9"
  const googleDocTemplate = DriveApp.getFileById(file_id);
  const destinationFolder = DriveApp.getFolderById(folder_id)
}

我会想象它是这样的,但它仍然将 pdf 保存到我驱动器的根文件夹中

function pdf() {
   const ss = SpreadsheetApp.openById("1j_Ltyx9e8Kb-ywLnJDLl5fzoWpfk3elD9xGvZX665DE");
  const folder_id = ss.getRange("Sheet1!C9").getValue(); // get value in "Sheet1!C9"
  const destinationFolder = DriveApp.getFolderById(folder_id)
  
  var blob = DriveApp.getFileById("1Jac6DZweMjiikCF5qvyZc367PRyn1RoUSs6Osy_F4n0").getBlob();
  DriveApp.createFile(blob);
  
}

标签: google-apps-scriptgoogle-sheetsgoogle-drive-apigoogle-slides

解决方案


您可以直接在文件夹内创建文件:

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const file_id = ss.getRange("Settings!B9").getValue(); // get slides id
  const folder_id = ss.getRange("Settings!C9").getValue(); // get folder id
  const folder = DriveApp.getFolderById(folder_id)
  const blob = DriveApp.getFileById(file_id).getBlob();
  folder.createFile(blob); // create pdf file of the slide
}

确保幻灯片的文件 id 位于 中Settings!B9,文件夹 id 位于Settings!C9.


推荐阅读