我想我所有的代码外包给图书馆,但是我使用ScriptProperties来设置和获取持续性,全局变量(如“模式”:“编辑”或“数据”),为使用该代码的每个电子表格。
根据该规范,不可能有一个图书馆写的托管脚本ScriptProperties: https://developers.google.com/apps-script/guide_libraries#scoping
脚本属性(**):在库中创建时,同一个实例是包括所有脚本可见。
**这意味着,图书馆有自己的资源/功能的实例和使用该库共享和访问同一实例的所有脚本。
因此,这使得它无法设置全局使用一个单一的代码库中的每个电子表格。
有一种解决方法或ScriptProperties这个缺点图书馆的解决方案组合?
感谢所有的最好的
马里奥
或许编码多了几分典雅,更具重用性...
还包括当一个电子表格使用一个以上的用户,其是有用的功能,和他们每个人都需要有自己的设置。
/**
*Returns a script property from the library file to a calling script
* @param {string} key The name of the scriptProperty property to return.
* @return {string} the string value of the key passed
*/
function getLibraryProperty(key) {
return ScriptProperties.getProperty(key);
}
/**
*Returns the appropriate library property for the calling spreadsheet.
* @param {string} key The name of the scriptProperty property to return.
* @return {string} the string value for the key passed
*/
function getLibPropForSpreadsheet(key){
return(ScriptProperties.getProperty(setkey_(key).fullKey));
}
/**
*Returns the appropriate library property for the calling spreadsheet by user
* @param {string} key The name of the scriptProperty property to return.
* @return {Object.<string,string>} Object containing the key, full key, and value
*/
function getLibPropForSSUser(key){
return(ScriptProperties.getProperty(setkey_(key).fullUserKey));
}
/**
*Returns the appropriate library property for the calling spreadsheet.
* @param {string} key The name of the scriptProperty property to return.
* @return {Object.<string,string>} the string value of the key passed
*/
function setLibPropForSpreadsheet(key, keyValue)
{
return(setLibProp_(key, setkey_(key).fullKey, keyValue));
}
function setLibPropForSSUser(key, keyValue)
{
return(setLibProp_(key, setkey_(key).fullUserKey, keyValue));
}
function setLibProp_(key, fullKey, keyValue){
ScriptProperties.setProperty(fullKey,keyValue)
var theResult = new Object();
theResult.value = keyValue;
theResult.key = key;
theResult.fullKey = fullKey;
return(theResult);
}
function setkey_(key){
var theCaller = SpreadsheetApp.getActiveSpreadsheet().getId();
var theUser = Session.getEffectiveUser().getEmail();
var fullUserKey = theCaller + '-' +theUser+ '-' + key;
var fullKey = theCaller + '-' + key;
var theKeys = new Object();
theKeys.fullUserKey = fullUserKey;
theKeys.fullKey = fullKey;
return(theKeys);
}
一个解决方案是一个合格的托管ScriptProperties的一个实例库,即类似的东西
function test() {
var props = ScriptProperties.getProperties();
MyLib.func1(props);
}
MyLib.gs
function func1(props) {
var mode = props["mode"];
// ...
}
如果托管脚本处理多个电子表格,然后可以使用电子表格id作为属性键的一部分,例如,有两个电子表格与IDS XYZ000
和XYZ001
。 托管脚本包含性能的轿跑车
-
XYZ000.mode
= normal
-
XYZ001.mode
= extended
托管脚本需要有效的电子表格ID,并且与特性一起传递给库方法。
一个成熟的解决方案是有一个设置类的电子表格,在主导脚本属性设置类的JSON字符串表示来存储每一个处理电子表格,加载使用电子表格的ID属性电子表格设置,并通过设置实例去图书馆。 下面是一个示例代码。
function Settings(mode) {
this.mode = mode;
};
function setDefaultSettings() {
var ID0 = "XYZ000";
var SettingID0 = new Settings("normal");
ScriptProperties.setProperty(ID0, SettingID0);
}
function test() {
var props = ScriptProperties.getProperty("XYZ000");
MyLib.func1(props);
}
这里是我试图解决这个...
我的所有属性存储在库中,然后使用一个getter和setter函数库,可以很容易的调用电子表格来访问这些值。
使用呼叫电子表格的ID,以便为属性名复合键。 函数返回一个对象与所述fullkey(化合物)中,“裸露”键,并且该值返回给调用者(验证,错误检查等)。
library.gs
二传手
function setLibPropForSpreadsheet(key, keyValue)
{
var theCaller = SpreadsheetApp.getActiveSpreadsheet().getId();
var fullKey = theCaller + '-' + key;
ScriptProperties.setProperty(fullKey, keyValue)
var theResult = new Object();
theResult.value = keyValue;
theResult.key = key;
theResult.fullKey = fullKey;
return(theResult);
}
吸气
function getLibPropForSpreadsheet(key){
var theCaller = SpreadsheetApp.getActiveSpreadsheet().getId();
var fullKey = theCaller + '-' + key;
return(ScriptProperties.getProperty(fullKey));
}
然后你就可以在你的电子表格中调用脚本从而使用这些功能:
spreadsheet.gs
function setIt() {
var test = getData.setLibPropForSpreadsheet("thesetting", "the value")
Logger.log(test)
}
function getIt(){
Logger.log(getData.getLibPropForSpreadSheet("thesetting"))
}
文章来源: GAS Libraries and ScriptProperties: Way to save property to including script (not library)