Home > OS >  Trying to create a script for trigger mail based on a condition in google sheets
Trying to create a script for trigger mail based on a condition in google sheets

Time:11-24

So this is the script am using, it sends out a blank mail without any content in them. Here I am trying to compare two columns and if the value in one column is great am trying to add the row header to the list which has to be sent out via mail. Can someone help me out pls.

function Mailer1() {
var ss = SpreadsheetApp.openById("");    
var sheet = ss.getSheetByName("Today");  
var values = sheet.getRange("AD2:AD").getValues();
var value1s = sheet.getRange("AE2:AE").getValues();
var results = [];
for(var i=0;i<values.length;i  )
{
  if(values[i]>value1s[i])
  {
     var value5 = sheet.getRange(i 2,1).getValue();
     results.concat(value5);
  }
}
MailApp.sendEmail('[email protected]','Restock Needed At:',results);

};

CodePudding user response:

I'm guessing that the values in 30 and 31 are some sort of quantities and the value in column 1 is a date.

Try doing this:

function Mailer1() {
  var ss = SpreadsheetApp.openById("");
  var sh = ss.getSheetByName("Today");
  var values = sh.getRange(2, 30, sh.getLastRow() - 1).getValues().flat();
  var value1s = sh.getRange(2, 31, sh.getLastRow() - 1).getValues().flat();
  var results = [];
  for (var i = 0; i < values.length; i  ) {
    if (values[i] > value1s[i]) {
      results.push(sh.getRange(i   2, 1).getValue());
    }
  }
  MailApp.sendEmail('[email protected]', 'Restock Needed At:', results.join('\n'));
};
  • Related