使用 Apps 脚本将 Google 电子表格中的每个单独工作表另存为 Google Drive 上自己的 PDF

问题描述 投票:0回答:1

我有一个包含 46 张表格的电子表格。我希望能够隐藏其中 4 个,然后将其余的以 PDF 形式发送到 google 云端硬盘。不需要单击按钮即可发送文件。

我在那里发现了这一点,并将其修改为在导出之前隐藏工作表,但它会下载到本地驱动器并需要输入,而不是自动发送到谷歌驱动器。

function PDF() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var allSheets = ss.getSheets();
  hideSheet("test2");
  allSheets.forEach(function(sheet){
    if(!sheet.isSheetHidden()){
      var sheetId = sheet.getSheetId(); 
      var url = "https://docs.google.com/a/mydomain.org/spreadsheets/d/" + ss.getId() + "/export?exportFormat=pdf&gid=" + sheetId + "&access_token=" + ScriptApp.getOAuthToken();
      var str = '<input type="button" value="Download" onClick="location.href=\'' + url + '\'" >';
      var html = HtmlService.createHtmlOutput(str);
    SpreadsheetApp.getUi().showModalDialog(html, "Click the button to download");
    }
  })
}

function hideSheet(sheetName) {
 SpreadsheetApp.getActive().getSheetByName(sheetName).hideSheet();
}
google-sheets google-apps-script pdf google-drive-api
1个回答
0
投票

要将 PDF 直接发送到 Google 云端硬盘而不需要手动干预,您可以直接将 PDF 保存到 Google 云端硬盘

function PDF() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var allSheets = ss.getSheets();
  hideSheet("test2");
  allSheets.forEach(function(sheet){
    if(!sheet.isSheetHidden()){
      var sheetId = sheet.getSheetId(); 
      var url = "https://docs.google.com/a/mydomain.org/spreadsheets/d/" + ss.getId() + "/export?exportFormat=pdf&gid=" + sheetId + "&access_token=" + ScriptApp.getOAuthToken();
      
      // Save PDF to Google Drive so you don't have to do it manually later
      var folderSave = DriveApp.getRootFolder(); // make this where you want to save it.
      var blob = UrlFetchApp.fetch(url).getBlob().setName(sheet.getName() + ".pdf");
      // this should make the file 
      folderSave.createFile(blob);
    }
  });
}

function hideSheet(sheetName) {
 SpreadsheetApp.getActive().getSheetByName(sheetName).hideSheet();
}
© www.soinside.com 2019 - 2024. All rights reserved.