Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 使用服务器处理程序中的缓存_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 使用服务器处理程序中的缓存

Google apps script 使用服务器处理程序中的缓存,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我正在尝试将一些用户输入从电子表格上的Google应用程序脚本表单保存到私有缓存中 下面是一个测试脚本: var cache = CacheService.getPrivateCache(); function onLoad() { var sheet = SpreadsheetApp.getActiveSpreadsheet(), entries = [{ name : "Show form", functionName : "showForm

我正在尝试将一些用户输入从电子表格上的Google应用程序脚本表单保存到私有缓存中

下面是一个测试脚本:

var cache = CacheService.getPrivateCache();

function onLoad() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet(),
      entries = [{
        name : "Show form",
        functionName : "showForm"
      }];
  sheet.addMenu("Test Menu", entries);
}

function showForm() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(),
      app = UiApp.createApplication(),
      setButton = app.createButton("Set"),
      setHandler = app.createServerClickHandler('setTest');

  setButton.addClickHandler(setHandler);
  app.add(setButton);
  spreadsheet.show(app);
}

function setTest(event) {
  cache.put("test", "test", 7200);

  Browser.msgBox("Test was set: " + cache.get("test") + ". Use the getTest cell formula to test the cache.");
}

function getTest() {
  var result = cache.get("test");

  return result;
}
单击菜单按钮后,将显示一个表单,并在服务器处理程序中设置缓存值。然后我尝试使用单元格中的=getTest()从缓存中获取值。我希望缓存返回值“test”,但它似乎返回null

我从这里开始这个问题:

我还发现了另一个类似的例子:

试图将表单上的一些用户输入保存到缓存中,并在以后能够从另一个函数访问它


有什么建议吗?

自定义功能的作用域有限,只能访问一些选定的服务。你会读到更多关于它们的局限性

缓存有点棘手——缓存是按“脚本范围”进行的。因此,当首先从服务器处理程序脚本范围访问缓存时,缓存将作为UiApp运行,UiApp具有更多权限,这与自定义函数作为脚本范围运行不同。因此,缓存不会在这两个作用域之间共享

您可以从其他自定义函数访问在自定义函数中设置的缓存项,但此缓存不能跨越这些边界

从理论上讲,您可以将其存储在ScripProperties中,但这可能非常笨拙,而且可能会滥用这些功能,这是所有用户共享的

ScriptProperties.setProperty("test", "test")


如果您能更深入地解释您的用例,也许我们可以提供一些替代解决方案

消息框是否显示了正确的值?如果您谈论的是弹出的msgBox,它确认缓存值确实是从setTest函数设置的,那么是的,它显示了。用例是弹出一个表单,供用户输入一些凭据,保存它们,然后让自定义函数使用这些凭据进行REST调用。啊。您可能需要将其传递到自定义函数中。虽然我知道,对于敏感数据,可能不太安全。或者,您可以使用自定义菜单按需将数据设置到单元格中。
var result = ScriptProperties.getProperty("test");