【问题标题】:UrlFetch from Google Sheet exportLink['application/pdf'] not returning PDF data来自 Google Sheet exportLink['application/pdf'] 的 UrlFetch 不返回 PDF 数据
【发布时间】:2015-09-15 23:28:30
【问题描述】:

我创建并定期发送一封电子邮件,作为来自 Google 表格的更新。出于各种客户原因,这是通过 3 种方式完成的,作为指向工作表的链接和作为附件(PDFXLSX)。

这直到最近才起作用。 XSLX 附件仍然有效,但 PDF 不再作为对UrlFetch 的响应发送到file.exportLinks('application/pdf') url。无论请求头是什么,它总是返回为Content-Type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

我在这里遗漏了其他未记录的更改吗?

function exportAsPDF(spreadsheetId) {
  spreadsheetId = spreadsheetId || 'SECRET_ID';

  var file = Drive.Files.get(spreadsheetId),
      url = file.exportLinks['application/pdf'];

  url += '&format=pdf&size=7&fzr=true&portrait=true&fitw=true&gid=0&gridlines=false&printtitle=false&sheetnames=false&pagenum=UNDEFINED&attachment=true'

  var token = ScriptApp.getOAuthToken(),
      response = UrlFetchApp.fetch(url, {
          headers: {
              'Authorization': 'Bearer ' +  token
          }
      });

  var headers = response.getAllHeaders(); // revealing content-type returned isn't pdf
  var pdfBlob = response.getBlob().getAs('application/pdf');      
  var pdfString = pdfBlob.getDataAsString(); // this naturally throws an error

  return response.getBlob(); // this returns to the send mail script
}

【问题讨论】:

  • Drive 是我将 Google Advanced Drive API 映射到的对象

标签: google-apps-script google-sheets


【解决方案1】:

我可以使用来自Convert all sheets to PDF with Google Apps Script 的实用程序获取 PDF。

该工作脚本将电子表格的编辑 URL 修改为导出 URL,如下所示:

https://docs.google.com/spreadsheets/d/<%SS-ID%>/export?exportFormat=pdf...

高级云端硬盘服务提供格式如下的导出 URL:

https://docs.google.com/spreadsheets/export?id=<%SS-ID%>&exportFormat=pdf...

我希望exportLinks 提供的 URL 比工作脚本中的 hack 更可靠。显然不是。

这已被提升为Issue 5114。给它加星标以接收更新。

【讨论】:

  • 这正在工作 - 有趣的是,我曾经在转换为新工作表的工作表停止工作之前使用该 URL:/
猜你喜欢
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
相关资源
最近更新 更多