I've written an Apps Script function that sets values to non-contiguous cells in multiple sheets. This particular function is tied to a custom sheet operation that the user can call from a custom menu tab. Here is the sheet that the Apps Script code is bound to:
https://docs.google.com/spreadsheets/d/1Yr7wBEhPdLF4m9OFCbljfMz4Tc250HgF1u7Dl5QyAZw/edit?usp=sharing
The function in question is:
In essence, there is a table in sheet1 that the user can choose to load data from. Each row of the table corresponds to the appropriate section to the right of the grey columns in sheet2 and sheet3 to load on the left. Whatever was on the left will be pushed onto the stack on the right.
The problem my sheet exposes is the undo capability of the sheet. What I want is for the sheet to be "undoable" atomically in one go with ctrl+z or undo if the function is run. However what I'm seeing is that the sheet requires more than one undo to fully revert the change. If I add another sheet, the number of undos required increases. I'm not sure why this is happening and I have not observed this behavior before in such similar functions (similar in complexity) that I've written for my sheet. I think this is an important point as this is the first time that I've observed this behavior without being able to refactor my code such that an atomic undo fully reverts the change. This leads me to believe that there is something in my code structure that is preventing this from happening.
How can I fix this issue so that when the function is run, it can be fully reverted with only one undo?