简体   繁体   English

Google脚本:返回NaN的Javascript日期调用

[英]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. 我在同一Google Scripts项目中的两个独立函数中包含相同的代码。 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. 但是,在另一个函数中,它返回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. 编辑:功能1不起作用。 Function 2 works. 功能2起作用。

Can anyone point out any mistakes or provide some guidance? 谁能指出任何错误或提供一些指导? I'm using Chrome and the same issue occurs in FF. 我使用的是Chrome,FF中也发生了同样的问题。

Edit 2: Thanks for giving this some thought. 编辑2:感谢您的思考。 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. 同样,我遇到的问题是,尽管使用了完全相同的数据数组,但var pmsDate在函数2中正确返回了DateString,但在函数1中却返回了NaN。 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 功能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 ? 您是否尝试从1而不是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? 为什么在Date函数中有这么多参数? I thought there could be only 3 separated by comma. 我以为只能用逗号分隔3个。 Did you check the startDate is a date? 您是否已检查startDate是一个日期?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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