How do I fill up a range in a Google Spreadsheet based on a startDate that has been set by an inputBox? Basically, (1) User inputs startDate; (2) script fills up cells A1 to A13 starting from startDate and adding 7 days for each row.
Here's what I tried to do:
var startDate = Browser.inputBox('What is the start date?', Browser.Buttons.OK_CANCEL);
for (var k = 0; k < (13); k++) {
sheet.getRange((k+1),1).setValue(startDate + k * 7);
}
Unfortunately I think setValue treats startDate as a string? So for an input of 15/10/2013 in the box, and cells formatted to dd-mmm-yyyy, the cells are filled up with 15-Oct-20130, 15-Oct-20137, 15-Oct-201314 and so on. What I'd hoped for was 15-Oct-2013, 22-Oct-2013, 29-Oct-2013 and so on. Help?
You could take advantage of UIApp's dedicated widget to choose the starting date and of using spreadsheet formulas to create your date sequence (as you know that =date+7
in spreadsheet gives automatically the date 7 days later.
Here is how it could go :
function dateSequence() {
var app = UiApp.createApplication().setTitle('Choose a start Date').setHeight('100').setWidth('200');
var date = app.createDateBox().setName('date').setValue(new Date());
var clickHandler = app.createServerHandler('setDate').addCallbackElement(date);
app.add(date).add(app.createButton('submit', clickHandler));
SpreadsheetApp.getActive().show(app)
}
function setDate(e){
var app = UiApp.getActiveApplication().close();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1,1).setValue(e.parameter.date);
for (var k = 1; k < 13; k++) {
sheet.getRange((k+1),1).setFormula("= (A"+k+")+"+7);
}
return app;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.