Help with a Schedule Trigger Code

134 views
Skip to first unread message

Ramon Corrales

unread,
Apr 7, 2023, 7:03:29 PM4/7/23
to Google Apps Script Community
I need help with this code.  
The sendEmal Funtion's works when I manually run the code... works perfectly,

 I want to execute the Function called "sendEmail" every Mondays at 8:30 am

I get no errors when saving and running the code.  However, the email is not sent, 

Can I get a new set of eyes and see what needs to be changed.

Thank you.


function setTrigger() {

   //Deletes all existing triggers for the current project

   ScriptApp.getProjectTriggers().forEach(trigger => ScriptApp.deleteTrigger(trigger));
  

   // Sets a new trigger to run the sendemail function every Friday at 01:44 pm
   
   ScriptApp.newTrigger("sendEmail")
    .timeBased()
    .onWeekDay(ScriptApp.WeekDay.MONDAY)
    .atHour(8)
    .nearMinute(20)
    .inTimezone("America/Phoenix")
    .create();
   // Deletes all existing triggers for the current project
   ScriptApp.getProjectTriggers().forEach(trigger => ScriptApp.deleteTrigger(trigger));
  

     }


  


Ramon Corrales

Workforce Manager

Experience Center & Learning Space Services

University Technology Office

Arizona State University

Mail Code: 6504

1150 E University Dr. Suite 115

Tempe, AZ 85281

p: 855-278-5080 

email: ramon.c...@asu.edu

web: uto.asu.edu

 linkedin 


ASU #1 in the U.S. for innovation

 



CBMServices Web

unread,
Apr 7, 2023, 7:23:53 PM4/7/23
to google-apps-sc...@googlegroups.com
Well they create trigger looks good. But right after that you have delete all project triggers. So whatever trigger you created, you delete right after.

Remove the delete that you have at end.

--
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/BY3PR06MB816162D516637C42D5776D6081969%40BY3PR06MB8161.namprd06.prod.outlook.com.

Ramon Corrales

unread,
Apr 7, 2023, 8:00:41 PM4/7/23
to Google Apps Script Community
Thank you for your suggestion.

I deleted the line of code you suggested and ran the code again.  Unfortunately, it did not send emai.

Here is the entire code, perhaps this will help.

again, when I run the function sendEmail, by itself .. the code work perfectly and sends the table to an email, as it is 
designed to do so.

When I add the scheduled trigger....... its does not work

here is the entire code.  
Thanks again for your asistance


function sendEmail() {
   const ss  = SpreadsheetApp.getActiveSpreadsheet();
   const ws = ss.getSheetByName("Gaby"); //  Tab Name 
   const lr = ws.getLastRow();
   const tableRangeValues = ws.getRange(41lr - 110)
    .getDisplayValues()
    .filter(row => row.join("").trim() !== ""); // remove empty rows

  const h1 = ws.getRange("A1:B2").getDisplayValue(); // Today's attendance & date
  const subheader = ws.getRange("a3:l3").getValues(); // Header    
  const Name = subheader[0][0];
  const CalledOutSick = subheader[0][1];
  const CalledOutPersonal = subheader[0][2];
  const EarlyOut = subheader[0][3];
  const LateNoCall = subheader[0][4];
  const LateCalled = subheader[0][5];
  const FMLAAbsent = subheader[0][6];
  const Total = subheader[0][7];

  
  
   
  const footer = ws.getRange("A25").getDisplayValue(); //      statement------------      This is not relevent Paragraph <> adds this to email
  const subfooter = ws.getRange("A24").getDisplayValue();  // WFM Thank you       This is not relevent Paragraph <> adds this to email
     
  const htmlTemplate = HtmlService.createTemplateFromFile("AttendTable");  // HTML file
  
  htmlTemplate.h1 = h1;
  htmlTemplate.subheader = subheader;
  htmlTemplate.Name = Name;
  htmlTemplate.CalledOutSick = CalledOutSick;
  htmlTemplate.CalledOutPersonal = CalledOutPersonal;
  htmlTemplate.EarlyOut = EarlyOut;
  htmlTemplate.LateNoCall = LateNoCall;
  htmlTemplate.LateCalled = LateCalled;
  htmlTemplate.FMLAAbsent = FMLAAbsent;
  htmlTemplate.Total = Total;
  htmlTemplate.footer = footer;
  htmlTemplate.subfooter = subfooter;
  htmlTemplate.tableRangeValues = tableRangeValues;

  const htmlForEmail = htmlTemplate.evaluate().getContent();
  
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var individualSheet = spreadsheet.getActiveSheet();
  
  var toEmail = individualSheet.getRange(1,10).getValue();
  var subject = individualSheet.getRange(1,1).getValue();
  var ccEmail = individualSheet.getRange(1,19).getValue();
  Logger.log(sendEmail)

  // send email
  GmailApp.sendEmail(toEmailsubject"", { htmlBodyhtmlForEmail });

  }
   function setTrigger() {
   ScriptApp.getProjectTriggers().forEach(trigger => ScriptApp.deleteTrigger(trigger));
   //Deletes all existing triggers for the current project

   
  

   // Sets a new trigger to run the sendemail function every Friday at 4:54 pm
   
   ScriptApp.newTrigger("sendEmail")
    .timeBased()
    .onWeekDay(ScriptApp.WeekDay.FRIDAY)
    .atHour(16)
    .nearMinute(54)
    .inTimezone("America/Phoenix")
    .create();
   // Deletes all existing triggers for the current project
   ScriptApp.getProjectTriggers().forEach(trigger => ScriptApp.deleteTrigger(trigger));


     }


  
 



Ramon Corrales

Workforce Manager

Experience Center & Learning Space Services

University Technology Office

Arizona State University

Mail Code: 6504

1150 E University Dr. Suite 115

Tempe, AZ 85281

p: 855-278-5080 

email: ramon.c...@asu.edu

web: uto.asu.edu

 linkedin 


ASU #1 in the U.S. for innovation

 




From: google-apps-sc...@googlegroups.com <google-apps-sc...@googlegroups.com> on behalf of Ramon Corrales <Ramon.C...@asu.edu>
Sent: Friday, April 7, 2023 4:02 PM
To: Google Apps Script Community <google-apps-sc...@googlegroups.com>
Subject: [Apps-Script] Help with a Schedule Trigger Code
 

CBMServices Web

unread,
Apr 7, 2023, 8:08:46 PM4/7/23
to google-apps-sc...@googlegroups.com
Using code to do the trigger is great if you plan on changing the trigger programmatically. If this is just a one time thing, then your best option is just to configure the trigger manually. This will make sure the code is authorized correctly before it runs as well.

Put your cursor on left side of the editor screen, trigger icon will pop open, then go to trigger screen, click the + sign on bottom right of screen and fill out details on trigger.


CBMServices Web

unread,
Apr 7, 2023, 8:11:10 PM4/7/23
to google-apps-sc...@googlegroups.com
Also, do remember that triggers are not guaranteed to run on the minute you schedule them. They will run within an hour of the scheduled time..

Peter Berkhout

unread,
Jul 17, 2023, 2:30:56 AM7/17/23
to Google Apps Script Community
Have you tried using opening the spreadsheet by id?
const ss = SpreadsheetApp.openById("yourspreadsheetIDhere");


Op zaterdag 8 april 2023 om 02:00:41 UTC+2 schreef jcor...@asu.edu:
Reply all
Reply to author
Forward
0 new messages