Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么Google Apps脚本会将日期值结果缩短1天?_Javascript_Google Apps Script_Google Sheets - Fatal编程技术网

Javascript 为什么Google Apps脚本会将日期值结果缩短1天?

Javascript 为什么Google Apps脚本会将日期值结果缩短1天?,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,目标: 我使用下面的代码简单地测试我是否能够提取正确的日期和天数 函数myFunction(){ const spreadSheet=SpreadsheetApp.getActiveSpreadsheet(); const getDate=新日期(电子表格.getRange('B4').getValue()); 常量getDays=(新日期(getDate.getFullYear(),getDate.getMonth()+1,0)).getDate(); Logger.log(getDate)

目标: 我使用下面的代码简单地测试我是否能够提取正确的日期和天数

函数myFunction(){
const spreadSheet=SpreadsheetApp.getActiveSpreadsheet();
const getDate=新日期(电子表格.getRange('B4').getValue());
常量getDays=(新日期(getDate.getFullYear(),getDate.getMonth()+1,0)).getDate();
Logger.log(getDate);
Logger.log(getDays);
}
问题(更新): 主要问题很可能与脚本编辑器中显示的时区和电子表格文件的时区之间的差异有关。您可以更改/检查两者以确保它们相同

代码中的另一个问题与此和此行相关:

new Date(getDate.getFullYear(), getDate.getMonth() + 1, 0)
  • 您已将
    0
    指定为
    new Date()
    的第三个参数,该参数超出范围,这就是您获取上一个月(10月)的日期的原因
在同一行中,将
1
添加到单元格的当前月份。这将为您提供12月(假设您在第3个参数中修复了日索引)。正如你所看到的:

表示月份的整数值,从0开始表示一月到 12月11日

解决方案:

因此,总的来说,如果您解决了这两个问题,这一行应该是:

const getDays = (new Date(getDate.getFullYear(), getDate.getMonth(), 1)).getDate();
关于你的另一个问题:

我想知道为什么会发生这种情况,我想知道如何才能解决 根据B4单元格中的内容输入正确的日期

正如我在评论中解释的那样,如果您想要单元格中显示的确切格式,那么应该使用而不是

有关此问题原因的更多详细信息:

说明: 当我执行此代码时:

function myFunction() {
  const spreadSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Purchases (Beta)');
  const getDate = new Date(spreadSheet.getRange('B4').getValue());
  const getDate2 = spreadSheet.getRange('B4').getDisplayValue();
  const getDays = (new Date(getDate.getFullYear(), getDate.getMonth(), 1)).getDate();
  Logger.log(getDate);
  Logger.log(getDate2);
  Logger.log(getDays);
}
我得到这些日志:

B4单元格中的日期为:
2020-11-01
,因此,除上述问题外,我认为一切正常。

问题(更新): 主要问题很可能与脚本编辑器中显示的时区和电子表格文件的时区之间的差异有关。您可以更改/检查两者以确保它们相同

代码中的另一个问题与此和此行相关:

new Date(getDate.getFullYear(), getDate.getMonth() + 1, 0)
  • 您已将
    0
    指定为
    new Date()
    的第三个参数,该参数超出范围,这就是您获取上一个月(10月)的日期的原因
在同一行中,将
1
添加到单元格的当前月份。这将为您提供12月(假设您在第3个参数中修复了日索引)。正如你所看到的:

表示月份的整数值,从0开始表示一月到 12月11日

解决方案:

因此,总的来说,如果您解决了这两个问题,这一行应该是:

const getDays = (new Date(getDate.getFullYear(), getDate.getMonth(), 1)).getDate();
关于你的另一个问题:

我想知道为什么会发生这种情况,我想知道如何才能解决 根据B4单元格中的内容输入正确的日期

正如我在评论中解释的那样,如果您想要单元格中显示的确切格式,那么应该使用而不是

有关此问题原因的更多详细信息:

说明: 当我执行此代码时:

function myFunction() {
  const spreadSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Purchases (Beta)');
  const getDate = new Date(spreadSheet.getRange('B4').getValue());
  const getDate2 = spreadSheet.getRange('B4').getDisplayValue();
  const getDays = (new Date(getDate.getFullYear(), getDate.getMonth(), 1)).getDate();
  Logger.log(getDate);
  Logger.log(getDate2);
  Logger.log(getDays);
}
我得到这些日志:

单元格B4中的日期为:
2020-11-01
,因此,除了我上面描述的问题外,其他一切对我都很好。

修改点:
  • 当我看到从
    2020-11-01
    单元格值中检索到的
    Sat Oct 31 20:00:00 GMT-04:00 2020
    的值时,我认为在您的情况下,谷歌应用程序脚本项目的时区可能与谷歌电子表格的时区不同。为了检查这一点,请使用以下脚本

      console.log(Session.getScriptTimeZone())
      console.log(SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone())
    
    • 运行此脚本时,可以在日志中看到Google Apps脚本项目和Google电子表格的时区
    • 我认为从10月31日星期六20:00:00 GMT-2020年04:00的值来看,在你的情况下,谷歌应用程序脚本项目的时区可能是美国/纽约。关于这一点,当使用新IDE创建新的Google Apps脚本项目时,默认时区将变为
      America/new_York
      。我不确定这是一个bug还是当前的规范。
      • 此问题已报告给问题跟踪者
我认为,从上述情况来看,当时区改变时,您的问题可能会得到解决。那么下面的修改呢

修改流程: 首先,请从电子表格中检查您的时区。在当前阶段,当电子表格创建为新电子表格时,将设置时区

console.log(SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone())
下一步,请将您的时区复制并粘贴到Google Apps脚本项目。在这种情况下,请使用检索到的时区修改清单文件(
appsscript.json

“时区”:“####”,修改点:
  • 当我看到从
    2020-11-01
    单元格值中检索到的
    Sat Oct 31 20:00:00 GMT-04:00 2020
    的值时,我认为在您的情况下,谷歌应用程序脚本项目的时区可能与谷歌电子表格的时区不同。为了检查这一点,请使用以下脚本

      console.log(Session.getScriptTimeZone())
      console.log(SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone())
    
    • 运行此脚本时,可以在日志中看到Google Apps脚本项目和Google电子表格的时区
    • 我认为从10月31日星期六20:00:00 GMT-2020年04:00的值来看,在你的情况下,谷歌应用程序脚本项目的时区可能是美国/纽约。关于这一点,当使用新IDE创建新的Google Apps脚本项目时,默认时区将变为
      America/new_York
      。我不确定这是一个bug还是当前的规范。
      • 此问题已报告给问题跟踪者
我想从上面坐下来