我正在尝试创建一个用于项目管理的工作表,以便在许多对话者之间轻松协作(我知道这里有免费的工具,与公共(public)组织合作很难在项目中插入新程序)。
这是创建的示例文件:Google Sheet for Test
想法是每次输入新问题时,作者都可以选择菜单“问题”> “发送问题”。 它应该向收件人电子邮件地址(C列)发送自动电子邮件......在填写问题单元格的情况下(在“I2”中选中的条件)。
但目前,该脚本无法按预期运行。
注意:我目前正在学习 JS。
代码如下:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Question')
.addItem('Send Question', 'sendQuestion')
.addSeparator()
.addItem('Send Answer', 'sendAnswer')
.addToUi();
}
function sendQuestion() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Questions");
var range = sheet.getRange("A22:L2");
var emailvalid = range.getCell(1,7).getValue();
var question = range.getCell(1,5).getValue();
var answer = range.getCell(1,6).getValue();
var qvalid = range.getCell(1,8).getValue();
var ansvalid = range.getCell(1,10).getValue();
if(qvalid == "Nope"){
Browser.msgBox("Please, write a question !");
}
else {
return false};
if(emailvalid == "Nope"){
Browser.msgBox("Please, input a valid email address !");
}
else {
return false};
var from = range.getCell(1,1).getValue();
var recipient = range.getCell(1,2).getValue();
var emailTo = range.getCell(1,3).getValue();
var subject = "Project Management | Your received a new question" + question;
var link = "https://docs.google.com/spreadsheets/d/1QUbw0WNju55h5pk3l8QqVCYa_jSCzrIzMjf0N4v-z8c/edit?usp=sharing"
var options = {}
options.htmlBody = "Hi" + recipient +"," + "<br />"+ "<br />" + "You received a new question from " + from + "<br />" + "<br />" + "You can answer directly into the table here: "<"br />"+"<br />" + link + "<br />" + "<br />"+ "Thank you";
GmailApp.sendEmail(emailTo, subject, " ", options);
}
function emailSent(){
var ui = SpreadsheetApp.getUi();
var sent = sendQuestion("Send Email ?",ui.ButtonSet.YES_NO);
if(sent == ui.Button.YES) {
ui.alert("Your Message has been sent.");
} else {
ui.alert("Email canceled");
}
}
最佳答案
你的主要错误是你有这个 else return false
语句,如果你的验证通过,它基本上终止函数,它应该是相反的,如果验证失败,在您弹出通知问题后。
if(qValid == 'Nope') {
Browser.msgBox('etc...');
return; //return here, not in the else clause
}
//same for email valid
对于此类功能,我还会做很多其他“改进”。但我想这超出了这里的重点,你边走边学,最重要的是它现在可以工作了。
关于javascript - 在工作表中,填充单元格后向特定收件人发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52224871/