[英]Calculate time duration with google script
I'm new to coding and I found a script that I slightly adapted for my purpose: record on a timesheet the timestamps of the beginning and the end of an action, when you press buttons "Begin" and "End". 我是编码的新手,我发现了一个脚本,我稍微适应了我的目的:当你按下“开始”和“结束”按钮时,在时间表上记录动作开始和结束的时间戳。 I would also like the script to calculate the time duration between the two timestamps, which at first I thought would be easy but I'm unable to find a solution, so I'm asking for your help.
我还希望脚本能够计算两个时间戳之间的持续时间,起初我觉得这很简单,但我找不到解决方案,所以我要求你的帮助。
function setValue(cellname, value) {
SpreadsheetApp.getActiveSpreadsheet().getRange(cellname).setValue(value);
}
function getValue(cellname, value) {
return SpreadsheetApp.getActiveSpreadsheet().getRange(cellname).getValue();
}
function getNextRow() {
return SpreadsheetApp.getActiveSpreadsheet().getLastRow() + 1;
}
function getLastRow() {
return SpreadsheetApp.getActiveSpreadsheet().getLastRow();
}
function addRecord(a, b) {
var row =getNextRow();
setValue('A' + row, a);
setValue('B' + row, b);
}
function addRecord2(c) {
var row =getLastRow();
setValue('C' + row, c);
}
function Begin() {
addRecord(getValue('B1'), new Date());
}
function End() {
addRecord2(new Date());
}
Thanks everybody, I wrote a new version of my script after reading your suggestions. 谢谢大家,我在阅读你的建议后写了一个新版本的脚本。 But I get a #NUM!
但是我得到了#NUM! error in column C.
C列中的错误
var start = 0;
var finish = 0;
function setValue(cellname, value) {
SpreadsheetApp.getActiveSpreadsheet().getRange(cellname).setValue(value);
}
function getNextRow() {
return SpreadsheetApp.getActiveSpreadsheet().getLastRow() + 1;
}
function getLastRow() {
return SpreadsheetApp.getActiveSpreadsheet().getLastRow();
}
function addRecord(a) {
var row =getNextRow();
setValue('A' + row, a);
}
function addRecord2(b) {
var row =getLastRow();
setValue('B' + row, b);
}
function addRecord3(c) {
var row =getLastRow();
setValue('C' + row, c);
}
function begin() {
start=addRecord(new Date().getTime());
}
function end() {
finish=addRecord2(new Date().getTime());
addRecord3((finish-start))
}
var start = 0;
var finish = 0;
var yourCell = SpreadsheetApp.getActiveSpreadsheet().getRange(cellname);
function begin(){
start = new Date().getTime();
}
function end(){
finish = new Date().getTime();
writeElapsed(start, finish)
}
function writeElapsed(start, finish){
yourCell.setValue(finish - start); //will give elapsed time in ms
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.