Google Apps脚本中的持久变量 [英] Persistant variable in Google Apps Script
问题描述
以下内容始终显示0:
var gNumber = 0;
function myTest() {
Browser.msgBox(gNumber);
gNumber++;
}
我当然可以使用ScriptProperties或UserProperties永久存储值.适用于简单值,但除非将其字符串化,否则它不适用于对象.但是对于后者,我无法永久存储打开工作表并允许我通过方法(获取,设置)对其进行操作的对象.
I can, of course, use ScriptProperties or UserProperties to store a value permanently. Fine for simple values but it will not work for an object unless I stringify it. But with the latter, I cannot store permanently an object that opens a sheet and lets me manipulate it via methods (get, set).
我有一个订单电子表格,该表格使用客户"电子表格中的信息.每次检查订单时,都需要打开客户"电子表格并将其数据加载到数组中.有没有一种方法可以永久保留一个对象,这样我就不需要一遍又一遍地打开客户了?
I have an Orders spreadsheet that uses information from the Customers spreadsheet. Everytime I check an order, I need to open the Customers spreadsheet and load its data into an array. Is there a way to keep an object permanently so that I don't need to open Customers over and over again?
永久地说,我的意思是只要与脚本关联的工作表是打开的!
By permanently, I mean as long as the sheet associated with the script is open!
有办法吗?
推荐答案
如果您需要暂时存储变量,请使用 ScriptDb
If you need to store the variables for a temporary amount of time, use CacheService. If you want more persistent storage (similar to a spreadsheet), then you can make use of ScriptDb
当然,还可以使用ScriptProperties/UserProperties,但我更喜欢CacheService而不是ScriptProperties,因为您可以很快达到ScriptProperties的极限.
Of course, ScriptProperties / UserProperties are also there for you to use, but I prefer CacheService over ScriptProperties because you can hit the limit on ScriptProperties pretty quickly.
这篇关于Google Apps脚本中的持久变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!