Skip to content

Instantly share code, notes, and snippets.

@freddiefujiwara
Last active July 23, 2021 10:16
Show Gist options
  • Save freddiefujiwara/de1dff07830997f4dbd09f2da13fdd15 to your computer and use it in GitHub Desktop.
Save freddiefujiwara/de1dff07830997f4dbd09f2da13fdd15 to your computer and use it in GitHub Desktop.
WishToDoList
{
"timeZone": "Asia/Tokyo",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"webapp": {
"executeAs": "USER_DEPLOYING",
"access": "ANYONE_ANONYMOUS"
}
}
const SPREADSHEET_ID = "1LjFZsrQJJkOHcmKPWnFwcipUjIRRwNNsJfQU1r1Y_RE";
const sheet = SpreadsheetApp.openById(SPREADSHEET_ID);
const list = sheet.getSheetByName("list");
function doPost(e) {
const values = list.getDataRange().getValues();
const headers = values.shift();
const data = JSON.parse(e.postData.getDataAsString());
const id = data.id;
const output = ContentService.createTextOutput();
if (!id || id >= list.getLastRow()) {
output.setMimeType(ContentService.MimeType.JSON);
output.setContent(JSON.stringify({
status: false
}));
return output;
}
Object.keys(data).forEach(key => {
if ('id' === key) return;
if (-1 === headers.indexOf(key)) return;
list.getRange(id + 1, headers.indexOf(key) + 1).setValue(data[key]);
});
output.setMimeType(ContentService.MimeType.JSON);
output.setContent(JSON.stringify({
status: true
}));
return output;
}
function doGet(e) {
let result = [];
const values = list.getDataRange().getValues();
const headers = values.shift();
result = result.concat(values.map((row) => {
let data = {};
row.map((column, index) => {
data[headers[index]] = column;
});
return data;
}));
const output = ContentService.createTextOutput();
if (e.parameter.callback === undefined) {
output.setMimeType(ContentService.MimeType.JSON);
output.setContent(JSON.stringify(result));
} else {
output.setMimeType(ContentService.MimeType.JAVASCRIPT);
output.setContent(e.parameter.callback + "&&" + e.parameter.callback + "(" + JSON.stringify(result) + ");");
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment