Email APP Script Firing Multiple Functions on change

28 views
Skip to first unread message

Rajkumar Trivedi

unread,
Aug 21, 2020, 6:40:30 AM8/21/20
to Google Apps Script Community
Dear Community,

I have created 3 scripts and I am able to send emails when the cell value has changed my problem is that multiple scripts are getting fired even when the condition is not matched. Below are the 4 Scripts

The following script should only fire when the status is changed to COMPLETE for the range mentioned


function SendEmailToAditya() {
  var checkCurrentStatus = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ToothAcheStatusSheet").getRange("N2:N999");
  var statusinfo= checkCurrentStatus.getValues();
  
  if(statusinfo=='COMPLETE'){
  {
    var EmailMessage = 'Hey AD,VA has finished reviewing toothache.care blog, your time to do the graphics.';
    var esubject = 'Graphic Request for Toothache.care';
    MailApp.sendEmail('e...@e.com', esubject, EmailMessage);
    
  }
  }
   
}

function SendEmailToKS() {
  var checkCurrentStatus = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ToothAcheStatusSheet").getRange("S2:S999");
  var statusinfo= checkCurrentStatus.getValue();
  if(statusinfo=='COMPLETED')
  {
    var EmailMessage = 'Hey Team,Graphics are ready awaitng final approval';
    var esubject = 'Graphic Complete - Sending for approval';
    MailApp.sendEmail('em...@a.com', esubject, EmailMessage);
       
  }
}

The following should fire only when the status is changed to OK and only for the range mentioned

function FinalApproval() {
  var checkCurrentStatus = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ToothAcheStatusSheet").getRange("U2:U999");
  var statusinfo= checkCurrentStatus.getValue();
  if(statusinfo=='OK')
  {
    var EmailMessage = 'Good to go for the articles. Please publish';
    var esubject = 'Approval from M';
    MailApp.sendEmail('r...@tab32.com', esubject, EmailMessage);
  
    
  }
}


The following should fire only when the status is changed to DONE and only for the range mentioned


function Donel() {
  var checkCurrentStatus = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ToothAcheStatusSheet").getRange("V2:V999");
  var statusinfo= checkCurrentStatus.getValue();
  if(statusinfo=='DONE')
  {
    var EmailMessage = 'Post Published';
    var esubject = 'Post Done';
    MailApp.sendEmail('r...@r.com', esubject, EmailMessage);
   
  }
}


The emails are getting fired and I am even receiving the emails, the problem is that when the first script range has data in it and I change the cell value for the range mentioned in the second script it fires both the script and send me two emails and same is with the third if i update the cell value in the range mentioned in the third script and if there is data present already in the first range and second range then it will fire 3 emails.

I am looking out that it should fire email only for the cell value updated for the script.  Need guidance on what I am doing wrong.

I have created a trigger for all these 3 scripts on change option inside the trigger.

Jean-Luc Vanhulst

unread,
Aug 21, 2020, 8:11:02 AM8/21/20
to google-apps-sc...@googlegroups.com
Getrange().getvalues() returns an array with values so you cannot compare that with a string. You can check one cell like if you do a getrange( ‘n3’).getValue() == ‘COMPLETE’

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/678b546f-a6fb-469e-bad6-541382d1950do%40googlegroups.com.
--

Cor

unread,
Aug 21, 2020, 8:15:30 AM8/21/20
to google-apps-sc...@googlegroups.com
You can loop through the array and then compare it with any string you need.
 
Hth.

Op vr 21 aug. 2020 14:11 schreef Jean-Luc Vanhulst <jlvan...@write2market.com>:

Rajkumar Trivedi

unread,
Aug 21, 2020, 10:30:52 AM8/21/20
to Google Apps Script Community
Can anyone share code with me?
Reply all
Reply to author
Forward
0 new messages