Google Apps 脚本中的可原子撤消功能

Atomically undo-able function in Google Apps Script

我在 Google Apps 脚本 中创建了一个函数,用于 Google 电子表格。该函数逐步转换一个范围。

如果用户尝试 undo,它只会撤消函数的一个步骤。

我希望它表现得原子,所以使用undo 会撤消整个事情。有办法吗?

对于上下文,代码片段:

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|

我希望在 一次 单击后撤消 return 到初始状态,而不是 三次

无法控制撤消行为。 Google 床单从一开始就有这个问题,但仍然不能正常工作。

您需要编写自己的撤消命令,并且它不能挂接到撤消事件。