如果特定单元格包含的值大于10

时间:2017-03-31 07:10:54

标签: javascript google-apps-script google-sheets

所以我有一个可以启动电子邮件的脚本,我想让它在特定单元格(A5)包含超过10的值时运行。这是脚本:

function onEdit() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rownum = 1; // #of rows
  var rowstart = 2; // where to start row

  // get range of cell
  // column 1 = email, column 2 is first name
  var dataRange = sheet.getRange(rowstart, 1, rownum, 2)


  var data = dataRange.getValues()
  for (i in data) {
    if(sheet.getRange(5,1).getValue()>10){      //change row and column in get range to match what you need
      var row = data[i];
      var emailadr = row[0]; // data select
      var msgcontent = "Hey " + row[1] + ",\n Just:)chilling \n    Sent from my email"; // body
      var sbjt = "You have mail!"; //subject
      MailApp.sendEmail(emailadr, sbjt, msgcontent);}
    }
  }

在当前项目的触发器下,我有以下内容:

  

运行:onEdit

     

事件:来自电子表格编辑

现在,当我设置单元格" A5"对于" 21",脚本不会运行。问题是什么?我似乎无法弄明白。谢谢。

1 个答案:

答案 0 :(得分:0)

使用记录器显示它确实有效。您可以在下面的示例中看到。您可能希望尝试使用基于时间的触发器并将其设置为检查每x个时间量并发送电子邮件。 Google可能不允许sendemail运行" onedit"



function onEdit() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rownum = 1; // #of rows
  var rowstart = 2; // where to start row

  // get range of cell
  // column 1 = email, column 2 is first name
  var dataRange = sheet.getRange(rowstart, 1, rownum, 2)


  var data = dataRange.getValues()
  
  
 for (i in data) {
   if(sheet.getRange(5,1).getValue()>10){      //change row and column in get range to match what you need
     var row = data[i];
         Logger.log("Yes it is larger than 10");
      
    //  var emailadr = row[0]; // data select
    //  var msgcontent = "Hey " + row[1] + ",\n Just:)chilling \n    Sent from my email"; // body
    //  var sbjt = "You have mail!"; //subject
    //   MailApp.sendEmail(emailadr, sbjt, msgcontent);
   }
  }
}