当单元格不为空时运行脚本

时间:2019-05-20 07:41:17

标签: google-apps-script google-sheets

当单元格(例如A1)不再为空时,脚本会发送自动邮件,并且需要被触发。

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; 
  var numRows = 1; 

  var dataRange = sheet.getRange(startRow, 1, numRows, 11);

  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var emailAddress = row[9]; 
    var message = row[10];
    var subject = 'Subject';
    MailApp.sendEmail(emailAddress, subject, message);
  }
}

1 个答案:

答案 0 :(得分:0)

您需要的只是一个简单的if语句:

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; 
  var numRows = 1; 

  var dataRange = sheet.getRange(startRow, 1, numRows, 11);

  var data = dataRange.getValues();
  for (var i in data) {
    if (data[i][0] != '') { //check cell A is empty in the row the script is running for.
      var row = data[i];
      var emailAddress = row[9]; 
      var message = row[10];
      var subject = 'Subject';
      MailApp.sendEmail(emailAddress, subject, message);
    }
  }
}