【发布时间】:2016-02-21 19:49:33
【问题描述】:
我在 google 表格中有一个脚本,它将当前表格作为电子邮件的附件发送。
该脚本对我来说 100% 的时间都有效,但任何其他运行相同内容的人都会出错。寻找想法或解决方案,以便每个人都可以使用脚本,而不仅仅是我。他们都授权脚本运行,但仍然是同样的错误。 (脚本下方列出的错误)
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Send Email')
.addItem('Course Update', 'menuItem1')
.addItem('Training Week Email', 'menuItem2')
.addToUi();
}
function menuItem1() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
COURSEUPDATE();
}
function menuItem2() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
TRAININGWEEK();
};
//This is the second Script to send entire doc to me
function COURSEUPDATE() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Email subject and message body
var url = ss.getUrl();
url = url.replace(/edit$/,'');
var url_ext = 'export?exportFormat=pdf&format=pdf' // export as pdf
+ '&size=letter' // paper size
+ '&portrait=false' // orientation, false for landscape
+ '&fitw=true' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
+ '&pagenumbers=false&gridlines=false' // hide pagenumbers 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();
var response = UrlFetchApp.fetch(url + url_ext + SpreadsheetApp.getActiveSheet().getSheetId(), {
headers: {
'Authorization': 'Bearer ' + token
}
});
var email = SpreadsheetApp.getActiveSheet().getRange("A3").getValue();
var message = "Versal Course Updates";
var subject = "Versal Course Progress Updates";
MailApp.sendEmail(email, subject, message, {attachments:[response]});
};
//Traiing Week Email
function TRAININGWEEK() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Email subject and message body
var url = ss.getUrl();
url = url.replace(/edit$/,'');
var url_ext = 'export?exportFormat=pdf&format=pdf' // export as pdf
+ '&size=letter' // paper size
+ '&portrait=true' // orientation, false for landscape
+ '&fitw=true' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
+ '&pagenumbers=false&gridlines=false' // hide pagenumbers 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();
var response = UrlFetchApp.fetch("url + url_ext + SpreadsheetApp.getActiveSheet().getSheetId(), {
headers: {
'Authorization': 'Bearer ' + token
}
});
var email = SpreadsheetApp.getActiveSheet().getRange("A3").getValue();
var message = "Training Information";
var subject = "Stratus Training Information";
MailApp.sendEmail(email, subject, message, {attachments:[response]});
};
其他人都收到错误,我每次都成功:
【问题讨论】:
-
您的问题以“截断的服务器响应:”结尾——这是否意味着您的问题还有更多?
-
我有这个错误:“必要的授权您必须请求访问所有者或登录到具有必要权限的帐户。了解更多”
-
不,这是来自谷歌的消息的范围,没有更多的问题。只是想知道为什么它对我有用,但对其他人没有用。
-
尝试关注this post,看看会有什么回复。
-
要从 UrlFetch 中获取完整的错误,我相信您需要在请求中添加参数:“muteHttpExceptions”:true。那应该给你一些不被截断的东西。我没有看到为什么这里需要授权标头。与此问题无关,但您应该删除“//或 DocumentApp 或 FormApp”。添加菜单的位置(来自文档中的示例)。注释掉的代码仍会生成权限请求 - 因此您的最终用户最终会看到比实际情况更令人生畏的请求。
标签: google-apps-script google-sheets