无法再为某些用户从Google表格电子表格中生成PDF [英] Can no longer produce PDF from Google Sheets spreadsheet for some of the users
问题描述
近一年来,我们一直在使用以下代码将Google表格(保存在theSheetName
中)导出为InboundID
中指定的PDF.
For almost a year we have been using the below code to export a Google Sheets sheet (held in theSheetName
) to a PDF named as specified in InboundID
.
上周,各种用户一次不能再生产PDF.我在包含"var newFile = DriveApp.createFile(blob);"的行中失败.错误是:
This last week, one at a time various users can no longer produce the PDF. I get a failure at the line containing "var newFile = DriveApp.createFile(blob);" with the error being:
从text/html到application/pdf的转换失败."
"Conversion from text/html to application/pdf failed."
当然,UrlFetchApp.fetch
正在返回HTML而不是PDF.同样,仅适用于某些用户.是否有人对我的用户为什么会看到此消息有任何想法?
And sure enough, the UrlFetchApp.fetch
is returning HTML instead of a PDF. Again, only for some users. Does anyone have any thoughts as to why my users might be seeing this?
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);
}
推荐答案
我遇到了这个确切的问题.经过一些调试后,我发现URL的创建不正确.
I was having this exact problem. After some debugging I saw that my URL was being created incorrectly.
我的代码与您的代码几乎相同.我找到罪魁祸首的地方是以下行:
My code was nearly identical to yours. Where I found the culprit was the following line:
var url_base = theSpreadSheet.getUrl().replace(/edit$/,'');
这实际上并没有像多年来那样清除编辑"到行尾.我不能说为什么,但是证明在日志中.因此,我改为手工制作网址:
This was not actually clearing out the 'edit' to the end of the line like it had for years. I cannot say why this is, but the proof was in the logs. So, instead I crafted the url by hand:
var url = 'https://docs.google.com/spreadsheets/d/'+SpreadsheetApp.getActiveSpreadsheet().getId()+'/';
这似乎解决了问题.这不是一个完美的面向未来的解决方案,因为如果Google更改了网址的制作方式,这将失败.但这暂时有效.
This seemed to solve the problem. This is not a perfect futureproof resolution, because if Google changes how the URLs are crafted, this will fail. But it works for now.
我希望这会有所帮助.您可以将代码创建的网址发送到日志中,并检查它们是否与我相同.
I hope this helps. You can send the url your code is creating to logs and check them to see if you have the same issue I did.
这篇关于无法再为某些用户从Google表格电子表格中生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!