I’m facing an issue where my Apps Script function works fine with manual edits, but it doesn’t respond when data is updated via API calls from Zapier.
Here’s how my setup works: I use Zapier to fetch data from Stripe and populate one sheet, while my script is intended to process that data and make changes to another sheet. The script triggers perfectly with manual inputs, but it stays dormant when new data arrives via the API.
I’ve tried implementing time-based triggers, but I couldn’t get them to work as needed. Has anyone experienced this issue before? It seems like changes from the API aren’t activating the onEdit trigger, and I could use guidance on the best solutions available.
function processData(e) {
var mainTab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MainData");
var apiTab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("APIData");
var mainData = mainTab.getDataRange().getValues();
var firstCol = 1;
var changedRow = e.range.getRow();
var changedColumn = e.range.getColumn();
var newValue = apiTab.getRange(changedRow, firstCol).getValue();
var timestamp = apiTab.getRange(changedRow, firstCol + 1).getValue();
var foundMatch = false;
var insertRow;
if (changedColumn == firstCol) {
for (var j = 0; j < mainData.length; j++) {
if (mainData[j][firstCol - 1] == newValue) {
foundMatch = true;
var currentCount = mainTab.getRange(j + 1, firstCol + 1).getValue() || 0;
mainTab.getRange(j + 1, firstCol + 1).setValue(currentCount + 1);
break;
}
}
if (!foundMatch) {
insertRow = mainData.length + 1;
mainTab.getRange(insertRow, firstCol).setValue(newValue);
if (timestamp >= new Date("2024-01-01T00:00:00.000Z") && timestamp <= new Date("2024-01-07T18:00:00.000Z")) {
mainTab.getRange(insertRow, firstCol + 1).setValue(1);
} else if (timestamp > new Date("2024-01-07T18:00:01.000Z") && timestamp <= new Date("2024-01-14T18:00:00.000Z")) {
mainTab.getRange(insertRow, firstCol + 1).setValue(2);
}
}
}
}
function onEdit(e) {
var currentSheet = e.source.getActiveSheet();
if (currentSheet.getName() === "APIData") {
processData(e);
}
}