45 lines
1.4 KiB
JavaScript
45 lines
1.4 KiB
JavaScript
/**
|
|
* Google Apps Script to handle POST requests from the Chrome Extension
|
|
* 1. Create a new Google Sheet.
|
|
* 2. Extensions > Apps Script.
|
|
* 3. Paste this code.
|
|
* 4. Deploy > New Deployment > Web App.
|
|
* 5. Set 'Execute as: Me' and 'Who has access: Anyone'.
|
|
* 6. Copy the Web App URL and paste it into the Extension.
|
|
*/
|
|
|
|
function doPost(e) {
|
|
try {
|
|
var data = JSON.parse(e.postData.contents);
|
|
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
|
|
|
|
// Get headers
|
|
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
|
|
if (headers[0] === "" && sheet.getLastColumn() === 1) {
|
|
// New sheet, set headers from JSON keys
|
|
headers = Object.keys(data);
|
|
sheet.appendRow(headers);
|
|
}
|
|
|
|
// Map data to headers
|
|
var row = headers.map(function(header) {
|
|
return data[header] || "N/A";
|
|
});
|
|
|
|
sheet.appendRow(row);
|
|
|
|
return ContentService.createTextOutput(JSON.stringify({ status: "success" }))
|
|
.setMimeType(ContentService.MimeType.JSON);
|
|
|
|
} catch (error) {
|
|
return ContentService.createTextOutput(JSON.stringify({ status: "error", message: error.toString() }))
|
|
.setMimeType(ContentService.MimeType.JSON);
|
|
}
|
|
}
|
|
|
|
// Handle preflight (for some environments, though 'no-cors' usually avoids this)
|
|
function doOptions(e) {
|
|
return ContentService.createTextOutput("")
|
|
.setMimeType(ContentService.MimeType.TEXT);
|
|
}
|