Json 测试是否缺少用户输入,并停止或延迟Google脚本的运行
我有下面的谷歌脚本运行在谷歌表Json 测试是否缺少用户输入,并停止或延迟Google脚本的运行,json,google-apps-script,google-sheets,Json,Google Apps Script,Google Sheets,我有下面的谷歌脚本运行在谷歌表 function sendNotification(event) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getActiveSheet(); var row = sheet.getActiveRange().getRow(); var cellvalue = ss.getActiveCell().getValue().toString(); var ema
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
} }
函数发送通知(事件){
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”+行)。getDisplayValue()>999和&emailAdd.length>1)
{
var rowVals=getActiveRowValues(表);
var alias=GmailApp.getAlias();
Logger.log(别名);
var bodyHTML,o,sendTO,subject;//在不赋值的情况下声明变量
o={};//创建一个空对象
bodyHTML=“有来自“+rowVals.team+”团队中“+rowVals.name+”的新分配请求。
”
+“发出仓库交货日期箱数量”+rowVals.depot+“”+rowVals.date+“”+rowVals.Quantity+“”
+“
要查看请求的完整详细信息,请使用下面的链接。
”+
""
+“
这是一封自动发送的电子邮件。请不要回复它。”;
o、 htmlBody=bodyHTML;//将HTML添加到属性名为htmlBody的对象中
o、 from=别名[0];//将from选项添加到对象
sendTO=”email@yourdomain.com";
subject=“分配请求-”+rowVals.quantity+“+rowVals.date”上的案例,
GmailApp.sendmail(sendTO,subject,“,o);//将第三个参数保留为空字符串,因为htmlBody高级参数是在对象中设置的。
};
}
函数getActiveRowValues(工作表){
var cellRow=sheet.getActiveRange().getRow();
//获取仓库价值
var depotCell=sheet.getRange(“E”+cellRow);
var depot=depotCell.getDisplayValue();
//获取日期值
var dateCell=sheet.getRange(“F”+cellRow);
var date=dateCell.getDisplayValue();
//获取数量值
var quantCell=sheet.getRange(“G”+cellRow);
var quant=quantCell.getDisplayValue();
//返回带有值的对象
变量nameCell=sheet.getRange(“B”+cellRow);
var name=nameCell.getDisplayValue();
var teamCell=sheet.getRange(“C”+cellRow);
var team=teamCell.getDisplayValue();
返回{
仓库:仓库,
日期:日期:,
数量:数量,
姓名:姓名,,
团队:团队
} }
它工作正常,但如果填写电子表格的人没有按升序填写列,则发送的电子邮件会丢失信息。
是否有办法延迟脚本的运行,直到行(B、C、D、E、F和G列)完成?我已经看过了
实用程序。sleep
,但不确定在脚本中放在哪里。当我尝试这样做时,似乎没有任何区别。这可能不是你想要听到的答案。但我不熟悉任何与特定细胞相关的触发器。这个问题已经提过很多次了,最近的一次是关于编辑的事件。我想您可以在编辑时检查每个单元格是否包含适当的数据。就我个人而言,我更喜欢在这种情况下使用一个对话框或侧边栏,这样我就可以在html环境中使用Javascript的所有功能来帮助表单提交过程,最后我可能会在那里放置一个发送按钮。测试返回对象是否缺少值。以下代码执行此操作的方法是将对象转换为数组,然后获取数组的长度。数组中应该有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;
}
您可以通过突出显示丢失数据的单元格,或确定丢失的数据片段并通知用户来改进此功能。我继续使用
实用程序。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
} }
函数发送通知(事件){
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”+行).getDisplayValue()>999)
{
公用设施。睡眠(1200000)
var rowVals=getActiveRowValues(表);
var alias=GmailApp.getAlias();
Logger.log(别名);
var bodyHTML,o,sendTO,subject;//在不赋值的情况下声明变量
o={};//创建一个空对象
bodyHTML=“有来自“+rowVals.team+”团队中“+rowVals.name+”的新分配请求。
”
+“发出仓库交货日期箱数量”+rowVals.depot+“”+rowVals.date+“”+rowVals.Quantity+“”
+“
要查看请求的完整详细信息,请使用下面的链接。
”+
""
+“
这是一封自动发送的电子邮件。请不要回复它。”;
o、 htmlBody=bodyHTML;//将HTML添加到属性名为htmlBody的对象中
o、 from=别名[0];//将from选项添加到对象
sendTO=”email@yourdomain.com";
subject=“分配请求-”+rowVals.quantity+“+rowVals.date”上的案例,
GmailApp.sendmail(sendTO,subject,“,o);//将第三个参数保留为空字符串,因为htmlBody高级参数是在对象中设置的。
};
}
函数getActiveRowValues(工作表){
var cellRow=sheet.getActiveRange().getRow();
//获取仓库价值
var depotCell=sheet.getRange(“E”+cellRow);
var depot=depotCell.getDisplayValue();
//获取日期值
var dateCell=sheet.getRange(“F”+cellRow);
var date=dateCell.getDisplayValue();
//获取数量值
var quantCell=sheet.getRange(“G”+cellRow);
var quant=quantCell.getDisplayValue();
//返回带有值的对象
变量nameCell=sheet.getRange(“B”+cellRow);
var name=nameCell.getDisplayValue();
var teamCell=sheet.getRange(“C”+cellR