Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function doPost(e) {
- Logger.log("== doPost DIPANGGIL ==");
- try {
- // Log mentah dari body POST
- const raw = e.postData.contents;
- Logger.log("Raw Payload: " + raw);
- const a = JSON.parse(raw);
- Logger.log("Parsed JSON: " + JSON.stringify(a));
- if (!a || typeof a !== "object" || Array.isArray(a)) {
- Logger.log("ERROR: JSON tidak valid atau kosong");
- throw new Error("Invalid or empty JSON object");
- }
- const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
- Logger.log("Spreadsheet dibuka");
- const keys = Object.keys(a);
- Logger.log("Keys ditemukan: " + keys.join(", "));
- if (keys.length === 0) {
- Logger.log("ERROR: Tidak ada keys");
- throw new Error("No data keys provided");
- }
- const lastCol = sheet.getLastColumn();
- Logger.log("Last Column: " + lastCol);
- let header = [];
- if (lastCol === 0) {
- Logger.log("Sheet kosong, header akan dibuat");
- header = keys;
- sheet.appendRow(header);
- } else {
- Logger.log("Ambil header dari baris 1");
- header = sheet.getRange(1, 1, 1, lastCol).getValues()[0];
- }
- Logger.log("Header: " + header.join(", "));
- const row = header.map(h => a[h] !== undefined ? a[h] : "");
- Logger.log("Row yang akan ditulis: " + row.join(", "));
- sheet.appendRow(row);
- Logger.log("Row berhasil ditambahkan");
- const response = {
- status: "success",
- received: a
- };
- Logger.log("Mengirim response sukses: " + JSON.stringify(response));
- return ContentService
- .createTextOutput(JSON.stringify(response))
- .setMimeType(ContentService.MimeType.JSON);
- } catch (err) {
- Logger.log("CATCH ERROR: " + err.message);
- const responseError = {
- status: "error",
- message: err.message
- };
- return ContentService
- .createTextOutput(JSON.stringify(responseError))
- .setMimeType(ContentService.MimeType.JSON);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment