如果在范围上

时间:2019-06-12 02:41:37

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

我想测试一个单元格范围(例如:L1:S22),如果该范围内的某个单元格中的值大于0,它应该发送电子邮件通知。

到目前为止,我只能获得一个单元格值,而不能获得范围。

function CheckSales(){

var app = SpreadsheetApp;
var activeSheet = app.getActiveSpreadsheet().getActiveSheet();

var temp = activeSheet.getRange("L1:S22").getValues();
var ui = SpreadsheetApp.getUi(); 

Logger.log(temp);

if (temp > 0){
  // Fetch the email address
  var emailRange = SpreadsheetApp.getActiveSpreadsheet()
                   .getSheetByName("Sheet1").getRange("L1:S22");
  var emailAddress = emailRange.getValues();

  // Send Alert Email.
  var message = 'This month your values were ' + temp; 
  var subject = 'Range exceeded Alert';
  MailApp.sendEmail(emailAddress, subject, message);
 }
} 

我希望这样的输出:本月,您在此单元格“”中的值为800

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助!它只会在一封电子邮件中分享所有月份的摘要。

function CheckSales(){

  var app = SpreadsheetApp;
  var activeSheet = app.getActiveSpreadsheet().getActiveSheet();

  var data=activeSheet.getDataRange().getValues();

  var emailAddress=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("B2").getValue();
  var resultArr=[];

  //To Loop through the whole data Rows
  for(var i=1;i<data.length;i++)
  {
    //Takes columns from L to S (To loop through the Columns)
    for(var j=11;j<19;j++)
    {
      var cellVal=data[i][j];
      Logger.log(cellVal)
      if(cellVal>0)
      {
        //Stores the Part No, Month Header Value of the Column, Cell Value which is greater then 0
        resultArr.push([data[i][0],data[0][j],cellVal])
      }
    }
  }
  if(resultArr.length>0)
  {
    var subject = 'Range exceeded Alert';

    //Creates a body through the obtained values
    var body='';
    for(var m=0;m<resultArr.length;m++)
    {
      body+="For Part No "+resultArr[m][0].toString()+" and Month "+resultArr[m][1].toString()+", Value is "+resultArr[m][2].toString()+"<br>";
    }

    MailApp.sendEmail({to:emailAddress,subject:subject,htmlBody:body});
  }

}