Google sheet as database
The Apps Script
const sheetName = 'Sheet1';
const scriptProp = PropertiesService.getScriptProperties();
function initialSetup() {
const activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
scriptProp.setProperty('key', activeSpreadsheet.getId());
}
function doPost(e) {
const lock = LockService.getScriptLock();
lock.tryLock(10000);
try {
const doc = SpreadsheetApp.openById(scriptProp.getProperty('key'));
const sheet = doc.getSheetByName(sheetName);
const headers = sheet
.getRange(1, 1, 1, sheet.getLastColumn())
.getValues()[0];
const nextRow = sheet.getLastRow() + 1;
const newRow = headers.map(function (header) {
return header === 'Date' ? new Date() : e.parameter[header];
});
sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow]);
return ContentService.createTextOutput(
JSON.stringify({ result: 'success', row: nextRow })
).setMimeType(ContentService.MimeType.JSON);
} catch (e) {
return ContentService.createTextOutput(
JSON.stringify({ result: 'error', error: e })
).setMimeType(ContentService.MimeType.JSON);
} finally {
lock.releaseLock();
}
}
function doGet() {
const doc = SpreadsheetApp.getActiveSpreadsheet();
const sheetName = 'Sheet1'; // Make sure to define sheetName
const sheet = doc.getSheetByName(sheetName);
const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
const values = sheet
.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn())
.getDisplayValues();
const result = values.map((col) => {
let obj = {};
headers.forEach((header, index) => {
obj[header] = col[index];
});
return obj;
});
return ContentService.createTextOutput(
JSON.stringify({ data: result })
).setMimeType(ContentService.MimeType.JSON);
}