我正在为收到的答案添加额外的评论,并希望更详细地评论代码,以便像我这样的新手可以从答案中学到更多。我使用了 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.");
}
}
}
有关 Phil 提供的工作解决方案的更多信息。
我目前正在收到错误消息...
TypeError:无法从未定义中读取属性“0”。(第 38 行)
指的是这条线。
if (currentValue[i][0]=="true");
我当前唯一记录的是 Logger.log('row '+i+' col[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