admin管理员组文章数量:1025504
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! error in column 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))
}
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! error in column 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))
}
Share
Improve this question
edited Jan 10, 2017 at 22:55
Marie
asked Jan 9, 2017 at 21:49
MarieMarie
211 silver badge3 bronze badges
7
-
2
new Date() returns a Date object. a Date object has a get a
getTime
method that returns a number of milliseconds ... subtract one from the other to get the difference in milliseconds .. perform appropriate calculations to get years,months,days,hours,minutes,seconds as required – Jaromanda X Commented Jan 9, 2017 at 21:51 - There are also getHours(), getMinutes(), etc. See this Date/Time Functions tutorial – Karl_S Commented Jan 9, 2017 at 22:23
- 1 Few questions: What do you mean by pressing buttons? Have you inserted drawing and assigned script to those? If yes, then have you assigned Begin() and End() functions itself? Also, you want to calculate timestamps of beginning and end of an action, which action? Pressing of begin and then end button respectively? Also, time difference can be measured in various formats. What exactly are you looking for? – Shyam Kansagra Commented Jan 10, 2017 at 5:34
-
1
Agreeing with @ShyamKansagra -- there's a few more details needed. Nonetheless, the issue is simply paring the two dates in the code (generated by
Begin()
andEnd()
). Possible duplicate of stackoverflow./q/11174385/4625829. – AL. Commented Jan 10, 2017 at 10:05 - Thanks for your responses! @ShyamKansagra, yes, I've assigned function "Begin" and "End" respectively to 2 buttons. What I want is that pressing button Begin generates a timestamp (in column A), and pressing button End generates another timestamp (ColB) + the duration between the two timestamps (ColC). A: 06/02/2017 06:00:12|| B: 06/02/2017 06:03:13 || C: 00:03:01. – Marie Commented Jan 10, 2017 at 22:01
1 Answer
Reset to default 3var 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
}
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! error in column 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))
}
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! error in column 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))
}
Share
Improve this question
edited Jan 10, 2017 at 22:55
Marie
asked Jan 9, 2017 at 21:49
MarieMarie
211 silver badge3 bronze badges
7
-
2
new Date() returns a Date object. a Date object has a get a
getTime
method that returns a number of milliseconds ... subtract one from the other to get the difference in milliseconds .. perform appropriate calculations to get years,months,days,hours,minutes,seconds as required – Jaromanda X Commented Jan 9, 2017 at 21:51 - There are also getHours(), getMinutes(), etc. See this Date/Time Functions tutorial – Karl_S Commented Jan 9, 2017 at 22:23
- 1 Few questions: What do you mean by pressing buttons? Have you inserted drawing and assigned script to those? If yes, then have you assigned Begin() and End() functions itself? Also, you want to calculate timestamps of beginning and end of an action, which action? Pressing of begin and then end button respectively? Also, time difference can be measured in various formats. What exactly are you looking for? – Shyam Kansagra Commented Jan 10, 2017 at 5:34
-
1
Agreeing with @ShyamKansagra -- there's a few more details needed. Nonetheless, the issue is simply paring the two dates in the code (generated by
Begin()
andEnd()
). Possible duplicate of stackoverflow./q/11174385/4625829. – AL. Commented Jan 10, 2017 at 10:05 - Thanks for your responses! @ShyamKansagra, yes, I've assigned function "Begin" and "End" respectively to 2 buttons. What I want is that pressing button Begin generates a timestamp (in column A), and pressing button End generates another timestamp (ColB) + the duration between the two timestamps (ColC). A: 06/02/2017 06:00:12|| B: 06/02/2017 06:03:13 || C: 00:03:01. – Marie Commented Jan 10, 2017 at 22:01
1 Answer
Reset to default 3var 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
}
本文标签: javascriptCalculate time duration with google scriptStack Overflow
版权声明:本文标题:javascript - Calculate time duration with google script - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745622843a2159681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论