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

标签 javascript google-apps-script google-sheets atomic undo

我在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/

相关文章:

javascript - 如何在我的代码所在的 js 文件之外查找具有 getElementById 的元素?

google-apps-script - 如何使用 GAS 获取文档中的 URL 并将其替换为 URL 指向的图像?

google-apps-script - 使用 Apps Script 在 Google Drive 中创建 zip 文件

google-apps-script - 填写单元格时的自动时间戳

javascript - Google Apps 脚本库不再链接

javascript - 在 android 的 webview 中嵌入一段 HTML 代码或 Javascript 脚本

javascript - vue,发射与传递函数作为 Prop

javascript - laravel Elixir 映射类似文件

javascript - 来自日期时间编号的 Google Apps 脚本时间

google-apps-script - 如何修复 Google App 脚本错误异常 : Data storage error (line 1, 文件 "Code")?