0

I think could be a simple one to solve, I am stuck working this simple issue out.

I have called a createEvent function to create a google calendar event. As part of this function I also get the google calendar event ID as EventId and want to return it.

For some reason I dont quite understand, the EventId value will not return.

  var EventId;

  createEvent(calendarId,title,startDt,endDt,desc,EventId);

  Logger.log('id after func = '+EventId);

   sheet.getRange(lr,EventIdHolder,1,1).setValue(EventId);
};

function createEvent(calendarId,title,startDt,endDt,desc,EventId) {
  var cal = CalendarApp.getCalendarById(calendarId);
  var start = new Date(startDt);
  var end = new Date(endDt);
//Manually set the Location, this can be modified to be dynamic by modifying the code if need be
  //var loc = sheet.getRange(lr,destId,1,1).getValue();
  var loc = "Some Location"
//Set the Options, in this case we are only using Description and Location, as we do not need Guests or sendInvites
  var event = cal.createEvent(title, start, end, {
      description : desc,
      location : loc
  });
   Logger.log('event = '+event);

   var EventId = event.getId();

   Logger.log('id generated = '+EventId);

   return EventId;

};
  • What do the `log`s output? – blex Jun 19 '15 at 17:45
  • [15-06-20 03:53:40:336 AEST] id generated = u32ce2tt81uvlgu0t7nf2ii1ho@google.com [15-06-20 03:53:40:336 AEST] id after func = undefined – JJ_Australia Jun 19 '15 at 17:58
  • Something's not right about the start of the code snippet you've posted - please double check your actual code and update the snippet. – Mogsdad Jun 19 '15 at 19:07

1 Answers1

1

Even if I'm not A JavaScript expert, I'd find it more logical to write it like that (assigning a value directly to the EventId variable) :

  ...
  var EventId = createEvent(calendarId,title,startDt,endDt,desc);
  Logger.log('id after func = '+EventId);
  sheet.getRange(lr,EventIdHolder,1,1).setValue(EventId);
}

function createEvent(calendarId,title,startDt,endDt,desc) {
  var cal = CalendarApp.getCalendarById(calendarId);
  var start = new Date(startDt);
  var end = new Date(endDt);
  //Manually set the Location, this can be modified to be dynamic by modifying the code if need be
  //var loc = sheet.getRange(lr,destId,1,1).getValue();
  var loc = "Some Location"
  //Set the Options, in this case we are only using Description and Location, as we do not need Guests or sendInvites
  var event = cal.createEvent(title, start, end, {
    description : desc,
    location : loc
  });
  Logger.log('event = '+event);
  var EventId = event.getId();
  Logger.log('id generated = '+EventId);
  return EventId; 
}

note : I agree with Mogsdad comment below about semicolumns not being mandatory... Anyway, the code was working without it ;-)

Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • 1
    You beat me to it, Serge. There is no pass-by-reference for strings in Javascript. Refer to [Does JavaScript pass by reference?](http://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference). – Mogsdad Jun 19 '15 at 19:11
  • Hey ! sorry ;-D, but feel free to give a better explanation, I'll certainly read it with great interest ! – Serge insas Jun 19 '15 at 19:15
  • All that was missing in yours was the "why" - that link explains it. Now your answer is perfect! – Mogsdad Jun 19 '15 at 19:17
  • Just read the post you refer to, it is indeed very clear (a 137 score is not bad at all!), now I know "why" too ! thanks. – Serge insas Jun 19 '15 at 19:21
  • Thank you, I am very new to js and this helps me to understand .Awesome...yes code all works now. – JJ_Australia Jun 20 '15 at 02:27