现在,我正在Google电子表格中使用Google Apps脚本创建一个邮件发送应用程序。 此应用程序执行以下操作。
我使用时应用程序正常工作。 但是其他可以编辑电子表格的用户无法发送电子邮件(显示ui没问题)。
从dubug日志中,我发现没有调用 doPost 函数。
请告诉我这是什么问题。
/*
* @OnlyCurrentDoc
*
* create custome menu in active spreadsheet
*/
function onOpen(e) {
Logger.log("onOpen called");
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [
{name: "MailSendApp", functionName: "displayAppUi"}
];
sheet.addMenu("CustomAppMenu", menuEntries);
}
/*
* @OnlyCurrentDoc
*
* display ui
*/
function displayAppUi() {
Logger.log("displayAppUi called");
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication();
var formPanel = app.createFormPanel().setId('form').setEncoding('multipart/form-data');
var scrollPanel = app.createScrollPanel();
var panel = app.createVerticalPanel();
var appLabel = app.createLabel("Sendmails SpreadSheet App");
var subjectLabel = app.createLabel("Subject");
var subjectTextBox = app.createTextBox().setName("subject");
var bodyLabel = app.createLabel("Body");
var bodyTextArea = app.createTextArea().setName("body");
var attachFile = app.createFileUpload().setName('attachFile');
var sendBtn = app.createSubmitButton().setText("Send ALL");
var sheetName = app.createHidden("sheetName", SpreadsheetApp.getActiveSheet().getName());
// set client handler
var submitClientHandler = app.createClientHandler()
.forEventSource().setEnabled(false)
.forEventSource().setText("Sending...");
sendBtn.addClickHandler(submitClientHandler)
// set sytles
app.setWidth(800);
app.setHeight(400);
panel.setStyleAttributes({width: "100%"});
appLabel.setStyleAttributes({color: "green", fontSize: "2em" });
subjectTextBox.setStyleAttributes({width: "100%"});
bodyTextArea.setHeight("240");
bodyTextArea.setWidth("780");
scrollPanel.setHeight("380");
scrollPanel.setWidth("800");
panel.add(appLabel)
.add(subjectLabel)
.add(subjectTextBox)
.add(bodyLabel)
.add(bodyTextArea)
.add(attachFile)
.add(sheetName)
.add(sendBtn);
scrollPanel.add(panel)
formPanel.add(scrollPanel);
app.add(formPanel);
sheet.show(app);
}
/*
* @OnlyCurrentDoc
*
* handler to send emails to addresses in active sheet with input subject & body.
*/
function doPost(e) {
Logger.log("doPost called");
var param = e.parameter;
var subject = param.subject;
var body = param.body;
var attachFile = param.attachFile;
var sheetName = param.sheetName;
var mailOptions = {};
var mailAttachments = []
// set attachment file if exists
if (attachFile != null) {
mailAttachments.push({
fileName: attachFile.getName(),
mimeType: attachFile.getContentType(),
content: attachFile.getBytes()
});
}
mailOptions["attachments"] = mailAttachments;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var emails = _collectEmailAddresses(sheet);
for(var i=0; i < emails.length; i++) {
var email = emails[i];
GmailApp.sendEmail(email, subject, body, mailOptions);
}
var app = UiApp.getActiveApplication();
app.close();
return app;
}
/*
* collect Email addresses from the argument sheet
*/
function _collectEmailAddresses(sheet) {
var emails = [];
var range = sheet.getDataRange();
var values = range.getValues();
// get skip row numbers(that is header row num) from property
var header_rownum = parseInt(PropertiesService.getScriptProperties().getProperty("header_rownum"));
for (var i = header_rownum; i < range.getLastRow(); i++) {
var email = values[i][1];
emails.push(email);
}
return emails;
}
答案 0 :(得分:0)
在openByID
触发器中使用.getActiveSpreadsheet().getSheetByName(sheetName)
代替doPost
。
var ss = SpreadsheetApp.openById(_spreadsheetId);
var sheet = ss.getSheetByName(_sheetName);