【问题标题】:Get data from localStorage (google script)从 localStorage 获取数据(谷歌脚本)
【发布时间】:2017-06-20 14:23:28
【问题描述】:

在我的谷歌电子表格应用程序中,我将一些参数保存在 localStorage 中的一个函数中。在另一个函数中,我需要检索它们。当然,我可以创建无模式对话框或侧边栏,什么时候是我的 html 代码和 js,但是第二个功能只刷新当前工作表,所以我不需要 UI。我也知道,服务器端无权访问 localStorage。 这是我的抽象代码:

server.gs:

function menuItemClicked()
{
    // when we click on item in addon menu, this called
    // and load 'client.html'
}

function refreshCurrentSheet(params)
{
    // called by 'client.html'
    // do something else with params...
}

client.html:

<script type="text/javascript">
    google.script.run.refreshCurrentSheet( localStorage.getItem('var') );
</script>

我没有在 API 中找到答案,该怎么做(

问题:如何在没有可见 UI 的情况下加载 html 并执行 js?

附:将这些参数保存在服务器端的缓存中并不是一个解决方案,因为我经常在客户端使用和更改它们

【问题讨论】:

  • 如果你根本不需要 UI,为什么不使用 Properties Service 来持久化数据呢?也许我误解了你的问题
  • 我相信 Google Apps Script 是基于 Javascript 1.6 的。它可以访问本地存储,所以我的猜测是 Google Apps 脚本也是如此。如果没有,希望有人会纠正我。这是reference。但即使没有,我认为使用 PropertyService 没有任何不利之处,您可以使用 google.script.run 访问任何脚本
  • localStorage 属于浏览器中的窗口对象。 GAS 是基于 JS 的,但是在 Google 服务器上运行在不同的环境中,这意味着全局对象是不同的。
  • @AntonDementiev,真的,我不知道属性服务。如果它没有像缓存服务这样的时间限制,那就是解决问题的方法。谢谢
  • 我没有删除我给你的原始答案。如果您想长期存储数据,那么 Properties Service 将是您的最佳选择。我之所以推荐缓存服务而不是属性是因为我认为您想在多个函数调用之间保存数据。

标签: javascript google-apps-script google-sheets


【解决方案1】:

您可以使用属性服务或缓存服务。两者都提供 localStorage 功能,允许您存储键值对(仅限字符串类型)。如果你想在函数调用之间持久化数据,缓存可能是最好的选择

更多关于缓存服务 https://developers.google.com/apps-script/reference/cache/

更多关于物业服务 https://developers.google.com/apps-script/reference/properties/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    相关资源
    最近更新 更多