我使用以下方法将时间戳放在E和F列中:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Timesheet" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(-1, 5);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
var nextCell = r.offset(0, 4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
}
我希望在列H(偏移7)中放置两个单元格之间的时间差。我试过了:
var startTime = r.offset(-1, 4);
var stopTime = r.offset(-1, 5);
var totalTime = r.offset(-1, 7);
var difTime = stopTime - startTime;
totalTime.setValue(difTime);
但它只是给了我NaN。
答案 0 :(得分:0)
尝试使用以下两行:
var difTime = stopTime - startTime;
roundedDayes.setValue(difTime);
我认为你将错误的变量传递给setValue()
答案 1 :(得分:0)
确保startTime
和stopTime
采用日期格式
在您的代码中添加此内容。
var startTime = r.offset(-1, 4);
var stopTime = r.offset(-1, 5);
var totalTime = r.offset(-1, 7);
var difTime = stopTime - startTime;
var difHours = Math.floor(difTime / (3600 * 1000));
totalTime.setValue(difHours );
答案 2 :(得分:0)
这是一个有效的解决方案,我更改了我的测试的偏移值,只使用start = colA,stop = col B并导致col C(或同一行上3个连续单元格的任何其他组合)
function test(){
var r = SpreadsheetApp.getActive().getActiveCell();
var startTime = new Date(r.offset(0, 0).getValue()).getTime();// get time in millisec
Logger.log(startTime);
var stopTime = new Date(r.offset(0, 1).getValue()).getTime();// get time in millisec
Logger.log(stopTime);
var totalTime = r.offset(0, 2); // define output range
var difTime = stopTime - startTime;
var difHours = Math.floor(difTime / (3600 * 1000)); // do the math
Logger.log(difTime);
totalTime.setValue(difHours ); // and update result cell
}