GAS库和ScriptProperties:将属性保存为包括脚本(不是库)的方法 [英] GAS Libraries and ScriptProperties: Way to save property to including script (not library)

查看:61
本文介绍了GAS库和ScriptProperties:将属性保存为包括脚本(不是库)的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我所有的代码外包给一个库,但是我正在使用ScriptProperty为每个使用的电子表格设置和获取持久的全局变量(例如"mode":"edit"或"data")代码.

I want to outsource all of my code to a library, however I am using ScriptProperties to set and get persistent, global variables (like "mode" : "edit" or "data") for every spreadsheet that uses the code.

根据规范,无法将库写入托管脚本ScriptProperties:

According to the specifications it is not possible to have a library write to the hosting scripts ScriptProperties: https://developers.google.com/apps-script/guide_libraries#scoping

脚本属性(**):在库中创建脚本时,所有实例(包括脚本)都可以看到同一实例.

Script Properties(**): The same instance is visible to all including scripts when created in the library.

**这意味着库具有自己的资源/功能实例,并且所有使用该库的脚本都共享并有权访问同一实例.

** This means that library has its own instance of the resource/feature and that all scripts that use the library share and have access to that same instance.

因此,这使得不可能使用一个代码库为每个电子表格设置全局变量.

So this makes it impossible to set globals for every spreadsheet using one single code library.

结合ScriptProperties,有没有针对图书馆这种缺点的解决方法或解决方案?

Is there a workaround or a solution to this shortcoming of Libraries in combination with ScriptProperties?

非常感谢

马里奥(Mario)

推荐答案

或者编码更优雅,更可重用...

Or perhaps coded a little more elegantly, and more reusable...

还包括一些功能,这些功能在多个用户使用电子表格时非常有用,并且每个功能都需要有自己的设置.

Also included functions which are useful when a spreadsheet is used by more than one user, and they each need to have their own settings.

/**
 *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);
}

这篇关于GAS库和ScriptProperties:将属性保存为包括脚本(不是库)的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆