如果表示条目包含电子邮件列中的电子邮件,则每当Google表单条目添加新行时,我都会寻求帮助来发送电子邮件。我是Javascript的新手,但是我把一些代码拼凑在一起,我打算在GSheets中运行onEdit触发器。
我的问题是,如果没有电子邮件地址,代码将失败。我需要知道如何将它包装在“if / else”中,或者只是一个简单的错误处理位就可以了,不确定。
如果我使用“if / else”,我需要检查电子邮件列是否包含值。我不需要检查它是否是有效的电子邮件;谷歌表格在提交时已经这样做了。
以下是我现在的代码:
function MessageNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Message Board"));
//
//extracts the values in last row and stores them into a two-dimensional
array called data
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
var dataRange = sheet.getRange(lastRow,3,1,8);
var data = dataRange.getValues();
//
//pull column elements into a one-dimensional array called rowData
for (i in data) {
var rowData = data[i];
var emailAddress = rowData[2];
var poster = rowData[7];
var subject = rowData[3];
var recipName = rowData[6];
var comment = rowData[4];
var replyLink = rowData[5];
//
//
var message = 'Dear ' + recipName + ',\n\n'+poster+' has posted the
following comment directed to you: '+'\n'+comment+'\n\n'+'To reply to this
comment click: '+replyLink;
var subject = subject;
MailApp.sendEmail(emailAddress, subject, message);
}
}
提前感谢你能给我的任何帮助。
答案 0 :(得分:0)
谢谢tehhowch的帮助。我是新手,所以我将不得不继续研究你提到的关于迭代最佳实践的链接。然而,我能够使用一个简单的“if”包装器来实现这一点,结果证明它比我想象的要简单
我确实发现表单提交无法识别活动工作表,因此手动测试我的代码工作,而表单提交没有触发它。
经过一番观察后,我换了:
var ss = SpreadsheetApp.getActiveSpreadsheet();
有了这个:
var ssID = '//insert spreadsheet id here';
var ss = SpreadsheetApp.openById(ssID);
这仍然不起作用,所以我不得不通过删除触发器并将其重新放入(发现此信息:On form submit trigger not working)
来启动它这可能不是最有效的代码,但这是我现在拥有的,它确实有效:
function MessageNotification() {
var ssID = '//insert spreadsheet id here';
var ss = SpreadsheetApp.openById(ssID);
ss.setActiveSheet(ss.getSheetByName("Message Board"));
//extracts the values in last row and stores them into a two-dimensional
array called data
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
var dataRange = sheet.getRange(lastRow,3,1,8);
var data = dataRange.getValues();
//
//pull column elements into a one-dimensional array called rowData
for (i in data) {
var rowData = data[i];
var emailAddress = rowData[2];
var poster = rowData[7];
var subject = rowData[3];
var recipName = rowData[6];
var comment = rowData[4];
var replyLink = rowData[5];
//
//
var message = 'Dear ' + recipName + ',\n\n'+poster+' has posted the
following comment directed to you: '+'\n'+comment+'\n\n'+'To reply to this
comment click: '+replyLink;
var subject = subject;
if(emailAddress)
{
MailApp.sendEmail(emailAddress, subject, message);}
}
}
答案 1 :(得分:0)
如问题评论中所述,您希望使用on form submit
触发器可用的event object
。这可以通过表单或其响应电子表格上的容器绑定脚本访问,只需向接收触发器的函数添加参数即可。
此对象的格式为:
e: {
authMode: <enum>,
namedValues: {
'q1title': [ <q1string> ],
'q2title': [ <q2string> ],
...
},
range: <Range>,
triggerUid: <string>,
values: [<q1string>, <q2string>, ...]
}
使用此对象意味着无需访问电子表格,以便根据表单内容向某人发送电子邮件。
function MessageNotification(e) {
if(!e) return; // No form event object was provided.
var responses = e.namedValues;
var emailQTitle = /* the title of the question that asks for the email */;
// Check that 1) this question exists in the response object, and also
// 2) it has an answer with a value that 3) is "truthy".
// https://developer.mozilla.org/en-US/docs/Glossary/Truthy
if(responses[emailQTitle] // 1
&& responses[emailQTitle].length // 2
&& responses[emailQTitle][0]) // 3
{
var emailAddress = responses[emailQTitle][0];
/* access the responses variable in a similar manner
for the other variables needed to construct the email */
MailApp.sendEmail(emailAddress, ... );
} else {
/* There was no response to the email question. */
// You can use View->Stackdriver Logging to inspect the form response, for
// example, to make sure that it had the format or values you expected.
console.log({form_object: e, responses: responses, emailTitle: emailQTitle});
}
}