【发布时间】:2013-08-25 19:53:45
【问题描述】:
目前,Spreadsheet/Sheet/Range 类中没有用于 Google Apps 脚本的 undo() 函数。在 Issue Tracker 上打开了几个问题,我现在只能找到一个(我不知道 Triaged 是什么意思):here。
有人建议使用 DriveApp 和修订历史记录的解决方法,但我环顾四周并没有发现任何东西(也许它被埋没了?)。在任何情况下,undo() 函数对于许多不同的操作都是非常必要的。我只能想到一种解决方法,但我无法让它工作(数据存储的方式,我什至不知道它是否可能)。这是一些伪 -
function onOpen () {
// Get all values in the sheet(s)
// Stringify this/each (matrix) using JSON.stringify
// Store this/each stringified value as a Script or User property (character limits, ignore for now)
}
function onEdit () {
// Get value of edited cell
// Compare to some value (restriction, desired value, etc.)
// If value is not what you want/expected, then:
// -----> get the stringified value and parse it back into an object (matrix)
// -----> get the old data of the current cell location (column, row)
// -----> replace current cell value with the old data
// -----> notifications, coloring cell, etc, whatever else you want
// If the value IS what you expected, then:
// -----> update the 'undoData' by getting all values and re-stringifying them
// and storing them as a new Script/User property
}
基本上,当电子表格打开时,将所有值存储为脚本/用户属性,并且仅在满足某些单元格条件(打开)时才引用它们。当您要撤消时,获取存储在当前单元格位置的旧数据,并将当前单元格的值替换为旧数据。如果不需要撤消该值,则更新存储的数据以反映对电子表格所做的更改。
到目前为止,我的代码已经失败,我认为这是因为当对象被字符串化和存储时嵌套数组结构丢失了(例如,它没有正确解析)。如果有人写过这种功能,请分享。否则,有关如何编写此内容的建议会很有帮助。
编辑:这些文档非常静态。行/列的数量不会改变,数据的位置也不会改变。如果可能的话,为临时修订历史实现 get-all-data/store-all-data 类型的函数实际上会满足我的需求。
【问题讨论】:
-
可以通过 Drive API 访问修订,但不能通过 Apps 脚本。撤消似乎因协作而变得复杂。假设您向字段写入值,而您正在与之协作的人删除了该行。您的撤消是否应该恢复该行以写入旧值?只是一个看起来很混乱的例子。回顾修订历史在这里似乎是安全/保守的,尽管更手动。
-
我将在问题正文中指定这不是协作文档,并且不会删除/编辑行。只能编辑一列,因为文档的其余部分使用
importRange函数来填充值。我知道协作撤消会很复杂。但是,我对如何将 Drive API 与 Google Apps Script 一起使用一无所知。我浏览了所有 GAS 文档,但没有看到任何关于修订历史的信息。
标签: google-apps-script google-sheets