This repository was archived by the owner on Feb 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheets-stuff.js
More file actions
126 lines (103 loc) · 3.82 KB
/
sheets-stuff.js
File metadata and controls
126 lines (103 loc) · 3.82 KB
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
const { google } = require("googleapis");
const { getGapiClient } = require("./gapi-credentials-load");
const spreadsheetId = {
"3D Print" : "1kN0h_cIYD3hXVbjN25dAvLbQDkcgIuIlHprRTBHEENg",
"Laser Cut" : "1fYLIYfgpsMatdU_0Yx0asM3B81-JFWs2cqIsEzxJ8f0",
"Poster" : "1g28e9iiXov1PLNtYomn2iKmvVLlHT75G7eV-bFDHG_c"
}
let drive;
let sheets;
let today = getToday();
async function initClient() {
return new Promise((resolve, reject) => {
getGapiClient().then(auth => {
drive = google.drive({ version: 'v3', auth });
sheets = google.sheets({ version: 'v4', auth });
resolve();
});
});
}
function getToday() {
let date = new Date();
let dd = String(date.getDate());
let mm = String(date.getMonth() + 1);
let yy = date.getFullYear() - 2000;
return mm + '/' + dd + '/' + yy;
}
async function getEmptyRow(sheetId) {
const res = await sheets.spreadsheets.values.get({
spreadsheetId: sheetId,
range: "In Queue!G3:G"
});
// const data = JSON.stringify(res.data, null, 2);
// console.log(data)
var emptyRow = 0;
res.data.values.some(element => {
if (element[0] != "") {
emptyRow++;
} else {
return true;
}
});
return emptyRow + 3;
}
async function updateRow(sheetId, row, values) {
let res = await sheets.spreadsheets.values.update({
spreadsheetId: sheetId,
range: `In Queue!A${row}`,
valueInputOption: "RAW",
resource: {
values: [values]
}
});
return res;
}
async function getFileNames(fileIds) {
let fileNames = [];
for (let i = 0; i < fileIds.length; i++) {
let res = await drive.files.get({
fileId: fileIds[i]
});
fileNames.push(res.data.name);
}
return fileNames;
}
async function convertToCells(data, boxLink) {
let fileNames = "";
console.log(today);
console.log(data);
let academic = data["Academic"] == "Yes" ? "Y" : "";
let source;
if (data["Source"] == "Own Material") {
source = "Theirs";
} else if (data["Source"] == "Makerspace Material") {
source = "Ours";
} else if (data["Source"] == "Scrap") {
source = "Scrap";
} else {
source = data["Source"];
}
let values = await getFileNames(data["Files"]).then(fns => {
fileNames = fns.join(", ");
let values = [];
if (data["Service"] == "3D Print") {
values = [null, null, null, null, today, "Forms", "Pending", data["Type"], null, data["Material"], academic, data["First Name"] + ' ' + data["Last Name"], data["Email"], fileNames, data["Files"].length, boxLink, null, "Send confirmation email. " + data["Specific Requests"]];
} else if (data["Service"] == "Laser Cut") {
values = [null, null, null, null, today, "Forms", "Pending", academic, data["First Name"] + ' ' + data["Last Name"], data["Email"], fileNames, boxLink, data["Files"].length, source, data["Material"], "Send confirmation email. " + data["Specific Requests"]];
} else if (data["Service"] == "Poster") {
values = [null, null, null, null, today, "Forms", "Pending", data["Type"], academic, data["First Name"] + ' ' + data["Last Name"], data["Email"], fileNames, boxLink, "Send confirmation email. " + data["Specific Requests"], data["Width"], data["Height"], data["Files"].length];
} else {
throw new Error("Unknown service");
}
return values;
});
return values;
}
async function updateSheets(data, boxLink) {
today = getToday();
await initClient();
let values = await convertToCells(data, boxLink);
let sheetId = spreadsheetId[data["Service"]];
getEmptyRow(sheetId).then(r => updateRow(sheetId, r, values));
}
module.exports = { updateSheets };