Google apps script 测试气体中X的范围
我有一个表格,我为一系列的演讲活动预订了几天的时间。当这个范围内的某一天达到房间容量时,我正试图让它通过电子邮件发送给我。我在测量一个单元格的容量,它是假的,直到它达到容量,然后变为真 我的容量测试变量是Google apps script 测试气体中X的范围,google-apps-script,Google Apps Script,我有一个表格,我为一系列的演讲活动预订了几天的时间。当这个范围内的某一天达到房间容量时,我正试图让它通过电子邮件发送给我。我在测量一个单元格的容量,它是假的,直到它达到容量,然后变为真 我的容量测试变量是 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("counts"); var currentValue = sheet.getRange("D2:D13").getValues(); Logger.log('cu
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("counts");
var currentValue = sheet.getRange("D2:D13").getValues();
Logger.log('currentValue = '+currentValue)
这将在日志中返回,currentValue=false,false,false,false,false,false,false,false,false,false,false,false,false,false
我的脚本中有一行应该会触发一封邮件,上面写着
if (currentValue = "true")
我在这一行尝试过各种组合。目前,每次有人提交预订时,它都会发送电子邮件。:)
我的“如果”语法应该是什么,这样它只能在currentValue范围内的某个地方出现“true”时发送电子邮件
谢谢你能借给我的任何帮助。Dave要检查是否相等,必须使用
=
运算符。使用一个等号表示您试图将“true”的值分配给变量currentValue
。要检查它们是否相等,请使用==
其次,您需要遍历currentValue
的每个值,以检查它们是否为真。为此:
//currentValue is a 2D array
var nRows = currentValue.length;
for(var i=0;i<nRows;i++) {
if (currentValue[i][0]=="true") {
//send email
}
}
//currentValue是一个2D数组
var nRows=currentValue.length;
对于(var i=0;i调用getValues()时,它返回一个2D对象[][]数组,因此当您记录currentValue时,您应该得到如下结果:
[[true], [true], [true], [true], [true], [true], [true], [false], [true], [false]]
电子表格具有存储布尔值的功能,这就是为什么每当您键入true时,它都会将该值转换为大写,并将该值解释为布尔值。因此,当您在脚本中检索该值时,应将其视为布尔值
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var currentValues = sheet.getRange("D2:D13").getValues();
for (var x in currentValues) {
var value = currentValues[x][0];
if (value) {
Logger.log('True value');
// Send email, but do you want to send an email every single time this script
// is run??
} else if (value == false) { // You can leave this as else instead of else if
Logger.log('false value')
}
}
我假设您希望在用户提交表单时触发此脚本。而不是每次脚本看到“true”时都发送电子邮件值,您可以将CellRange存储在scriptDB中,并仅在数据库中不存在CellRange的情况下发送电子邮件。我正在为收到的答案添加其他注释,并希望对代码进行更详细的注释,以便像我这样的新手可以从答案中学到更多信息。我使用了Phil提供给我的代码,但我拒绝使用一开始我把它放错位置了
function onFormSubmit(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("counts");
var currentValue = sheet.getRange("D2:D13").getValues();
//currentValue is a 2D array (think of it like a matrix). You can get an
//individual element from it by calling currentValue[row#][col#]
// for example: currentValue[3][0] will give you the item in the 4th row, 1st column
// NOTE: arrays are zero-based, so the first item is index 0.
//you can also obtain a row of elements
// var rowThree = currentValue[2];
// rowThree is now a 1D array. get an element by calling rowThree[col#]
var nRows = currentValue.length;
//currentValue.length gives you the number of elements in the first dimension
//of the 2D array. so in this case, it gives you the number of rows.
for(var i=0;i<nRows;i++) {
//creates a new variable i, defines i is initially 0.
//for every loop while i is less than nRows, run the loop, then increment i by 1
//so this will run the following code once for every row number.
Logger.log('row '+i+' col[0] = '+currentValue[i][0]); //log the value in the current row
if (currentValue[i][0]=="true") //the double equals operator checks for equality
//whereas a single equals would ASSIGN the value
//"true" to the array at location [i][0]
{
MailApp.sendEmail("dave@davelalande.com","Talks Room Capacity Reached","A date within the next round of Talks has reached capacity, " +
"\nplease check the sheet https://docs.google.com/spreadsheet/ccc?key=""#gid=1 and removed the date.");
}
}
}
我现在唯一想记录的是
Logger.log('row'+i+'列[0]='+currentValue[i][0])
正在输出这个
row 0 col[0] = false
row 1 col[0] = false
row 2 col[0] = false
row 3 col[0] = false
row 4 col[0] = false
row 5 col[0] = false
row 6 col[0] = false
row 7 col[0] = false
row 8 col[0] = false
row 9 col[0] = false
row 10 col[0] = false
row 11 col[0] = false
这似乎起到了作用。我想给你的代码添加注释,让像我这样的新手理解它在做什么。我一直希望SOF上的heavy会对函数进行注释,所以我想对代码进行注释,如果我不理解,请你纠正我。再次感谢Phil…我对你在这里的意思感到困惑。你想要我的答案吗代码注释?或您的代码注释?如果我对任何事情都不清楚,我提前表示歉意!Phil,我创建了一个进一步记录解决方案的示例。我很抱歉要求您提供更多信息,因为您一开始就非常慷慨地提供了解决方案。我只是想提供更多关于每行代码的实际信息我在为我们这些新手程序员学习。我肯定还有什么地方不对劲。|当我在生产表中部署解决方案时,我打开了错误通知,我收到了一个错误。我忘记了在我的测试环境中打开它们。|这是错误。2/12/13 12:57 PM onFormSubmit TypeError:Cannot read pr未定义的属性“0”。(第39行)formSubmit 2/12/13 12:57 PM第39行是…如果(currentValue[i][0]==“true”);您是否在for循环中记录currentValue?Logger.log('currentValue='+currentValue)
?另外,请务必查看我对下面答案的评论。我没有收到类似的日志。我的日志看起来像currentValue=false、false、false、false、false、false、false、false、false、false、false、false、false。此解决方案实际上有效。我最初认为上面的解决方案有效,但我没有设置错误通知在我的测试表中,新手犯了错误。Phil的解决方案似乎有一个关于TypeError的错误:无法从undefined中读取属性“0”。在昨晚阅读之后,有很多人看到了这个问题。我不明白什么是undefined?
row 0 col[0] = false
row 1 col[0] = false
row 2 col[0] = false
row 3 col[0] = false
row 4 col[0] = false
row 5 col[0] = false
row 6 col[0] = false
row 7 col[0] = false
row 8 col[0] = false
row 9 col[0] = false
row 10 col[0] = false
row 11 col[0] = false