简体   繁体   中英

Google Scripts: Javascript Date call returning NaN

Thanks all for your attention to this.

I have an identical piece of code contained in two separate functions within the same Google Scripts project. The code is just a basic loop to match a given date and then use the index to get a value from a spreadsheet. The specific line in question is:

var pmsDate = new Date(data[j][dateColIndex]).setHours(0, 0, 0, 0);

The code works fine in one function; in the other function, however, it returns NaN. I literally copied & pasted the loop structure from one function to the other, so I don't understand why it does not work. I have tried every possible manipulation I can think of but nothing seems to correct the problem.

Edit: Function 1 does NOT work. Function 2 works.

Can anyone point out any mistakes or provide some guidance? I'm using Chrome and the same issue occurs in FF.

Edit 2: Thanks for giving this some thought. As requested I've posted the code for those functions (they are not very long so I posted the whole thing). The "Data" sheet referenced herein is just a spreadsheet with dates and some corresponding data. Again, the issue I'm having is that var pmsDate correctly returns a DateString in Function 2 but returns NaN in Function 1 despite using the exact same data array. Appreciate any help!

Function 1:

function getMonthlyRooms(month) {

  var forecastMonth = getMonthDigit(month);
  var monthDays = daysInMonth(month);
  var year = getYear();

  var startDate = new Date(year, forecastMonth, 1, 0, 0, 0, 0);

  var sheetData = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
  var sheetDataCols = sheetData.getDataRange().getNumColumns();
  var sheetDataRows = sheetData.getDataRange().getNumRows();


  // find column header indexes

  var dataHeaders = sheetData.getRange(1, 1, 1, sheetDataCols).getValues();

  for (i = 0; i < sheetDataCols; i++) {
    if (dataHeaders[0][i] == "CONSIDERED_DATE") {
      var dateColIndex = i;
    }
    if (dataHeaders[0][i] == "NO_ROOMS") {
      var roomsColIndex = i;
    }
  }


  // find what row the month begins

  var data = sheetData.getDataRange().getValues();

  for (j = 0; j < sheetDataRows; j++) {
    var pmsDate = new Date(data[j][dateColIndex]).setHours(0, 0, 0, 0);

    if (pmsDate == startDate) {
      var startRow = j + 1;
    }
  }


  // loop through range and sum

  var monthData = sheetData.getRange(startRow, (roomsColIndex + 1), monthDays, 1).getValues();

  var occRooms = 0;

  for (var k in monthData) {
    occRooms += monthData[k][0];
  }

  return occRooms;  

}

Function 2

function getDailyRooms(date) {

  var forecastDate = new Date(date).setHours(0, 0, 0, 0);

  var sheetData = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
  var sheetDataCols = sheetData.getDataRange().getNumColumns();
  var sheetDataRows = sheetData.getDataRange().getNumRows();


  // find column header indexes

  var dataHeaders = sheetData.getRange(1, 1, 1, sheetDataCols).getValues();

  for (i = 0; i < sheetDataCols; i++) {
    if (dataHeaders[0][i] == "CONSIDERED_DATE") {
      var dateColIndex = i;
    }
    if (dataHeaders[0][i] == "NO_ROOMS") {
      var roomsColIndex = i;
    }
  }


  // loop through data

  var data = sheetData.getDataRange().getValues();

  for (j = 0; j < sheetDataRows; j++) {
    var pmsDate = new Date(data[j][dateColIndex]).setHours(0, 0, 0, 0);

    if (pmsDate == forecastDate) {
      var occRooms = sheetData.getRange(j + 1, roomsColIndex + 1).getValue();
    }
  }

  return occRooms;

}

Have you tried to begin your loop with 1 instead of 0 ? Doing so would allow skipping headers values (just an idea)

My guess is that there is a difference in the data or logic that the functions use, resulting in an edge case you are not handling. One way to simplify the debugging would be to put shared code within a single function, instead of having two copies. This would make it easier to see the data going in and the data coming out.

Why so many arguments in Date function? I thought there could be only 3 separated by comma. Did you check the startDate is a date?

var startDate = new Date(year, forecastMonth, 1, 0, 0, 0, 0);

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