Google脚本将工作表导出为PDF:工作表隐藏/可见时的行为不同

时间:2018-11-06 21:30:59

标签: javascript google-apps-script google-sheets

我有一个Google电子表格,其中包含一个名为“模板”的工作表,并附带了一个Google脚本,该脚本将“模板”工作表转换为具有以下功能的PDF Blob:

function getPdfBlob() {
  var ss = SpreadsheetApp.getActive();
  var ssId = ss.getId();
  var sheet = ss.getSheetByName("template");
  var sheetId = sheet.getSheetId();
  var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ssId);
  var url_ext = "exportFormat=pdf&format=pdf" +  // export as pdf / csv / xls / xlsx
      "&size=letter" +                           // paper size legal / letter / A4
      "&portrait=true" +                         // orientation, false for landscape
      "&fitw=true&source=labnol" +               // fit to page width, false for actual size
      "&sheetnames=false&printtitle=false" +     // hide optional headers and footers
      "&pagenumbers=true&gridlines=false" +      // hide page numbers and gridlines
      "&fzr=false" +                             // repeat row headers (frozen rows) on each page
      "&gid=";                                   // the sheet's Id
  var token = ScriptApp.getOAuthToken();
  var response = UrlFetchApp.fetch(url + url_ext + sheetId, {
    headers: { Authorization: "Bearer " + token }
  });
  return response.getBlob().setName('test.pdf');
}

正常显示工作表时,该功能正常运行。当我隐藏工作表时,我突然收到错误消息:

Request failed for https://docs.google.com/spreadsheets/[...] returned code 500. Truncated server response: <!DOCTYPE html><html lang="en"><head><meta name="description" content="Web word processing, presentations and spreadsheets"><meta name="viewport" c... (use muteHttpExceptions option to examine full response) (line 16, file "Code")

隐藏工作表似乎不会改变其工作表ID。我猜测这与以下事实有关:隐藏时无法通过URL直接导航到特定工作表-Google将我重定向到可见工作表。有解决的想法吗?

0 个答案:

没有答案