我在Google Apps 脚本中创建了一个函数,以便在Google 电子表格中使用。该函数逐步转换范围。
如果用户尝试撤消
,它只会撤消该函数的其中一个步骤。
我希望它能够原子地运行,因此使用撤消
可以撤消整个事情。有办法做到这一点吗?
对于上下文,代码片段:
function onOpen() {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [{name: 'Run myFunction', functionName: 'myFunction'}];
spreadsheet.addMenu('Stuff', menuItems);
}
function myFunction(range) {
range = range || SpreadsheetApp.getActiveRange();
forRowInRange(range, replaceValues) ; // <= This should behave atomically!
}
function forRowInRange(range, fn) {
// ...split range into sub-ranges for each row...
// ...invoke fn on each sub-range...
}
function replaceValues(range) {
// ...replace 1s with Xs, and 0s with spaces
}
现有的和期望的行为
|0|1|0|
- |1|0|1| Initial state
/ |0|1|0|
|
| |
| V
|
| | |X| |
| |X| |X| After function is invoked
| | |X| |
|
| |
| V
|
| | |X| |
| |X| |X| After undo is pressed once
| |0|1|0|
|
| |
| V
|
| | |X| |
| |1|0|1| After undo is pressed twice
| |0|1|0|
|
| |
| V
|
\ |0|1|0|
-> |1|0|1| After undo is pressed three times (or ideally, ONCE)
|0|1|0|
我希望撤消在单击一次后返回到初始状态,而不是三次。
最佳答案
无法控制撤消行为。 Google 表格从一开始就存在这个问题,但仍然无法正常工作。
您需要编写自己的撤消命令,并且它不能与撤消事件 Hook 。
关于javascript - Google Apps 脚本中的原子可撤消功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37497023/