/
Code.gs
102 lines (102 loc) · 3.17 KB
/
Code.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Sums input hours using syntax hh:mm.
*
* @param {range} input The values to add up.
* @customfunction
*/
function ADDTIMES(input) {
let minutes = 0;
let debug = false;
if (input == undefined) return "Undefined";
if (typeof(input) == 'object') {
if (typeof(input[0]) == 'object') {
for (let i = 0; i < input.length; ++i) {
for (let j = 0; j < input[i].length; ++j) {
if (isNaN(parseInt(input[i][j]))) continue;
minutes += parseInt(input[i][j].slice(0,input[i][j].indexOf(":")))*60
if (isNaN(minutes)) return "NaN from hours calc 1";
minutes += parseInt(input[i][j].slice(input[i][j].indexOf(":")+1))
if (isNaN(minutes)) return "NaN from min calc 1";
}
}
}
else {
return "This doesn't ever happen";
}
}
else {
minutes += parseInt(input.slice(0,input.indexOf(":")))*60
if (isNaN(minutes)) return "NaN from hours calc";
minutes += parseInt(input.slice(input.indexOf(":")+1))
if (isNaN(minutes)) return "NaN from minutes calc";
}
let h = Math.trunc(parseFloat(minutes)/60);
if (isNaN(h)) {
return "NaN from hour display"
}
let m = (minutes%60>=10 ? minutes%60 : "0" + minutes%60);
if (isNaN(m)) {
return "NaN from minute display"
}
if (debug == true) {
debug = "";
try {
debug += typeof(input) + "[" + input.length + "]" + ", "
debug += typeof(input[0]) + "[" + input[0].length + "]" + ", "
debug += typeof(input[0][0]) + "[" + input[0][0].length + "]"
} catch {}
}
if (debug != "") return (h + ":" + m + ", " + debug)
return (h + ":" + m);
}
/**
* Subtracts input hours using syntax hh:mm.
*
* @param {a,b} input Subtracts b range from a range.
* @customfunction
*/
function SUBTIMES(a,b) {
let minutes = 0;
if (typeof(a) == "string" && a.indexOf(":")==-1) { // overrides for format 00h00m
if (a.indexOf("h")!=-1) {
minutes += parseInt(a.slice(0,a.indexOf("h")))*60
if (a.indexOf("m")!=-1) {
minutes += parseInt(a.slice(a.indexOf("h")+1,a.indexOf("m")))
}
else if (!isNaN(parseInt(a.slice(a.indexOf("h")+1)))) {
minutes += parseInt(a.slice(a.indexOf("h")+1))
}
}
else {
minutes += parseInt(a.slice(a.indexOf("h")+1))
}
if (isNaN(minutes)) {
return "";
}
return FORMATMINUTES(minutes);
}
if (typeof(a[0]) == 'object') a = ADDTIMES(a);
if (typeof(b[0]) == 'object') b = ADDTIMES(b);
minutes = GETMINUTES(b)-GETMINUTES(a);
while (minutes < 0) {
minutes += 12*60
}
return FORMATMINUTES(minutes);
}
/**
* Gets total minutes from hours and minutes using syntax hh:mm.
*
* @param {input} input Input hh:mm to get total minutes.
* @customfunction
*/
function GETMINUTES(input) {
return parseInt(input.slice(0,input.indexOf(":")))*60 + parseInt(input.slice(input.indexOf(":")+1))
}
/**
* Gets hours and minutes in syntax hh:mm from an integer representing total minutes.
* @param {input} input Input total minutes to get hours and minutes in syntax hh:mm.
* @customfunction
*/
function FORMATMINUTES(minutes) {
return (Math.trunc(minutes/60) + ":" + (Math.abs(minutes)%60>=10 ? Math.abs(minutes)%60 : "0" + Math.abs(minutes%60)));
}