【问题标题】:Date variable not updating in Google Sheets App Scripts日期变量未在 Google 表格应用脚本中更新
【发布时间】:2018-02-01 15:43:58
【问题描述】:

所以我使用谷歌表格电子表格来跟踪我一个月内的所有支出。我创建了一个函数来输出当月当天我花了多少钱(即今天它会显示每个月第一天的支出)。它包含消费日期和购买金额的列。

function spentByThisDay(date, spent) {
  var d = new Date();
  var currentDate = d.getDate();
  var currDate;
  var sum =0;
  for(var i=0; i<date.length; i++){
    currDate = new Date(date[i]);
    if(currDate.getDate()<=currentDate)
      sum+=parseFloat(spent[i]);
  }
  return sum;
}

这个功能很好用。我的问题是它没有更新我过去几个月表上的当前日期(今天,第一天)。我必须手动进入并删除调用函数的单元格

=spentByThisDay(A2:A100,D2:D100)

并将其重新粘贴到单元格中以使其更新。

是否有任何解决方法可以让此输出每天或每次打开电子表格时更新?

【问题讨论】:

标签: google-apps-script google-sheets


【解决方案1】:

我是这样做的,它似乎工作正常。

function spentByThisDay(date, spent) {
  var currentDate=new Date();
  var sum =0;
  for(var i=0; i<date.length; i++){
    var currDate = new Date(date[i]);
    if(currDate.valueOf()<=currentDate.valueOf()){
      sum+=parseFloat(spent[i]);
    }
  }
  return sum;
}  

【讨论】:

  • 函数本身没有问题,是工作表没有更新的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多