I’m building a web app using Google Apps Script that displays data from my spreadsheet. Users should be able to edit certain fields directly in the web interface, and these changes need to be saved back to the original Google Sheet.
The problem is that when users make edits through the web app, the data doesn’t get updated in the spreadsheet. I’m using a doPost function to handle the updates, but something isn’t working correctly.
Here’s my current code:
function doGet() {
var targetSheet = "MainData";
var worksheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(targetSheet);
var allData = worksheet.getDataRange().getValues();
var currentDate = new Date();
currentDate.setHours(0, 0, 0, 0);
var matchingRows = allData.filter((record, idx) => idx !== 0 && record[3] === "pending");
matchingRows.sort((x, y) => new Date(y[5]) - new Date(x[5]));
var htmlContent = `
<h1>Data Management</h1>
<table border="1" style="width: 100%; border-collapse: collapse;">
<tr>
<th>Contact</th>
<th>Status</th>
<th>Notes</th>
<th>Active</th>
</tr>
`;
matchingRows.forEach((record, idx) => {
var contactInfo = record[1].toString();
var isActive = record[15] === true;
htmlContent += `
<tr>
<td>${contactInfo}</td>
<td contenteditable='true' onBlur='modifyData(${idx + 2}, 3, this.innerText)'>${record[2]}</td>
<td contenteditable='true' onBlur='modifyData(${idx + 2}, 16, this.innerText)'>${record[14]}</td>
<td><input type='checkbox' ${isActive ? "checked" : ""} onclick='modifyData(${idx + 2}, 16, this.checked)'></td>
</tr>
`;
});
htmlContent += `</table>
<script>
function modifyData(rowNum, colNum, newValue) {
fetch(ScriptApp.getService().getUrl(), {
method: 'POST',
body: JSON.stringify({ rowNum, colNum, newValue }),
headers: { 'Content-Type': 'application/json' }
});
}
</script>`;
return HtmlService.createHtmlOutput(htmlContent);
}
function doPost(request) {
try {
var requestData = JSON.parse(request.postData.contents);
var worksheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MainData");
if (requestData.rowNum && requestData.colNum !== undefined) {
worksheet.getRange(requestData.rowNum, requestData.colNum).setValue(requestData.newValue);
return HtmlService.createHtmlOutput("Update successful");
}
} catch (err) {
Logger.log("Update failed: " + err);
return HtmlService.createHtmlOutput("Update failed");
}
}
What am I missing to make the data updates work properly?