I have a Google Sheet with three columns: - Date and time (timestamp) - Duration - Description
I have an script that when I write something in 'Description', inserts in 'Date' the date and time at this moment, and the 'Duration':
function onEdit(e) {
if(e.source.getActiveSheet().getName() == "Sheet2" ) {
var col = e.source.getActiveCell().getColumn();
if(col == 3 ) {
// I'm in column three
var cellTimeStamp = e.range.offset(0,-2); // First column of the same row
var cellTimeDiff = e.range.offset(0,-1); // Second column of the same row
var cellTimePrev = e.range.offset(-1,-2); // First column of the previous row
var timeTimeStamp = new Date();
var iniTime = cellTimePrev.getValue().getTime();
var finTime = timeTimeStamp.getTime() ;
var timeDiff = String(finTime - iniTime) ;
cellTimeStamp.setValue(timeTimeStamp);
cellTimeDiff.setValue(timeDiff); // [***]
}
}
}
When this executes (as an event) in the column of 'Duration' there is NOT something in the format of 'HH:mm:ss'.
But if I remove the last line in this script and adds this formulae in the sheet: =A3-A2 (in row 3) =A4-A3 (in row 4) ... then it works ok.
I'd like to know how to meet the same result but with a script.
Thanks in advance.
[hh]:mm:ss
. I don't think this has ever been written down, I had to get it by usingrange.getNumberFormat()
– Matthh:mm:ss
which was fine providing the difference was under a day. I had to figure out how to show the duration once the differences exceeded a day. I wish Google would just documentRange::setNumberFormat()
... – Matt