Home > Back-end >  Running a script on multiple checkboxes with loop
Running a script on multiple checkboxes with loop

Time:06-21

I have a file, I want to use it on my mobile phone. I'm trying to send an email to contact every time I check one of the boxes. For now, I'm doing a function for every checkbox, 20 in total. Is there a way to do it in a loop? Thanks a lot for your help.

And here is my code:

function onEditTriggerCourse1(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Courses Attribuées");
  var range = sheet.getActiveRange()
  if (range.isChecked()) {
    if (range.getA1Notation() == "g2") {
    sheet.getRange('a2').setValue(new Date());
    sendValidation1()
    
    range.uncheck();
    
    } else  {
    range.uncheck()
  }
}
}

function onEditTriggerCourse2() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Nouveau Client");
  var range = sheet.getActiveRange()
  if (range.isChecked()) {
    if (range.getA1Notation() == "g3") {
    sheet.getRange('a3').setValue(new Date());
    sendValidation2()
    range.uncheck();
    
    } else  {
    range.uncheck()
  }
}
}


function sendValidation1() {
 var recipient1= SpreadsheetApp.getActiveSpreadsheet().getRange("email1").getValue();
 var subject1=SpreadsheetApp.getActiveSpreadsheet().getRange("subject2").getValue();
 var body1=SpreadsheetApp.getActiveSpreadsheet().getRange("body1").getValue();
 var nameOfSender1=SpreadsheetApp.getActiveSpreadsheet().getRange("sender1").getValue();;
   var message = {
    to: recipient1,
    subject: subject1,
    body: body1,
    name: nameOfSender1,
  }

   MailApp.sendEmail(message);
}

function sendValidation2() {
 var recipient2= SpreadsheetApp.getActiveSpreadsheet().getRange("email1").getValue();
 var subject2=SpreadsheetApp.getActiveSpreadsheet().getRange("subject2").getValue();
 var body2=SpreadsheetApp.getActiveSpreadsheet().getRange("body2").getValue();
 var nameOfSender2=SpreadsheetApp.getActiveSpreadsheet().getRange("sender2").getValue();;
   var message = {
    to: recipient2,
    subject: subject2,
    body: body2,
    name: nameOfSender2,
  }

   MailApp.sendEmail(message);
}

CodePudding user response:

Try the enter image description here

After:

enter image description here

Email:

enter image description here

Note: You can edit the recipient, subject, body and sender by changing the 2nd parameter of getRange() in the sendEmail function. In my code, I only pick what I think will fit and just to test the sendEmail Function. Also, let me know if the code I provided works and if you have any questions.

References:

  • Related