-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.gs
93 lines (80 loc) · 2.08 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
function doGet() {
return HtmlService.createTemplateFromFile("index")
.evaluate()
.addMetaTag("viewport", "width=device-width, initial-scale=1");
}
// CONSTANTS
TIMESHEET_TABLE = "timesheet";
/**
* Returns a UUID.
* @return UUID.
* @customfunction
*/
function UUID() {
return Utilities.getUuid();
}
function getCurrentDateTimeDay() {
const now = new Date();
// Format the current date
const formattedDate = Utilities.formatDate(
now,
Session.getScriptTimeZone(),
"yyyy-MM-dd"
);
// Format the current time
const formattedTime = Utilities.formatDate(
now,
Session.getScriptTimeZone(),
"HH:mm:ss"
);
// Get the day of the week
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
const dayOfWeek = days[now.getDay()];
return {
date: formattedDate,
time: formattedTime,
day: dayOfWeek,
};
}
function clock(action, employee_id, latitude, longitude) {
const key = Session.getTemporaryActiveUserKey();
const { date, time, day } = getCurrentDateTimeDay();
data = {
id: UUID(),
type: action,
employee_id,
time,
date,
day,
key,
latitude,
longitude,
lat_long: `${latitude} ${longitude}`,
};
const sheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(TIMESHEET_TABLE);
// Get all data in the sheet
const sheetData = sheet.getDataRange().getValues();
// Get the header row
const headers = sheetData[0];
// Create a new row array with the same length as headers
const newRow = new Array(headers.length).fill("");
// Populate the new row with data from the object
for (let key in data) {
const columnIndex = headers.indexOf(key);
if (columnIndex !== -1) {
newRow[columnIndex] = data[key];
}
}
// Append the new row to the sheet
sheet.appendRow(newRow);
return data["time"];
}