-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ts
369 lines (299 loc) · 11 KB
/
code.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
const clockify_workspace_id = "REDACTED";
const clockify_user_id = "REDACTED";
const clockify_api_key = "REDACTED";
const entry_range = "A12:G200";
const totals_range = "A6:C9";
const CLOCKIFY_GET_ENTRIES = `https://api.clockify.me/api/v1/workspaces/${clockify_workspace_id}/user/${clockify_user_id}/time-entries?start={0}&end={1}`;
const CLOCKIFY_GET_PROJECT = `https://api.clockify.me/api/v1/workspaces/${clockify_workspace_id}/projects`;
const CLOCKIFY_GET_TAGS = `https://api.clockify.me/api/v1/workspaces/${clockify_workspace_id}/tags`;
/**
* Add menu items to the google sheets interface
*/
function onOpen() {
let ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Time Tracking')
.addItem('Retrieve items', 'loadTimeReg')
.addItem('Generate Markdown', 'showMarkdown')
.addItem('Generate ALL Markdowns', 'showAllMarkdowns')
.addItem('Show projects', 'showProjects')
.addItem('Show Time Entries', 'showEntries')
.addToUi();
}
/**
* Used to replace arguments in strings with url encoded values
* @param template Template string, arguments are formatted {i} where i is the index of the argument
* @param args Array of arguments to put in the template
*/
function url_format(template, args) {
let ret = template;
for (let i = 0; i < args.length; i++) {
ret = ret.replace(`{${i}}`, encodeURIComponent(args[i]));
}
return ret;
}
/**
* Make a request to the clockify API
* @param template Template request URI
* @param arguments Arguments for the template string given in the first argument
*/
function clockify_request(template, arguments = []) {
var response = UrlFetchApp.fetch(
url_format(template, arguments),
{
"headers": {
"X-Api-Key": clockify_api_key
}
}
).getContentText();
return JSON.parse(response);
}
/**
* Convert a clockify duration string to an object containing the hour, minute and seconds seperately
* @param str The clockify duration string
*/
function extractDuration(str: string) {
let obj = {
hour: "00",
minute: "00",
second: "00",
toStr: function () {
return this.hour + ":" + this.minute + ":" + this.second;
}
};
Logger.log(str);
let index = str.indexOf("H");
if (index != -1) {
let length = 0;
index--;
while (str.charAt(index) >= '0' && str.charAt(index) <= '9') {
index--;
length++;
}
obj.hour = str.substr(index + 1, length);
}
index = str.indexOf("M");
if (index != -1) {
let length = 0;
index--;
while (str.charAt(index) >= '0' && str.charAt(index) <= '9') {
index--;
length++;
}
obj.minute = str.substr(index + 1, length);
}
index = str.indexOf("S");
if (index != -1) {
let length = 0;
index--;
while (str.charAt(index) >= '0' && str.charAt(index) <= '9') {
index--;
length++;
}
obj.second = str.substr(index + 1, length);
}
return obj;
}
/**
* Load all clockify entries for the selected week, and match them with the right proof descriptions previously entered in the sheet
*/
function loadTimeReg() {
let entryRange = SpreadsheetApp.getActiveSheet().getRange(entry_range);
let startDate = SpreadsheetApp.getActiveSheet().getRange(2, 3).getDisplayValue();
let endDate = SpreadsheetApp.getActiveSheet().getRange(3, 3).getDisplayValue();
let weekEntries = clockify_request(CLOCKIFY_GET_ENTRIES, [startDate, endDate]);
let projects = clockify_request(CLOCKIFY_GET_PROJECT);
let projectMap = {};
for (let project of projects) {
projectMap[project.id] = project.name;
}
let tags = clockify_request(CLOCKIFY_GET_TAGS);
let tagMap = {};
for (let tag of tags) {
tagMap[tag.id] = tag.name;
}
let oldProofs = {};
let oldValues = entryRange.getValues();
for (let i = 0; i < 100; i++) {
if (oldValues[i][0] != "") {
oldProofs[oldValues[i][0] + "T" + oldValues[i][1]] = oldValues[i][5];
}
}
entryRange.clear();
let data = new Array(0);
for (let entry of weekEntries) {
if(entry.timeInterval.end == null) {
continue;
}
var startTime = new Date(entry.timeInterval.start);
let rowData = new Array(7);
rowData[0] = startTime.getDate() + "-" + (1 + startTime.getMonth()) + "-" + startTime.getFullYear();
rowData[1] = startTime.getHours() + ":" + startTime.getMinutes();
let durationObject = extractDuration(entry.timeInterval.duration);
rowData[2] = durationObject.toStr();
//// Place tag again
let tagValue = "";
if (entry.tagIds) {
for (let tag of entry.tagIds) {
if (tagValue != "") tagValue += ", ";
tagValue += tagMap[tag];
}
}
rowData[3] = tagValue;
//// Place description
rowData[4] = entry.description;
//// Place project ID
rowData[6] = projectMap[entry.projectId];
data.push(rowData);
}
let outputRange = SpreadsheetApp.getActiveSheet().getRange(entryRange.getRow(), entryRange.getColumn(), data.length, entryRange.getWidth());
outputRange.setValues(data);
let displayValues = outputRange.getDisplayValues();
let newValues = new Array(displayValues.length);
for(let rowNr in displayValues) {
let displayValueRow = displayValues[rowNr];
//// Place proof again
let currentProof = oldProofs[displayValueRow[0] + "T" + displayValueRow[1]];
if (currentProof) {
newValues[rowNr] = [currentProof];
} else {
newValues[rowNr] = [""];
}
}
let proofRange = SpreadsheetApp.getActiveSheet().getRange(outputRange.getRow(), outputRange.getColumn() + 5, newValues.length);
proofRange.setValues(newValues);
}
/**
* Open a debug window showing all time entries for the selected week
*/
function showEntries() {
let startDate = SpreadsheetApp.getActiveSheet().getRange(2, 3).getDisplayValue();
let endDate = SpreadsheetApp.getActiveSheet().getRange(3, 3).getDisplayValue();
let projects = clockify_request(CLOCKIFY_GET_ENTRIES, [startDate, endDate]);
var htmlOutput = HtmlService
.createHtmlOutput(JSON.stringify(projects));
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Projects');
}
/**
* Show all Clockify projects listed for the current API settings (top of this file)
*/
function showProjects() {
let projects = clockify_request(CLOCKIFY_GET_PROJECT, []);
let table = "<table><tr><td>name</td><td>id</td></tr>";
for (let project of projects) {
table += `<tr><td>${project.name}</td><td>${project.id}</td></tr>`
}
table += "</table>";
var htmlOutput = HtmlService
.createHtmlOutput(table);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Projects');
}
/**
* Generate a markdown table for a given row/column dataset
* @param headers Table headers to use for each column
* @param data Data to convert to markdown
*/
function generateMDTable(headers, data) {
let md_headers = "|";
let md_seperator = "|";
for (let header of headers) {
if(header == "Starttijd") {
continue;
}
md_headers += header + "|";
md_seperator += "----|";
}
md_headers += "\r\n";
md_seperator += "\r\n";
let md_data = "";
for (let row = 0; row < data.length; row++) {
if (data[row][0] == "") {
continue
}
md_data += "|";
for (let col = 0; col < headers.length; col++) {
if(col == 1 && headers.length > 3) {
continue;
}
md_data += data[row][col] + " |";
}
md_data += "\r\n";
}
return `${md_headers}${md_seperator}${md_data}`;
}
const tableHeaders = ["Datum", "Starttijd", "Duur", "Categorie", "Omschrijving", "Details + Bewijslast", "_(C)_"];
const totalsHeaders = ["Onderdeel", "Deze week", "Totaal"];
/**
* Format the currently selected week according to a preset markdown template
*/
function getMarkdown() {
let week = SpreadsheetApp.getActiveSheet().getName();
let entryTable = generateMDTable(tableHeaders, SpreadsheetApp.getActiveSheet().getRange(entry_range).getDisplayValues());
entryTable = entryTable.replace(/\|R2D2 Extra \|/g, "|![E](uploads/3d01f7850afee42575d32bd87f23c75c/image.png \"E\")|");
entryTable = entryTable.replace(/\|R2D2 Research \|/g, "|![R](uploads/f6816b8ec1d90a06bdf6b81deb104273/image.png \"R\")|");
entryTable = entryTable.replace(/\|R2D2 \|/g, "|![S](uploads/3d01f7850afee42575d32bd87f23c75c/image.png \"S\")|");
let totalsTable = generateMDTable(totalsHeaders, SpreadsheetApp.getActiveSheet().getRange(totals_range).getDisplayValues());
let description = SpreadsheetApp.getActiveSheet().getRange("E6").getValue();
return `
## ${week}
> ${description}
### Tijdregistraties
#### Cumulatief
${totalsTable}
_____________________________________________________________________________________________________________________
${entryTable}
_____________________________________________________________________________________________________________________
`
}
/**
* Show a markdown string
* @param md Markdown string to display, if not given, the markdown template will be used for the currently selected week
*/
function showMarkdown(md = getMarkdown()) {
var htmlOutput = HtmlService
.createHtmlOutput(`
<script>
function copy(id) {
let textarea = document.getElementById(id);
textarea.select();
document.execCommand("copy");
}
</script>
<h2>Entry table</h2>
<textarea style="width: 100%; height:400px;resize: none; " id="entries">
${md}
</textarea>
<button style="width:100%;" onclick="copy('entries')">Copy</button>
`
).setWidth(600)
.setHeight(700);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Markdown viewer');
}
/**
* Generate and show the combined markdown format for all weeks currently in the sheet
*/
function showAllMarkdowns() {
let sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
let fullMarkDown = "";
for(let sheetIndex = sheets.length -1; sheetIndex >= 1; sheetIndex--) {
sheets[sheetIndex].activate();
fullMarkDown += getMarkdown();
fullMarkDown += "<br>";
}
showMarkdown(fullMarkDown);
}
/**
* A function used in the excel sheet to calculate total spent time per week.
* This function returns the name of the sheet to the left of the current sheet, so it can be used in cross-references
*/
function previousName() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var curSheet = ss.getActiveSheet();
var curSheetIndex = curSheet.getIndex();
if (curSheetIndex == 1) {
return "FUCKINGERROR";
}
var preSheetIndex = curSheetIndex - 2;
var preSheet = ss.getSheets()[preSheetIndex];
return preSheet.getName();
}