UserProperties 与 userProperties - 如何保持脚本之间的持久性?
UserProperties vs. userProperties - How can I maintain persistency between scripts?
旧的 UserProperties 为用户维护脚本之间的持久性 - "User Properties are key-value pairs unique to a user. User Properties are scoped per user; any script running under the identity of a user can access User Properties for that user only." (https://developers.google.com/apps-script/reference/properties/user-properties)
但是,新属性服务 userProperties 的行为有所不同 - 它仅与 "The current user of the current script" (https://developers.google.com/apps-script/guides/properties#comparison_of_property_stores) 相关。
我需要在脚本之间保存数据。任何人都知道如何在不将数据保存在外部电子表格或文档中以供脚本检索的情况下完成此操作?
感谢您的帮助。
使用脚本作为彼此的库可以获得很多好处。 documentation gives a good guide about how the scoping of variables and properties just below.
基本上,您可以传入或传出驻留在其自身范围内的库的脚本对象。
脚本 1:
//set a dummy data object
PropertiesService
.getUserProperties()
.setProperty('data', 'i come from script 1');
function script1Properties() {
var up = PropertiesService.getUserProperties();
return up;
}
脚本 2:
//set a dummy data object in this script too.
PropertiesService
.getUserProperties()
.setProperty('data', 'i come from script 2');
function script2() {
var up1 = PropertiesService.getUserProperties();
var up2 = Script1.publishProperties();
Logger.log(up1.getProperty('data'));
Logger.log(up2.getProperty('data'));
debugger
}
将脚本 1 作为库附加到脚本 2。
> Run Script
控制台输出:
[15-01-08 15:55:41:493 GMT] i come from script 1
[15-01-08 15:55:41:501 GMT] i come from script 2
旧的 UserProperties 为用户维护脚本之间的持久性 - "User Properties are key-value pairs unique to a user. User Properties are scoped per user; any script running under the identity of a user can access User Properties for that user only." (https://developers.google.com/apps-script/reference/properties/user-properties)
但是,新属性服务 userProperties 的行为有所不同 - 它仅与 "The current user of the current script" (https://developers.google.com/apps-script/guides/properties#comparison_of_property_stores) 相关。
我需要在脚本之间保存数据。任何人都知道如何在不将数据保存在外部电子表格或文档中以供脚本检索的情况下完成此操作?
感谢您的帮助。
使用脚本作为彼此的库可以获得很多好处。 documentation gives a good guide about how the scoping of variables and properties just below.
基本上,您可以传入或传出驻留在其自身范围内的库的脚本对象。
脚本 1:
//set a dummy data object
PropertiesService
.getUserProperties()
.setProperty('data', 'i come from script 1');
function script1Properties() {
var up = PropertiesService.getUserProperties();
return up;
}
脚本 2:
//set a dummy data object in this script too.
PropertiesService
.getUserProperties()
.setProperty('data', 'i come from script 2');
function script2() {
var up1 = PropertiesService.getUserProperties();
var up2 = Script1.publishProperties();
Logger.log(up1.getProperty('data'));
Logger.log(up2.getProperty('data'));
debugger
}
将脚本 1 作为库附加到脚本 2。
> Run Script
控制台输出:
[15-01-08 15:55:41:493 GMT] i come from script 1
[15-01-08 15:55:41:501 GMT] i come from script 2