简体   繁体   English

在电子表格中使用Google脚本在日期中添加天数

[英]Add days to a date with google-scripts in spreadsheet

I want to add 7 days to a specific cell (which contains a date) when using a script in spreadsheets 在电子表格中使用脚本时,我想在特定单元格(包含日期)中添加7天

So far I have this: 到目前为止,我有这个:

 var Date = sheet.getRange("A3");
 var Day = [Date.getCell(1, 1).getValue()];
    Day = Number(Day)+7;
    Date.setValue(Day);

but it doesn't add 7 days and I keep getting really long GMT dates. 但这并没有增加7天,而且我的GMT日期仍然很长。 My format needs to be like: 21-06-2016 (european style) 我的格式必须是:21-06-2016(欧洲风格)

Took me a while, but here it is: 花了我一段时间,但在这里是:

In this case, cell A3 is the date which needs to change a week (7days) after using google-scripts. 在这种情况下,单元格A3是使用Google脚本后需要更改一周(7天)的日期。

Take a different cell and give it the same date for you to start with (for example take A1 ). 改用其他单元格,并为其指定相同的开始日期(例如,采用A1 )。 Now take cell A2 and give it the function =A1 Change this cells layout from a date layout to a normal tekst. 现在,使用单元格A2并赋予其功能=A1将此单元格布局从日期布局更改为普通tekst。 Now the date will change in a number. 现在,日期将更改为一个数字。 For instance: 26-12-2016 ( cell A1 ) will become 42730 ( cell A2 ). 例如:26-12-2016( 单元格A1 )将变为42730( 单元格A2 )。

Now go to cell A3 and give it the function =A2 and give it the layout of a date (in this case: 26-12-2016) 现在转到单元格A3并为其提供函数=A2并为其提供日期的布局(在这种情况下:2016年12月26日)

In Script: use this 在脚本中:使用此

var Date = sheet.getRange("A2");  
var Day = [Date.getCell(1,1). getValue()];]

Day = Number(Day)+7; //change "7" for the amount of days you want
Date.setValue(Day);

This script will add the amount of days you want to cell A2 and cell A3 will change accondingly. 此脚本将为单元格A2添加所需的天数,并且单元格A3将相应地更改。

After you used the scripts once, there is no need for the use of cell A1 anymore because cell A2 has changed after using the script. 一次使用脚本后,就不再需要使用单元格A1 ,因为使用脚本后单元格A2已更改。 You can clear cell A1 if you like. 您可以根据需要清除单元格A1

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

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