简体   繁体   English

通过 Google App Script / Javascript 从电子表格中添加 1 天 - 月份保持重置为当前月份

[英]Add 1 day to date from spreadsheet via Google App Script / Javascript- Month Keeps Reseting to current month

I am trying to set up a Google App Script function that grabs a date (formatted dd/mm/yy) from the last column of a spread, and creates a new column with the date + one day.我正在尝试设置一个谷歌应用程序脚本 function 从价差的最后一列中获取日期(格式为 dd/mm/yy),并创建一个包含日期 + 一天的新列。

I have seen previous solutions and tried to use the same, ie newDate.setDate(lastDate.getDate()+1) but have had issues getting the value formatted correctly in the script.我已经看过以前的解决方案并尝试使用相同的解决方案,即newDate.setDate(lastDate.getDate()+1)但在脚本中正确格式化值时遇到了问题。 This is a variation of my code that I'm using to loop through for a year's worth of values to see what I get:这是我的代码的一个变体,我用它来循环一年的值,看看我得到了什么:

for (var i=0;i<365;i++){

    var lastRow = outputSheet.getLastRow();
    var newDate = new Date();

    var lastDate = outputSheet.getRange(lastRow,1).getValue();
    var newDateRng = outputSheet.getRange(lastRow+1,1);
    Logger.log(lastDate + 1, typeof lastDate, typeof (lastDate + 1));
    newDate.setDate(lastDate.getDate());
    Logger.log(newDate);
    newDate.setDate((newDate.getDate() + 1));
    Logger.log(newDate);
    var newDateFormatted = Utilities.formatDate(newDate, ss.getSpreadsheetTimeZone(), "dd/MM/YY");
    Logger.log(newDateFormatted);
    newDateRng.setValue(newDateFormatted);
}

With a start date of "01/03/2020", I get the following behaviour:开始日期为“01/03/2020”,我得到以下行为:

01/03/2020 2020 年 1 月 3 日

02/05/2020 2020 年 2 月 5 日

03/05/2020 2020 年 3 月 5 日

... ...

31/05/2020 2020 年 5 月 31 日

01/06/2020 2020 年 1 月 6 日

02/05/2020 2020 年 2 月 5 日

03/05/2020 2020 年 3 月 5 日

... ...

31/05/2020 2020 年 5 月 31 日

01/06/2020 2020 年 1 月 6 日

02/05/2020 2020 年 2 月 5 日

... ...

etc. All the way through the year.等。一直到一年。 Although the day increase, the month seems to reset after the first day of the month.虽然天数增加了,但该月似乎在该月的第一天之后重置。

As a note, I am specifically looking to pick the date off of the spreadsheet rather than using new Date as today and new Date +1 as tomorrow.作为说明,我特别希望从电子表格中选择日期,而不是将新日期用作今天,将新日期 +1 用作明天。

Thanks谢谢

You need to use a different variable in the loop otherwise you will always return to the same month.您需要在循环中使用不同的变量,否则您将始终返回同一个月。

Also avoid using strings for the result, keep date objects and display it properly.还要避免使用字符串作为结果,保留日期对象并正确显示。

The code goes like this:代码如下:

function otherTest(){
  var lastDate = SpreadsheetApp.getActiveSheet().getActiveCell().getValue();
  var date = new Date(lastDate); // create new date object
  var result = [];
  for (var i=0;i<365;i++){
    date=new Date(date).setDate(new Date(date).getDate()+1)
    Logger.log('date='+new Date(date))
    result.push([new Date(date)]);
  } 
  SpreadsheetApp.getActiveSheet().getRange(1,2,result.length,1).setValues(result).setNumberFormat('dd/MM/yyyy');
}

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

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