【问题标题】:Error while pushing Dates to Array in Apps Script在 Apps 脚本中将日期推送到数组时出错
【发布时间】:2021-11-05 04:09:38
【问题描述】:

这就是函数(我感觉几个小时前我做了同样的事情,结果是我所期望的)

function errorFunction() {
  let master = []

  let a = [1,2] //a simple array to loop over

  a.forEach(function(b) {
    let theDate = Date.today() // 8th Sep 2021
    let i = 0
    while (i<2) {
      theDate = Date.parse(theDate).addDays(1)
      Logger.log(theDate) // dates are alternating between 9th and 10th september
      master.push([b,i,theDate])

      i = i+1
    }

  })
  
  Logger.log(master) // all dates are 10th september
}

代码将 9 月 9 日和 9 月 10 日推送到数组。但是数组的输出只有9月10日。

我的期望:

[
  [1.0, 0.0, Fri Sep 9 00:00:00 GMT+05:30 2021],
  [1.0, 1.0, Fri Sep 10 00:00:00 GMT+05:30 2021],
  [2.0, 0.0, Fri Sep 9 00:00:00 GMT+05:30 2021],
  [2.0, 1.0, Fri Sep 10 00:00:00 GMT+05:30 2021]
]

我得到的输出:

[
  [1.0, 0.0, Fri Sep 10 00:00:00 GMT+05:30 2021],
  [1.0, 1.0, Fri Sep 10 00:00:00 GMT+05:30 2021],
  [2.0, 0.0, Fri Sep 10 00:00:00 GMT+05:30 2021],
  [2.0, 1.0, Fri Sep 10 00:00:00 GMT+05:30 2021]
]

我正在使用最新版本的 DateJs 库。

【问题讨论】:

  • 请提供更多详细信息。 ab 是什么?什么是Date.today(),9 月 9 日或 10 日?
  • @DmitryKostyuk 我已经更新了问题。

标签: javascript google-apps-script datejs


【解决方案1】:

如果你将Date对象传递给DateJSparse()方法,它会返回对象(link),addDate()方法也不会创建一个新的对象 (link)。因此,当您为每个b 值创建一个新的theDate 对象时,您修改了while 循环中的same 对象,并将对它的引用放入master 数组中。 p>

解决方法是每次修改theDate时创建一个新的Date对象。例如,改变这一行:

theDate = Date.parse(theDate).addDays(1)

...到这个:

theDate = new Date(theDate.valueOf()).addDays(1)

...或者这个(DateJS 定义了一个clone() 方法):

theDate = theDate.clone().addDays(1)

这将在添加 1 天之前克隆 theDate 对象。

【讨论】:

  • 这似乎没有克隆日期。我试过这样做,但它不起作用。
  • 修正了解释和代码。我尝试了https://script.google.com/ 中的代码,它对我有用(我得到“9、10、9、10”)。
  • 其实clone()返回的new Date(this.getTime())new Date(this.valueOf())是一样的,但是使用clone()反而让代码更具可读性。
【解决方案2】:

不知道你为什么需要那个库,这个 sn-p 可以完成这项工作:

const errorFunction = () => {
  const days = [1, 2];
  const i = [0, 1];
  const today = new Date();
  const addDays = (date, numDays = 1) =>
    new Date(new Date().setDate(date.getDate() + numDays));
  return days.reduce((acc, day) => {
    return [...acc, ...i.map(item => [day, item, addDays(today, day)])];
  }, []);
};

console.log(errorFunction());

如果这是您所期望的,请告诉我。

【讨论】:

    【解决方案3】:

    正如@kol 指出的那样,我将引用放入数组中。

    所以我没有参考,而是这样做了:

    function errorFunction() {
      let master = []
    
      let a = [1,2] //a simple array to loop over
    
      a.forEach(function(b) {
        let theDate = Date.today() // 8th Sep 2021
        let i = 0
        while (i<2) {
          theDate = Date.parse(theDate).addDays(1)
          Logger.log(theDate) // dates are alternating between 9th and 10th september
          master.push([b,i,new Date(theDate)])
    
          i = i+1
        }
    
      })
      
      Logger.log(master) // all dates are 10th september
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      相关资源
      最近更新 更多