我在Google表格中运行了以下Google脚本。
function sendNotification(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var emailAdd = "email@yourdomain.com";
if(event.range.getA1Notation().indexOf("G") > -1 && sheet.getRange("G" + row).getDisplayValue() > 999 && emailAdd.length > 1)
{
var rowVals = getActiveRowValues(sheet);
var aliases = GmailApp.getAliases();
Logger.log(aliases);
var bodyHTML,o,sendTO,subject;//Declare variables without assigning a value
o = {};//Create an empty object
bodyHTML = "There has been a new allocation request from " + rowVals.name + " in the " + rowVals.team + " team.<br \> <br \> "
+ "<table border = \"1\" cellpadding=\"10\" cellspacing=\"0\"><tr><th>Issuing Depot</th><th>Delivery Date</th><th>Case Quantity</th></tr><tr><td>"+rowVals.depot+"</td><td>"+rowVals.date+"</td><td>"+rowVals.quantity+"</td></tr></table>"
+ "<br \>To view the full details of the request, use the link below.<br \> <br \>" +
"<a href=\"https://docs.google.com\">Allocation Requests</a>"
+"<br \> <br \><i>This is an automated email. Please do not reply to it.<\i>";
o.htmlBody = bodyHTML;//Add the HTML to the object with a property name of htmlBody
o.from = aliases[0]; //Add the from option to the object
sendTO = "email@yourdomain.com";
subject = "Allocation Request - " + rowVals.quantity + " cases on " + rowVals.date,
GmailApp.sendEmail(sendTO,subject,"",o);//Leave the third parameter as an empty string because the htmlBody advanced parameter is set in the object.
};
}
function getActiveRowValues(sheet){
var cellRow = sheet.getActiveRange().getRow();
// get depot value
var depotCell = sheet.getRange("E" + cellRow);
var depot = depotCell.getDisplayValue();
// get date value
var dateCell = sheet.getRange("F" + cellRow);
var date = dateCell.getDisplayValue();
// get quantity value
var quantCell = sheet.getRange("G" + cellRow);
var quant = quantCell.getDisplayValue();
// return an object with your values
var nameCell = sheet.getRange("B" + cellRow);
var name = nameCell.getDisplayValue();
var teamCell = sheet.getRange("C" + cellRow);
var team = teamCell.getDisplayValue();
return {
depot: depot,
date: date,
quantity: quant,
name: name,
team: team
} }
它运行正常,但如果填写电子表格的人没有按升序填写列,则发送的电子邮件会丢失信息。
有没有办法延迟脚本的运行,直到行(B,C,D,E,F&amp; G列)完成?我查看了utilities.sleep
但不确定将其放在脚本中的位置。当我试图这样做时,它似乎没有任何区别。
答案 0 :(得分:1)
测试返回对象是否缺少值。以下代码执行此操作的方式是将对象转换为数组,然后获取数组的长度。数组中应该有4个元素。如果未定义进入对象的任何变量的值,则该元素将丢失,因此该数组将少于4个元素。
function sendNotification() {
var rowVals = getActiveRowValues(sheet);//Returns an object
var testArray = JSON.stringify(o).split(",");//Convert object to an array
Logger.log('length: ' + testArray.length)
if (testArray.length !== 4) {//Object must have 4 elements
Browser.msgBox('There is missing data!');
return; //quit
}
}
function getActiveRowValues() {
var depot = 'something';
var date;//For testing purposes - leave this as undefined
var name = 'the name';
var team = 'team is';
var o = {
depot: depot,
date: date,
name: name,
team: team
}
Logger.log(JSON.stringify(o))
return o;
}
您可以通过突出显示缺少数据的单元格,或者确切地确定缺少哪个数据并通知用户来改进这一点。
答案 1 :(得分:0)
这可能不是您想要听到的答案。但我并不熟悉任何与特定细胞相关的触发器。问题出现了很多次,我们最接近的是编辑事件。我想你可以检查每个编辑,看看适当的单元格是否包含适当的数据。就个人而言,我更喜欢这种情况的对话框或侧边栏,然后我可以在html环境中拥有Javascript的所有功能来帮助表单提交过程,最后我可能只是在那里放了一个发送按钮
答案 2 :(得分:0)
我继续玩utilities.sleep
,现在让它工作如下所示。
function sendNotification(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
if(event.range.getA1Notation().indexOf("G") > -1 && sheet.getRange("G" + row).getDisplayValue() > 999)
{
Utilities.sleep(1200000)
var rowVals = getActiveRowValues(sheet);
var aliases = GmailApp.getAliases();
Logger.log(aliases);
var bodyHTML,o,sendTO,subject;//Declare variables without assigning a value
o = {};//Create an empty object
bodyHTML = "There has been a new allocation request from " + rowVals.name + " in the " + rowVals.team + " team.<br \> <br \> "
+ "<table border = \"1\" cellpadding=\"10\" cellspacing=\"0\"><tr><th>Issuing Depot</th><th>Delivery Date</th><th>Case Quantity</th></tr><tr><td>"+rowVals.depot+"</td><td>"+rowVals.date+"</td><td>"+rowVals.quantity+"</td></tr></table>"
+ "<br \>To view the full details of the request, use the link below.<br \> <br \>" +
"<a href=\"https://docs.google.com\">Allocation Requests</a>"
+"<br \> <br \><i>This is an automated email. Please do not reply to it.<\i>";
o.htmlBody = bodyHTML;//Add the HTML to the object with a property name of htmlBody
o.from = aliases[0]; //Add the from option to the object
sendTO = "email@yourdomain.com";
subject = "Allocation Request - " + rowVals.quantity + " cases on " + rowVals.date,
GmailApp.sendEmail(sendTO,subject,"",o);//Leave the third parameter as an empty string because the htmlBody advanced parameter is set in the object.
};
}
function getActiveRowValues(sheet){
var cellRow = sheet.getActiveRange().getRow();
// get depot value
var depotCell = sheet.getRange("E" + cellRow);
var depot = depotCell.getDisplayValue();
// get date value
var dateCell = sheet.getRange("F" + cellRow);
var date = dateCell.getDisplayValue();
// get quantity value
var quantCell = sheet.getRange("G" + cellRow);
var quant = quantCell.getDisplayValue();
// return an object with your values
var nameCell = sheet.getRange("B" + cellRow);
var name = nameCell.getDisplayValue();
var teamCell = sheet.getRange("C" + cellRow);
var team = teamCell.getDisplayValue();
return {
depot: depot,
date: date,
quantity: quant,
name: name,
team: team
} }
现在它延迟了脚本运行1分钟,在脚本从中提取数据之前,剩余单元格有时间完成。