This sounds interesting and challenging, as not being able to access sheets lets you with just one part of the solution.
With access to the sheets, I would suggest you do the following:
Create a Webhook that will receive Workbook and SheetId with the row information being modified and then you could route in one scenario what to do depending on the workbook and sheet case.
but how to get a webhook fired for each sheet row CRUD operation?
You will need to add functions to each Google sheet to send the row operation to your webhook.
you can use the Google Apps Script’s onChange
trigger combined with the UrlFetchApp
service to send webhook requests. Here’s an example code snippet that sets up the desired functionality:
function sendWebhook(eventType, range, sheetName, editedBy) {
var webhookUrl = “YOUR_WEBHOOK_URL”;
var payload = {
eventType: eventType,
range: range,
sheetName: sheetName,
editedBy: editedBy,
timestamp: new Date().toString()
};
var options = {
method: “POST”,
contentType: “application/json”,
payload: JSON.stringify(payload)
};
UrlFetchApp.fetch(webhookUrl, options);
}
function onChange(e) {
var eventType;
var range;
var sheetName;
var editedBy;
if (e.changeType == “INSERT_ROW”) {
eventType = “row_added”;
range = e.source.getActiveRange().getA1Notation();
} else if (e.changeType == “REMOVE_ROW”) {
eventType = “row_deleted”;
range = e.source.getActiveRange().getA1Notation();
} else if (e.changeType == “EDIT”) {
eventType = “row_edited”;
range = e.source.getActiveRange().getA1Notation();
editedBy = e.source.getActiveSheet().getRange(e.range.rowStart, 1).getValue();
}
sheetName = e.source.getSheetName();
sendWebhook(eventType, range, sheetName, editedBy);
}
Make sure to replace "YOUR_WEBHOOK_URL"
with the actual URL you want to send the webhook requests to. The sendWebhook()
function formats the payload data and sends a POST request to the provided URL.
To set up the trigger, follow these steps:
- Open your Google Sheets document.
- Click on the “Extensions” menu.
- Select “Apps Script”.
- Replace the default code with the above code.
- Save the project.
Now, whenever a row is added, deleted, or edited in any worksheet under your Google account, the onChange()
function will be triggered and a webhook request will be sent to the specified URL.