initial commit
This commit is contained in:
5
.vscode/extensions.json
vendored
Normal file
5
.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"apenara.gas-intellisense"
|
||||
]
|
||||
}
|
||||
58
main.gs
Normal file
58
main.gs
Normal file
@@ -0,0 +1,58 @@
|
||||
function doGet(e) {
|
||||
var type = "";
|
||||
if (e.parameter.type) {
|
||||
switch (String(e.parameter.type).toLowerCase()) {
|
||||
case "json":
|
||||
type = "json";
|
||||
break;
|
||||
case "csv":
|
||||
type = "csv";
|
||||
break;
|
||||
default:
|
||||
type = "csv";
|
||||
}
|
||||
|
||||
} else {
|
||||
type = "csv";
|
||||
}
|
||||
|
||||
const sheet = SpreadsheetApp.openById("SPREADSHEET_ID") // <-- ID of spreadsheet to scrape
|
||||
.getsheets()[0]
|
||||
.getSheetValues(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
|
||||
|
||||
const colNames = data[0].filter((val) => val.toString().length > 0);
|
||||
|
||||
const trimRegex = /,*$/;
|
||||
const blankLineRegex = /^[\s\n\r]*/gm;
|
||||
var csvRows = new Array();
|
||||
var jsonContent = {
|
||||
"time": new Date(Date.now()).toISOString(),
|
||||
"codes": []
|
||||
};
|
||||
|
||||
for (const row of sheet) {
|
||||
if (type == "csv") {
|
||||
csvRows.push(row.join(",").replace(trimRegex, ""));
|
||||
} else {
|
||||
if (row[1].toString().length > 0) {
|
||||
var newObj = {};
|
||||
row.join().replace(trimRegex, "").split(",").forEach((val, i) => {
|
||||
newObj[colNames[i]] = val.toString();
|
||||
});
|
||||
jsonContent.codes.push(newObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const output = ContentService.createTextOutput();
|
||||
if (type == "csv") {
|
||||
output.setContent(csvRows.join("\u000A").replace(blankLineRegex, ""));
|
||||
output.setMimeType(ContentService.MimeType.CSV);
|
||||
} else {
|
||||
jsonContent.codes = jsonContent.codes.slice(1)
|
||||
output.setContent(JSON.stringify(jsonContent));
|
||||
output.setMimeType(ContentService.MimeType.JSON);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
Reference in New Issue
Block a user