Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script: In spreadsheet, how to select cell with date format value in it and change it to that date plus 7 days (1 week)

I have a google spreadsheet and want to use Google Apps Script to activate cell B1 in the first sheet (currently formatted as a date - 1/27/2014) and change it to that date plus 7 days (2/3/2014). I am pretty new to Google Apps Script and have only successfully written a couple other functions. What I was thinking so far was:

function changeDate() {

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheets()[0];

    var b1_date = sheet.getRange("B1").getValue();
    //...

}
like image 419
Kateb92 Avatar asked Jan 30 '14 21:01

Kateb92


1 Answers

function changeDate()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var cell = sheet.getRange("B1");
  var oldDate = cell.getValue().getTime(); //milliseconds from 1/1/1970
  var newDate = new Date();
  newDate.setTime(oldDate + (7*24*60*60*1000)); //add milliseconds in 7 days
  cell.setValue(newDate);  
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setTime

like image 57
AdamL Avatar answered Nov 15 '22 07:11

AdamL