How to put in a Cell the number of days pass from a specific date. For example:
Column1: 12/05/2013 18:00:00 Actual Days using Function New Date() Column2: The scrip
This is a possible way to get the result you want, I wrote it as a custom function, ie you have to put it in the the cell where you want it to appear in the form =dayToToday()
here is the script with a few logs and comments to show intermediate values.
function dayToToday(x){
var sh = SpreadsheetApp.getActiveSheet();
Logger.log(sh.getActiveCell().getRowIndex())
var refcell = sh.getRange(sh.getActiveCell().getRowIndex(),1).getValue();;// get value in column A to get the reference date
var refTime = new Date(refcell);
var ref = refTime.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var today = new Date();
var TD = today.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var day = parseInt(TD-ref);// get the difference in days (integer value )
return day ; // return result that will be in cell
}
EDIT : if I understand your use case I suggest you abandon the custom function aproach (that I never use since I don't like it so much) and go for this solution that uses a timer trigger and/or a menu to update values in your spreadsheet in one batch.
(ps : sorry for not having answer quickly enough... too busy these days :-)
function onOpen() {
var menuEntries = [ {name: "test function", functionName: "toTrigger"},
];
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.addMenu("test menu",menuEntries);//
}
// create a timer trigger that will call "toTrigger" every 15 minutes
function toTrigger(){
var sh = SpreadsheetApp.getActiveSheet();
var data = sh.getRange(1,1,sh.getLastRow(),2).getValues();
for(var n=0;n
second EDIT:
when using var data = sh.getRange(1,1,sh.getLastRow(),2).getValues();
the numbers in getRange are start row, start column, number of Rows, number of Columns so you'll have to gat a range that includes the columns you want and then use data [n][columnNumber-1] to get the corresponding value. (-1 because array start from 0 and columns count from 1 (A in fact but A is first column ;-)) if you need a variable reference then let me know because the function must be modified to accept 2 variables (a ref and another one).
LAST EDIT (I hope)
I changed some details... see [9] and [5] index corresponding to J and F
function toTrigger(){
var sh = SpreadsheetApp.getActiveSheet();
var data = sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
for(var n=0;n
update/edit
result in hours (following request in comments)
function hoursToToday(x){
var refcell = x;;// get value in column A to get the reference date
var refTime = new Date(refcell);
var refhrs = refTime.getHours();
var ref = refTime.setHours(refhrs,0,0,0)/3600000;// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var today = new Date();
var todayHrs = today.getHours()
var TD = today.setHours(todayHrs,0,0,0)/3600000;// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var hrs = TD-ref;// get the difference in days (integer value )
return hrs ; // return result that will be in cell
}