-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
72 lines (60 loc) · 2.35 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
function wtHours() {
const USER_AGENT = "<email of the user/app that's accessing the Toggl API";
const YOUR_EMAIL = "<email to send reports to>";
const WORKSPACE_ID = "<workspace ID from Toggl>";
const PROJECT_ID = "<project ID(s) from Toggl>";
const API_KEY = "<API key from Toggl>";
// Only include the previous week (see note below)
var until = new Date();
until.setDate(until.getDate() - 1);
var since = new Date();
since.setDate(since.getDate() - 7);
// ***This converts to UTC time. Your date range may be off if you run the script at a time where UTC time is a day ahead/behind of where you are.
until = until.toISOString().substring(0, 10);
since = since.toISOString().substring(0, 10);
const API_URL = "https://toggl.com/reports/api/v2/details?order_field=date&until=" + until + "&since=" + since + "&user_agent=" + USER_AGENT + "&workspace_id=" + WORKSPACE_ID + "&project_ids=" + PROJECT_ID;
const response = UrlFetchApp.fetch(API_URL, {
contentType: "application/json",
headers: {
"Authorization" : "Basic " + Utilities.base64Encode(API_KEY + ":" + "api_token")
}
})
const parsed = JSON.parse(response.getContentText()).data;
var outputArr = [];
var currentDay = -1;
for (var i = parsed.length - 1; i >= 0; i--) {
var day = new Date(parsed[i].start).getDay();
if (day !== currentDay) {
currentDay = day;
outputArr.push("<strong>" + parsed[i].start.substring(0,10) + ":</strong>");
}
var description = "";
if (parsed[i].description.trim().length) {
description = " (" + parsed[i].description.trim() + ")";
}
outputArr.push(_prettyTime(parsed[i].start) + "-" + _prettyTime(parsed[i].end) + description);
}
MailApp.sendEmail(YOUR_EMAIL, "Last Week's Hours", "", {
htmlBody: outputArr.join("<br />")
});
}
function _calcHours(dateObj) {
var hours = dateObj.getHours();
var amPm = "AM";
if (hours > 12) {
hours -= 12;
amPm = "PM";
} else if (hours === 12) {
amPm = "PM";
}
return {
hours: hours,
amPm: amPm
};
}
function _prettyTime(dateObj) {
dateObj = new Date(dateObj);
const timeInfo = _calcHours(dateObj);
const minutes = ("0" + dateObj.getMinutes()).slice(-2);
return timeInfo.hours + ":" + minutes + " " + timeInfo.amPm;
}