在其他文档上设置属性

Set Properties on other documents

我有一个用于创建其他电子表格文档的电子表格脚本。我想在这些 object 上设置自定义属性,最好使用属性 object.

目前:

var props = PropertiesService.getDocumentProperties()

只会 return 与当前活动文档关联的属性 object。有没有办法访问外部文档的属性 object?

我目前唯一的 work-around 是通过向其添加注释来使 header 单元格超载:

dataRange.setNote("foo=bar")

Is there a way to access the Properties object of external documents?

没有。 属性不能在脚本之间共享。ref 脚本只能修改自己的属性(无论是 ScriptProperties、UserProperties 还是 DocumentProperties)。

但是,您可以通过函数公开脚本属性 - 例如,一个库可以向多个脚本提供 API,以便它们可以在库中读取和写入一组公共属性。有关详细信息,请参阅 How to pass parameter(s) to timed trigger function in library script

您可以通过使用库的属性作为主脚本和从传播sheet 脚本之间的通信来适应您的情况,或者您可以使用从 sheet ID 作为属性的键对于存储在库属性中的从 sheets。

您提出的问题是 XY Problem 的一个示例,因此您对 PropertiesService 的关注可能会排除解决您的实际问题。如果是这种情况,您可能希望提出另一个关注问题的问题,而不是可能的解决方案。

正确答案是:不可能。

解释(来自getDocumentProperties,粗体是我的):

Gets a property store (for this script only) that all users can access within the open document, spreadsheet, or form. It is only available if the script is published and executing as an add-on or if it is bound to a Google file type. When document properties are not available this method returns null. Document properties created by a script are not accessible outside that script, even by other scripts accessing the same document.

因此其他脚本无法访问文档属性,这没关系,但这不是唯一的限制:它们也只能在您实际使用存储它们的文档时访问。


TLDR:您只能获取活动文档的文档属性。