无法再为某些用户从 Google Sheets 电子表格生成 PDF

Big*_*keL 3 pdf export google-sheets google-apps-script google-drive-api

近一年来,我们一直在使用以下代码将 Google 表格工作表(保存在 中theSheetName)导出为 .pdf 中指定的 PDF InboundID

上周,一次一个不同的用户无法再生成 PDF。我在包含“var newFile = DriveApp.createFile(blob);”的行中失败 错误是:

“从 text/html 到 application/pdf 的转换失败。”

果然,UrlFetchApp.fetch返回的是 HTML 而不是 PDF。同样,仅适用于某些用户。有没有人对为什么我的用户可能会看到这个有任何想法?

function sendPDFToDrive(theSheetName, InboundID) 
{
  var theSpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
  var theSpreadsheetId = theSpreadSheet.getId();
  var thisSheetId = theSpreadSheet.getSheetByName(theSheetName).getSheetId();  
  var url_base = theSpreadSheet.getUrl().replace(/edit$/,'');
  var theOutFileName = "GASFILE_M_" + (Math.floor(Math.random() * 8997) + 1000) + '.pdf'
  //export as pdf
  var url_ext = 'export?exportFormat=pdf&format=pdf'   
      + (thisSheetId ? ('&gid=' + thisSheetId) : ('&id=' + theSpreadsheetId)) 
      // following parameters are optional...
      + '&size=A4'      // paper size
      + '&scale=2' // 1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page
      + '&portrait=true'    // orientation, false for landscape
      + '&horizontal_alignment=CENTER'  //LEFT/CENTER/RIGHT
      + '&fitw=true'        // fit to width, false for actual size
      + '&sheetnames=false&printtitle=false&pagenumbers=false'  //hide optional headers and footers
      + '&gridlines=true'  // hide gridlines
      + '&printnotes=false' // don't show notes
      + '&fzr=true';       // repeat row headers (frozen rows) on each page
  // Setup options
  var options = 
  {
    headers: 
    {
      'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
    }
  }
  // Build the file
  var response = UrlFetchApp.fetch(url_base + url_ext, options);
  var blob = response.getBlob().setName(theOutFileName);
  var folder = DriveApp.getFolderById("ABC123FooBarID");
  var newFile = DriveApp.createFile(blob); //Create a new file from the blob
  newFile.setName(InboundID + ".pdf"); //Set the file name of the new file
  newFile.makeCopy(folder);
}
Run Code Online (Sandbox Code Playgroud)

小智 14

我遇到了这个确切的问题。经过一些调试后,我发现我的 URL 被错误地创建。

我的代码和你的几乎一样。我发现罪魁祸首是以下几行:

var url_base = theSpreadSheet.getUrl().replace(/edit$/,'');
Run Code Online (Sandbox Code Playgroud)

这实际上并没有像多年来那样清除行尾的“编辑”。我不能说这是为什么,但证据在日志中。所以,我手工制作了网址:

var   url = 'https://docs.google.com/spreadsheets/d/'+SpreadsheetApp.getActiveSpreadsheet().getId()+'/';
Run Code Online (Sandbox Code Playgroud)

这似乎解决了问题。这不是一个完美的面向未来的解决方案,因为如果 Google 更改 URL 的制作方式,这将失败。但它现在有效。

我希望这有帮助。您可以将您的代码创建的 url 发送到日志并检查它们以查看您是否遇到了与我相同的问题。


小智 6

为了扩展 Jesse 接受的答案 - 罪魁祸首肯定在这一行:

var url_base = theSpreadSheet.getUrl().replace(/edit$/,'');
Run Code Online (Sandbox Code Playgroud)

replace(/edit$/,'')调用不再edit像以前那样清除的原因是因为 返回的 URLtheSpreadSheet.getUrl()过去以 结尾edit,但现在返回结尾带有附加参数的 URL - ouid=1111111111111111111111&urlBuilderDomain=yourdomain.com

虽然完全重建 URL 也应该可行,但您还可以通过对正则表达式进行一些小的更改来修补脚本。您可以查找 edit + 任何其他字符,而不是查找edit$(意味着edit作为字符串中的最后一个字符),如下所示:

var url_base = theSpreadSheet.getUrl().replace(/edit.*$/,'');
Run Code Online (Sandbox Code Playgroud)

  • 您的正则表达式不适用于“https://docs.google.com/spreadsheets/d/1Tc2WnuWmQZZYfXeditVUStHyNmUP5K9rkTiJXzTGoA/edit?param1=1¶m2=2” (3认同)