support edit

This commit is contained in:
Julian Freeman
2026-03-11 14:01:47 -04:00
parent 527046339a
commit 22c511daf8
3 changed files with 106 additions and 51 deletions

View File

@@ -24,41 +24,51 @@ function doPost(e) {
sheet = ss.getActiveSheet();
}
var headers = [];
// 2. Determine headers
if (sheet.getLastRow() === 0) {
// Use provided headers if available
if (data.headers && Array.isArray(data.headers)) {
headers = data.headers;
} else {
// Fallback: Use keys but exclude internal control keys
headers = Object.keys(data).filter(function(key) {
return !["sheetName", "headers"].includes(key);
});
}
sheet.appendRow(headers);
var existingHeaders = [];
var lastCol = sheet.getLastColumn();
var lastRow = sheet.getLastRow();
// 2. Get incoming headers (from data.headers or keys)
var incomingHeaders = data.headers || Object.keys(data).filter(function(k) {
return !["sheetName", "headers"].includes(k);
});
if (lastRow === 0) {
// New sheet: Write all incoming headers as the first row
existingHeaders = incomingHeaders;
sheet.appendRow(existingHeaders);
} else {
// Get existing headers from the first row
var lastCol = sheet.getLastColumn();
if (lastCol > 0) {
headers = sheet.getRange(1, 1, 1, lastCol).getValues()[0];
// Existing sheet: Read current headers from row 1
existingHeaders = sheet.getRange(1, 1, 1, lastCol).getValues()[0];
// Find headers that are in 'incoming' but not in 'existing'
var missingHeaders = incomingHeaders.filter(function(h) {
return existingHeaders.indexOf(h) === -1;
});
if (missingHeaders.length > 0) {
// Append missing headers to the end of row 1
sheet.getRange(1, lastCol + 1, 1, missingHeaders.length).setValues([missingHeaders]);
// Update local existingHeaders list to include new ones
existingHeaders = existingHeaders.concat(missingHeaders);
}
}
// 3. Map data to headers
var row = headers.map(function(header) {
// 3. Map data to the (potentially updated) existingHeaders
var row = existingHeaders.map(function(header) {
var val = data[header];
if (val === undefined || val === null) return "N/A";
// If value is an object or array, stringify it to avoid [Ljava.lang.Object]
if (typeof val === 'object') return JSON.stringify(val);
return val;
});
sheet.appendRow(row);
return ContentService.createTextOutput(JSON.stringify({ status: "success", sheet: sheet.getName() }))
.setMimeType(ContentService.MimeType.JSON);
return ContentService.createTextOutput(JSON.stringify({
status: "success",
sheet: sheet.getName(),
addedColumns: missingHeaders ? missingHeaders.length : 0
})).setMimeType(ContentService.MimeType.JSON);
} catch (error) {
return ContentService.createTextOutput(JSON.stringify({ status: "error", message: error.toString() }))