如何遍历行以检查Google Apps脚本中的值

时间:2019-06-05 10:54:14

标签: google-apps-script

我的脚本检查单元格的值,并根据该值触发电子邮件。我需要它遍历行而不是应用于特定的单元格。代码运行良好,但是必须检查该列中的所有值。

function CheckCalStatus() {
var CalStatusRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("Q2"); 
var CalStatus = CalStatusRange.getValue();
if (CalStatus == "INVITED"){
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("P2");
var emailAddress = emailRange.getValues();

// Send Alert Email.
var message = 'Respond to your invite'; 
var subject = 'A Calendar Invite is waiting for your response';
MailApp.sendEmail(emailAddress, subject, message);
}

}

1 个答案:

答案 0 :(得分:1)

function CheckCalStatus() {

  var CalStatusData=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getDataRange().getValues();

  //Loops through each row of the sheet
  for(var i=0;i<CalStatusData.length;i++)
  {
    //Takes data of Q Column
    var CalStatus=CalStatusData[i][16];
    if (CalStatus == "INVITED"){

      //Takes data of P Column
      var emailAddress =CalStatusData[i][15];
      // Send Alert Email.
      var message = 'Respond to your invite'; 
      var subject = 'A Calendar Invite is waiting for your response';
      MailApp.sendEmail(emailAddress, subject, message);
    }
  }
}