Question

i am trying to have one function which i run on a timer call another function that requires parameters, my code is a very slightly modified version of this tutorial i have removed the sms call and the following code works

function testFunction() {
   var label = GmailApp.getUserLabelByName('GApps/Test');
   var emails = label.getThreads();
   var now = new Date().getTime();
   for(i in emails){
      CalendarApp.createEvent('You got an Email!', new Date(now+60000), new Date(now+60000));
   }
}

I have set up a filter in gmail that applies the sub-label test (from the GApps) label family on any email i get from a certain email address, when i get an email from this address and it gets the label test the above code adds an entry to my google calendar, with the event name You got and Email! that both begins and ends one minute from the current time

function gmail2cal(labLoc, event) {
   var label = GmailApp.getUserLabelByName(labLoc);
   var emails = label.getThreads();
   var now = new Date().getTime();
   for(i in emails){
      CalendarApp.createEvent(event,  new Date(now+60000), new Date(now+60000));
   }
}

Now here is the same code, with the strings substituted for variables, providing this function is called with two parameters that with the same values as the previous block of code it should function identically

function launcher(){
   gmail2cal('GApps/Test','You got an Email!');
}

But when i use the run function button in the Google apps script editor, the function gmail2cal doesn't work and i don't understand why

Was it helpful?

Solution

The Run command from the Google Apps Script editor doesn't pass any parameters into your function. So if you execute the function gmail2cal it is called without any paramters, which cause both labLoc and event to evaluate to undefined. Probably GmailApp.getUserLabelByName(undefined); returns nothing and thus your function doesn't do anything.

In this case you probably want to execute the function launcher from the Run menu, that function will execute gmail2cal with the 2 parameters it needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top