我的 Google 表格上有一个脚本,可以将转换为 PDF 的表格发送到邮件列表。 这个脚本是在许多不同的电子表格上设置的,但我发现其中一些在最后几天开始失败。
错误内容为:
异常:https://docs.google.com的请求失败返回代码 500。截断的服务器响应:
OAuthToken 似乎不适用于这些电子表格,但它适用于其他电子表格。
这是代码:
function sendSheetToPdf(){ // this is the function to call
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = SpreadsheetApp.getActive().getSheetByName('Acknowledged Orders');
// if you change the number, change it also in the parameters below
var ssC = SpreadsheetApp.getActive().getSheetByName('Acknowledged Orders')
var toEmail = ""
var shName = sh.getName()
if (sh.getRange("A11").getValue()!="Rows") {
sendSpreadsheetToPdf(sh, shName, toEmail,"Daily Acknowledged Orders", "Daily Acknowledged Orders");
}
}
function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = spreadsheet.getId()
var sheetId = sheetNumber ? '0' : null;
var url_base = spreadsheet.getUrl().replace(/edit$/,'');
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId))
// following parameters are optional...
+ '&size=A3' // paper size
+ '&portrait=true' // orientation, false for landscape
+ '&fitw=true' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false&pagenumbers=true' //hide optional headers and footers
+ '&gridlines=false' // hide gridlines
+ '&fzr=false'; // do not repeat row headers (frozen rows) on each page
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
}
}
var response = UrlFetchApp.fetch(url_base + url_ext, options);
var blob = response.getBlob().setName(pdfName + '.pdf');
if (email) {
var mailOptions = {
attachments:blob, htmlBody:htmlbody
}
var ccEmail = ""
MailApp.sendEmail(
email+","+ccEmail,
subject+" (" + pdfName +")",
"html content only",
mailOptions);
}
}
收到的回复:
<!DOCTYPE html><html lang="en"><head><meta name="description" content="Web word processing, presentations and spreadsheets"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0"><link rel="shortcut icon" href="//ssl.gstatic.com/docs/common/drive_favicon1.ico"><title>Error</title><meta name="referrer" content="origin"><link href="//fonts.googleapis.com/css?family=Product+Sans" rel="stylesheet" type="text/css"><style nonce="VuOBJHMXpK2pKNeVo731zA">/* Copyright 2020 Google Inc. All Rights Reserved. */
.goog-inline-block{position:relative;display:-moz-inline-box;display:inline-block}* html .goog-inline-block{display:inline}*:first-child+html .goog-inline-block{display:inline}#drive-logo{margin:18px 0;position:absolute;white-space:nowrap}.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/1x/googlelogo_color_116x41dp.png');background-size:116px 41px;display:inline-block;height:41px;vertical-align:bottom;width:116px}.docs-drivelogo-text{color:#000;display:inline-block;opacity:0.54;text-decoration:none;font-family:'Product Sans',Arial,Helvetica,sans-serif;font-size:32px;text-rendering:optimizeLegibility;position:relative;top:-6px;left:-7px;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}@media (-webkit-min-device-pixel-ratio:1.5),(min-resolution:144dpi){.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/2x/googlelogo_color_116x41dp.png')}}</style><style type="text/css" nonce="VuOBJHMXpK2pKNeVo731zA">body {background-color: #fff; font-family: Arial,sans-serif; font-size: 13px; margin: 0; padding: 0;}a, a:link, a:visited {color: #112ABB;}</style><style type="text/css" nonce="VuOBJHMXpK2pKNeVo731zA">.errorMessage {font-size: 12pt; font-weight: bold; line-height: 150%;}</style></head><body><div id="outerContainer"><div id="innerContainer"><div style="position: absolute; top: -80px;"><div style="margin: 18px 0; position: absolute; white-space: nowrap;"><a href="//support.google.com/docs/"><img height="35px" src="//ssl.gstatic.com/docs/common/product/spreadsheets_lockup1.png" alt="Google logo"/></a></div></div><p style="padding-top: 15px">Google Docs encountered an error. Please try reloading this page, or coming back to it in a few minutes.</p><p>To learn more about the Google Docs editors, please visit our <a href="https://support.google.com/docs/?hl=en&p=error_help" target="_blank">help center</a>.</p><p><br><b>We're sorry for the inconvenience.</b><br><i>- The Google Docs Team</i></p></div></div></body><style nonce="VuOBJHMXpK2pKNeVo731zA">html {height: 100%; overflow: auto;}body {height: 100%; overflow: auto;}#outerContainer {margin: auto; max-width: 750px;}#innerContainer {margin-bottom: 20px; margin-left: 40px; margin-right: 40px; margin-top: 80px; position: relative;}</style></html>
我也遇到了这个错误,这是我的经验:
我尝试将 Google Sheet 单元格转换为 PDF 并将其保存在 Google Drive 中。
function exportRangeAsImage() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
const sheet = spreadsheet.getActiveSheet()
const range = sheet.getRange("S2:AH35");
const url = spreadsheet.getUrl().replace(/edit$/, '')
const exportUrl = url + 'export?format=pdf&gid=' + sheet.getSheetId() +
'&range=' + range.getA1Notation() +
'&size=A4' +
'&portrait=true' +
'&fitw=true' +
'&sheetnames=false&printtitle=false&pagenumbers=false' +
'&gridlines=false&fzr=false';
const options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
}
}
const response = UrlFetchApp.fetch(exportUrl, options)
const blob = response.getBlob().setName("result.pdf")
const file = DriveApp.createFile(blob)
Logger.log('PDF save to: ' + file.getUrl())
}
我发现即使我先打开Google Apps脚本,然后运行脚本让Google Sheets识别范围,我仍然遇到这个错误。
但是,如果我从 Google Sheets 的菜单中选择 Apps Script 并再次运行该函数,就没有问题了。
我认为你需要检查一下网址。这是我的情况