0

Does anyone have an idea why this simple code does not work?:

var formattedSched = new Date(sched).setHours(15);

I am simply trying to add a time to a calendar event in google calendar through apps script. I am even using basically the same sample on the w3 page for set hours. http://www.w3schools.com/jsref/jsref_sethours.asp

But everytime I run the script to create an event in google calendar I get an error message that google thinks it is a number and not a date object. It works fine without the setHours() but with it, google thinks it's a number. Thanks

Update: Okay, I split out a test function to try and log what's happening:

function testsetcal() {

  var formattedSched = new Date(); Logger.log(formattedSched.setHours(15))

  CalendarApp.getDefaultCalendar()
  .createEvent('Commo', formattedSched, formattedSched);
}

and the Logger result without setHours is: Wed Nov 21 08:12:33 PST 2012

Logger result with setHours is: 1.35352881266E12 which looks nothing like the example result on the w3 page.

Update: still not working and getting same logger result with setHours separated from main variable: 1.35352881266E12

function testsetcal() {

  var formattedSched = new Date(); 
  var withHours = formattedSched.setHours(15);

  Logger.log(withHours);

  CalendarApp.getDefaultCalendar()
  .createEvent('Commo', withHours, withHours);
}

UPDATE: Okay wow - this is weird. I went to my google calendar to delete all the test events, at least just the ones I thought were successful. I then discovered that there were morte than I thought and some were set to 1500 - then I went back to the code and discovered that setHours() works on the outside of the google calendar create event like this:

function testsetcal() {  
  var formattedSched = new Date(); 

  formattedSched.setHours(15);

  Logger.log(formattedSched.setHours(15));

  CalendarApp.getDefaultCalendar()
  .createEvent('Commo', formattedSched, formattedSched);
}

So, this code set the hour to 1500, but that part of the code is to not be included in the creation of the event. Totally caught me surprised, but this seems to be the solution.

4
  • What is sched ? It appears that your Date object is not being properly constructed. I do not agree to the two answers that it is necessary to separate out the two statements.
    – Srik
    Nov 21, 2012 at 16:08
  • I just posted an updated simplified test code - getting weird results compared to the w3 page. I'd be surprised if separating from the variable matters, but gonna give it a shot and report back Nov 21, 2012 at 16:16
  • yeah, didn't work - trying all different kinds of formatting here and there and yet google still thinks the result is a number and not a date object - this should be so simple Nov 21, 2012 at 16:32
  • Figured it out - some of my code test were including the setHours within the create event. Seems I need to set the Hours outside the create event then just call the variable into the create event. Very weird compared to other ways to call variabales in GAS. Nov 21, 2012 at 16:46

3 Answers 3

0

Not quite, you are currently returning the setHours response to the var, try

var formattedSched = new Date(sched);
formattedSched.setHours(15);
0
0

You need to separate it out as in the example you referenced.

var formattedSched = new Date(sched);
formattedSched.setHours(15);

You are trying to implement the setHours() on a date object that has not been created yet.

0
0

Guess I should go ahead and close this out. Thanks for the replies. The code that works for me to get an event into GCal with a time:

function testsetcal() {  
  var formattedSched = new Date(); 

  formattedSched.setHours(15);

  Logger.log(formattedSched.setHours(15));

  CalendarApp.getDefaultCalendar()
  .createEvent('Commo', formattedSched, formattedSched);
}

The .setHours must be outside the .createEvent.

1
  • i can't checkoff my own answer for 2 days, fyi, for some reason Nov 21, 2012 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.