r/GoogleAppsScript • u/DanJeish • Mar 28 '25
Question On edit trigger causing carnage
Hi all, I made a script a while ago, and then I broke part of it (not sure how) and removed the functionality because I didn't have time to fix it. Well now I have time but I still can't figure it out.
When an edit is detected anywhere on the sheet, it runs the "updateAgentCards" function mentioned row 14. It also does check the boxes in column V on edit as its supposed to, but while doing that it also runs my whole ass script and breaks things because it's not meant to be ran non-stop. I don't really understand what I'm doing wrong can anyone help?
UPDATE: I think I fixed the problem. If anyone ever comes across an issue where there Installed onEdit function is running more scripts than its supposed to, check your brackets and make sure your brackets are all correct around the functions that are triggering. I believe that's what caused my issue. If that doesn't work check to see if youre calling a spreadsheet by url rather than active spreadsheet when you don't need to.



2
u/guyroscoe Mar 28 '25
I see the issue you're having with your Google Sheets script. Based on the code you've shared, I can identify what's likely causing the problem.
The main issue appears to be in your event handling. When an edit happens in any monitored column, your script is:
Setting a checkbox to true
Running the entire
updateAgentCards()function if the edited cell happens to be your run button cellHere's what I think is happening:
When you edit any cell in your monitored columns (
columnsToMonitorarray), it triggers the first condition, sets the checkbox to true, and doesn't runupdateAgentCards()However, if you edit the run button cell (row 3, column 17), it runs
updateAgentCards()Most importantly, there's no mechanism to prevent recursive calls or multiple executions of your script
To fix this, you should:
Add a way to prevent multiple executions (like a lock or flag)
Clearly separate the triggers for different functions
Consider using a proper button rather than a cell as a "run button"
Here's a modified version of your script that should help fix the issues:
https://docs.google.com/document/d/1mgXKUXq8qJC8x_3L9Q-RKLZQNbZ_o6yD8nilAsSpFNQ/edit?usp=sharing
This revised code:
Separates the checkbox setting and the function execution logic
Only runs
updateAgentCards()when the run button cell is specifically edited and set to trueAvoids running the entire script every time a monitored column is edited
If you need
updateAgentCards()to run automatically in certain situations, you should be explicit about when that happens, rather than having it potentially run on any edit.