【发布时间】:2018-05-21 05:11:16
【问题描述】:
我真的希望你能帮助我。
此 google 应用程序脚本在将 google 表格转换为 PDF 并通过电子邮件发送时运行良好,同时还将 PDF 的副本保存到特定的 Google Drive 文件夹;但现在它并没有停止工作并给我以下错误。我没有任何改变,上次成功尝试/发送是 2016 年 4 月。过去一年是否有 G-apps 更新?
这是错误:
Request failed for https://docs.google.com/a/filmclosings.com/spreadsheets/d/1Shy55DRkwKdSGayWQYKV6bCPMvm2qfgK3zD_plyi8lE/export?exportFormat=pdf&format=pdf&size=letter&portrait=false&fitw=true&source=labnol&sheetnames=false&printtitle=false&pagenumbers=false&gridlines=false&fzr=false&gid=1082764768 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" ... (use muteHttpExceptions option to examine full response)
这是我的谷歌应用脚本:
/* Send Spreadsheet in an email as PDF, automatically */
function emailSpreadsheetAsPDF() {
// Send the PDF of the spreadsheet to this email address
//var email = "amit@labnol.org";
var originalSpreadsheet = SpreadsheetApp.getActive();
var Output = originalSpreadsheet.getSheetByName("Output-BASIC");
var email = Output.getRange("M2").getValue();
// Subject of email message
// The date time string can be formatted in your timezone using Utilities.formatDate method
//var subject = "PDF Reports - " + (new Date()).toString();
var subject = "Film Closings Finance Plan";
// Get the currently active spreadsheet URL (link)
// Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");
var ss = SpreadsheetApp.openById('1Shy55DRkwKdSGayWQYKV6bCPMvm2qfgK3zD_plyi8lE');
//var ss = SpreadsheetApp.getActiveSpreadsheet();
// Email Body can be HTML too with your logo image - see ctrlq.org/html-mail
//var body = "PDF generated using code at ctrlq.org from sheet " + ss.getName();
var body = "Hello and thank you for your order - attached is the PDF of your Finance Plan from FilmClosings.com";
var url = ss.getUrl();
//var url = "https://docs.google.com/spreadsheets/d/1Shy55DRkwKdSGayWQYKV6bCPMvm2qfgK3zD_plyi8lE/edit";
url = url.replace(/edit$/,'');
/* Specify PDF export parameters
// From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
exportFormat = pdf / csv / xls / xlsx
gridlines = true / false
printtitle = true (1) / false (0)
size = legal / letter/ A4
fzr (repeat frozen rows) = true / false
portrait = true (1) / false (0)
fitw (fit to page width) = true (1) / false (0)
add gid if to export a particular sheet - 0, 1, 2,..
*/
var url_ext = 'export?exportFormat=pdf&format=pdf' // export as pdf
+ '&size=letter' // paper size
+ '&portrait=false' // orientation, false for landscape
+ '&fitw=true&source=labnol' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
+ '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
+ '&fzr=false' // do not repeat row headers (frozen rows) on each page
+ '&gid='; // the sheet's Id
var token = ScriptApp.getOAuthToken();
var sheets = ss.getSheets();
//make an empty array to hold your fetched blobs
var blobs = [];
for (var i=0; i<sheets.length; i++) {
// Convert individual worksheets to PDF
var response = UrlFetchApp.fetch(url + url_ext + sheets[i].getSheetId(), {
headers: {
'Authorization': 'Bearer ' + token
}
});
//convert the response to a blob and store in our array
blobs[i] = response.getBlob().setName(sheets[i].getName() + '.pdf');
}
//create new blob that is a zip file containing our blob array
var zipBlob = Utilities.zip(blobs).setName(ss.getName() + '.zip');
//optional: save the file to the root folder of Google Drive -- Jeff activated this and changed to go a specific Drive folder
DriveApp.getFolderById('0B59I-GV_lvsJaHdOUEZVMktzMHc').createFile(blobs[0]).setName(email+ ' Finplan');
// Define the scope
Logger.log("Storage Space used: " + DriveApp.getStorageUsed());
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {cc:"jeff@filmclosings.com", attachments:blobs[0] });
//GmailApp.sendEmail(email, subject, body, {attachments:[zipBlob]});
}
非常感谢您的时间和洞察力。
最好的,
京东
【问题讨论】:
-
您正在使用 OAuth。这可能是您的
token的问题。我认为发生了某种变化,但我不确定。您是否使用库进行 OAuth 授权? -
我知道的关于 OAuth 的唯一消息是在以下帖子 Link to Apps Script Community 如果问题不是授权问题,则可能与搜索字符串参数有关。您是否尝试过从末尾逐个删除每个搜索字符串参数并尝试每个参数?
-
感谢@SandyGood 的回复。当我今天第一次尝试使用该脚本时,它确实要求我授权电子邮件(就像我第一次使用它时那样)。我只是假设授权已经失效了 11 个月。授权过程中没有错误。只是在运行脚本时。
-
@sandy 当我在编辑器中运行脚本时,这是为错误突出显示的行:var response = UrlFetchApp.fetch(url + url_ext + sheet[i].getSheetId (), {
-
使用
Logger.log(url + url_ext + sheets[i].getSheetId())获取正在使用的URL。运行代码,然后查看日志。从日志打印输出中复制 URL。将 URL 放入浏览器,然后按“Enter”或刷新浏览器选项卡。检查文件是否已下载。
标签: pdf google-apps-script google-sheets