简体   繁体   中英

google apps script - createEvent error indicating NaN

Can anyone assist in why I'm getting a "NaN" for dates in the code below? I'm trying to create a calendar event but it keeps giving indicating "NaN" in the Logger.log. Execution transcript shows the following;

"Execution failed: Cannot find method createEvent(string,number,number). (line 38, file "Code")"

This is part of the code. The script runs based on a form submit of two dates. The script grabs the values from a spreadsheet. For example the two dates could be 4/1/2014 and 4/2/2014.

Here is the code:

        var ptoCalendar = CalendarApp.getCalendarById("ENTER CALENDAR ID")

    var startDate = Utilities.formatDate(new Date(sheet.getRange(i,j-3,1,1).getValue()), 'GMT', "yyyy-MM-dd'T'HH:mm:ss");
    var cnvtStartDate = new Date(startDate).setHours(08,00,00);
    Logger.log(cnvtStartDate);

    var endDate = Utilities.formatDate(new Date(sheet.getRange(i,j-2,1,1).getValue()), 'GMT', "yyyy-MM-dd'T'HH:mm:ss");
    var cnvtEndDate = new Date(endDate).setHours(17,00,00);
    Logger.log(cnvtEndDate);        

    var createEvent = ptoCalendar.createEvent(recipientName+'- PTO', cnvtStartDate, cnvtEndDate);

Any assistance is appreciated..

UPDATED CODE PRIOR TO Serge Response - My solution at the moment. NOTE: I'm providing the full function here, were as before was just a snippet

//This takes the date string from managersDecission() and covnerts back to a date type
function parseDate(dateString){
var time = Date.parse(dateString);
if(!time){
    time = Date.parse(dateString.replace("'T'"," "));
    if(!time){
        bound = dateString.indexOf('T');
        var dateData = dateString.slice(0, bound).split('-');
        var timeData = dateString.slice(bound+1, -1).split(':');

        time = Date.UTC(dateData[0],dateData[1]-1,dateData[2],timeData[0],timeData[1],timeData[2]);
    }
}
return new Date(time);
}

//Used to update the employee requested PTO in the request spreadsheet with managers decision
function managersDecission() {
//Section get the tracking spreadsheet and its values & requests the decision made by manager in grabRequestID()  
  var ss = SpreadsheetApp.openById(trackingSS)
  var sheet = ss.getSheetByName('Requests');
  var data = sheet.getDataRange().getValues();
  var decision = grabRequestID();

//for loop to set the decision of the manager for tracking  
  for (var i=0; i < data.length; i++) {
    for (var j=0; j < data[i].length; j++) {      
      if (data[i][j] == decision[0] && decision[1] == "Approved") {
    sheet.getRange(i+1,j+3,1,1).setValue(decision[1]);
    var recipientEmail = sheet.getRange(i+1,j,1,1).getValue();
    var recipientName = sheet.getRange(i+1,j-1,1,1).getValue();

//This section creates the calendar event for approved PTO        
    var ptoCalendar = CalendarApp.getCalendarById('CALENDAR ID')        
    var startDate = Utilities.formatDate(new Date(sheet.getRange(i+1,j-3,1,1).getValue()), 'GMT', "yyyy-MM-dd'T'HH:MM:SS");
//Debug Line
//Logger.log("start date is "+startDate)  
    var cnvtStartDate = parseDate(startDate);
//Debug Line
//Logger.log("parsed start date "+cnvtStartDate);
    var setHourStartDate = cnvtStartDate.setHours(08,00,00,00);
//Debug Line
//Logger.log("start Hours set "+setHourStartDate);        
    var endDate = Utilities.formatDate(new Date(sheet.getRange(i+1,j-2,1,1).getValue()), 'GMT', "yyyy-MM-dd'T'HH:MM:SS");
//Debug Line
//Logger.log("end date is "+endDate);
    var cnvtEndDate = parseDate(endDate);
//Debug Line
//Logger.log("parse end date "+cnvtEndDate);
    var setHourEndDate = cnvtEndDate.setHours(15,00,00,00);
//Debug Line
//Logger.log("end Hours set "+setHourEndDate);        
    var createEvent = ptoCalendar.createEvent(recipientName+'- PTO', cnvtStartDate, cnvtEndDate);

ADDING LOG INFO FOR SERGE'S BELOW COMMENT

[14-03-20 16:08:05:541 PDT] start date is 2014-03-31T07:03:00
[14-03-20 16:08:05:545 PDT] parsed start date Mon Mar 31 2014 00:03:00 GMT-0700 (PDT)
[14-03-20 16:08:05:545 PDT] start Hours set 1396278000000
[14-03-20 16:08:05:546 PDT] end date is 2014-04-02T07:04:00
[14-03-20 16:08:05:547 PDT] parse end date Wed Apr 02 2014 00:04:00 GMT-0700 (PDT)
[14-03-20 16:08:05:547 PDT] end Hours set 1396476000000

UPDATED DID SOME TESTING BASED ON Serge comment indicating cnvtStartDate was a date object

I removed the Utilities.formatDate and in fact the value pulled from spreadsheet is a date value and will allow script to run successfully. Issue was that is creates the calendar event for 12:00AM. Since this should block a full work day out (PTO day), I need it to set some times for start /end.

When running the script now and using the following:

var startDate =new Date(sheet.getRange(i+1,j-3,1,1).getValue());
var setHourStartDate = startDate.setHours(08,00,00,00);
var endDate = new Date(sheet.getRange(i+1,j-2,1,1).getValue());
var setHourEndDate = cnvtEndDate.setHours(15,00,00,00);
var createEvent = ptoCalendar.createEvent(recipientName+'- PTO', setHourStartDate, setHourEndDate);

The following error is giving. It thinks the ssetHourStartDate and end is a number??

"Execution failed: Cannot find method createEvent(string,number,number)."

I then adjusted he code to the following:

    var startDate =new Date(sheet.getRange(i+1,j-3,1,1).getValue());
    var setHourStartDate = new Date(startDate.setHours(08,00,00,00));
    var endDate = new Date(sheet.getRange(i+1,j-2,1,1).getValue());
    var setHourEndDate = new Date(endDate.setHours(17,00,00,00));   

    var createEvent = ptoCalendar.createEvent(recipientName+'- PTO', setHourStartDate, setHourEndDate);

And all is working well. It will create single day event for the full day or span multiple days across the top of calendar.

Thanks Serge as always for pointing thing out ..

why do you use Utilities.formatDate in the first place ?

If the cell in the sheet is a date you don't need to convert it to a string, just use it as it is.

If you really want to use it that way then use the correct syntax like below :

  var cnvtStartDate = new Date("2014/03/20"):// I replaced with the string "as it should be"
  cnvtStartDate.setHours(8,0,0,0);
  Logger.log(cnvtStartDate);// this is a date object

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM