gpt4 book ai didi

javascript - 从 Google 表单更新单元格后从 Google 表格发送电子邮件

转载 作者:行者123 更新时间:2023-11-30 19:21:47 25 4
gpt4 key购买 nike

你好,我现在有这个代码可以发送电子邮件:

function sendNotification(e){

if(e.range.getColumn()==4 && e.value=='Air Filter'){
var recipients = "xx@email.com";
var subject = "Update on "+e.range.getSheet().getName();
var body = "Air filter 50751-123 needs ordered for the Hurco"
MailApp.sendEmail(recipients, subject, body)
}

if(e.range.getColumn()==4 && e.value=='Coolant'){
var recipients = "xx@email.com"&&"xyx@email.com";
var subject = "Update on "+e.range.getSheet().getName();
var body = "Coolant is needed for the Hurco"
MailApp.sendEmail(recipients, subject, body)

}

}

我的问题是,当将 Google 表单提交到我的 Google 表格中,然后将某个单词提交到某个列中时,我希望 Google 向所需的收件人发送一封电子邮件。

当我实际进入工作表并编辑列时,我让它工作,但我希望它在输入表单时工作。 (这仅在我有 onEdit 的触发器时有效,当它在 onChange 时,我收到错误“TypeError:无法调用未定义的方法“getColumn”。(第 3 行,文件“Email Notif”))

它将要查看的列将有 4 个不同的词,并且每个词都可以链接到不同的收件人。 “空气滤清器”属于一个人,而“冷却液”则属于两个不同的人。

谢谢

最佳答案

要在提交表单时发送电子邮件,您需要使用 "on form submit" installable trigger .

必须更新代码才能使用正确的 event properties .您使用的是“on edit”事件的 .value 属性,但“on form submit”事件具有 .values,它是一个值数组,或者 .namedValues 这是一个对象,其中表单问题是键(即响应表的列标题)。

范围将是表单提交,因此检查第 4 列的范围没有多大意义,就像您需要为编辑事件所做的那样。

function sendNotification(e) {
var recipients = "xx@email.com";
var subject = "Update on " + e.range.getSheet().getName();
var body = "";
if (e.namedValues["Item that is needed."] == 'Air Filter') {
body = "Air filter 50751-123 needs ordered for the Hurco";
} else if (e.namedValues["Item that is needed."] == 'Coolant') {
recipients += ",xyx@email.com";
body = "Coolant is needed for the Hurco";
}
if (body.length > 0) {
MailApp.sendEmail(recipients, subject, body);
}
}

编辑:

根据评论中的要求,更新包括根据选定的列号检查以前提交的重复项。

function sendNotification(e) {
var recipients = "xx@email.com";
var subject = "Update on " + e.range.getSheet().getName();
var body = "";
if (checkForSameSubmission(e, 24, [2, 3])) { // event, hours to look back, columns to check (zero index!)
subject += " DUPLICATE";
body += "WARNING: DUPLICATE!\n";
}
if (e.namedValues["Item that is needed."] == 'Air Filter') {
body += "Air filter 50751-123 needs ordered for the Hurco";
} else if (e.namedValues["Item that is needed."] == 'Coolant') {
recipients += ",xyx@email.com";
body += "Coolant is needed for the Hurco";
}
if (body.length > 0) {
MailApp.sendEmail(recipients, subject, body);
}
}

// JSON.stringify will let us compare array values as strings
function checkForSameSubmission(event, hours) {
var sheetData = event.range.getSheet().getDataRange().getValues();
sheetData.shift(); // remove header row
sheetData.pop(); // remove just submitted row
var byColumns = function (_, index) { // for Array.prototype.filter
return columns.indexOf(index) > -1;
}
var toStrings = function (value) { // for Array.prototype.map
return String(value); // ensure all numbers become strings for final comparison
};
var thisSubmissionData = JSON.stringify(event.values.filter(byColumns).map(toStrings));
var isSameSubmission = function (dataRow) {
return thisSubmissionData === JSON.stringify(dataRow.filter(byColumns).map(toStrings));
}
return checkPreviousDataByHours(hours, sheetData, isSameSubmission);
}

function checkPreviousDataByHours(hours, data, someCallback) {
var minusHours = Date.now() - 1000*60*60*hours;
var lastHoursData = data.filter(function (row) { return +row[0] > minusHours; });
return lastHoursData.some(someCallback);
}

关于javascript - 从 Google 表单更新单元格后从 Google 表格发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57382059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com