【问题标题】:Where should I be calling the getUserProperties function?我应该在哪里调用 getUserProperties 函数?
【发布时间】:2018-07-25 03:26:58
【问题描述】:

我正在尝试创建一个按钮,该按钮在单击时“保存”来自已连接排序的多个列表的位置。我希望脚本能够在刷新页面时保留每个列表项的最新位置(,即如果列表 1 以 A、B、C 开头,我将其更改为 B、A、C 并刷新页面,它保持为 B、A、C。)。

我试图通过从 html 文档(作为 Web 应用程序发布)创建一个数组来完成此操作,使用 getUserProperities 函数保存它,然后在我下次打开 Web 应用程序时返回它。

我可以毫无问题地记录职位,但我无法确定在哪里运行 getUserProperities 函数,或者我可能正在处理什么其他问题。

在 Web 应用程序的控制台中,我收到此错误 Uncaught TypeError: Cannot read property 'getUserProperties' of undefined at saveList (userCodeAppPanel:4) at HTMLButtonElement.onclick (userCodeAppPanel:1)

我应该在哪里调用getUserProperties 函数?来自code.gsindex.html?我一直在研究HERE提供的示例。

我的代码的相关部分来自index.html,如下所示。

<script>
  $( function() {
    $( "#myList, #flight1a, #flight1b, #flight2a, #flight2b, #flight3a, #flight3b, #sortable2" ).sortable({
      connectWith: ".connectedSortable",
      update: function(event, ui) {
        var changedList = this.id;
        var order = $(this).sortable('toArray');
        var positions = order.join(';');

        console.log({
        id: changedList,
        positions: positions

        });
      }
    })
   });

</script>

 <script>
    function saveList() {

      google.script.run.PropertiesService.getUserProperties().setProperty('myProperty', JSON.stringify(positions));

      var returnedObj = PropertiesService.getUserProperties("myProperty");
          returnedObj = JSON.parse(returnedObj);
      }  
</script>

  </head>

  <body>

<button onclick="saveList()">Click me</button>

【问题讨论】:

  • Apps Script natives,如 PropertiesService 仅在 Apps Script 服务器环境的上下文中可用。您需要创建从客户端代码调用的服务器端函数foo(...)。在您的服务器功能中,您将设置/获取相关项目。
  • 不要忘记查看有关客户端服务器通信的 Apps 脚本指南 - 它们会非常有帮助。
  • 我确实浏览了该指南,这就是为什么我认为我需要从客户端调用它并使用 google.script.run,但我不确定如何获得这两个方面来回通信和共享数据。我不确定服务器端函数需要返回什么才能使 PropertiesService 实际运行,所以我尝试直接从客户端执行此操作。这澄清了一些。

标签: javascript jquery jquery-ui google-apps-script


【解决方案1】:

只能通过 Google Apps 脚本访问属性服务,因此必须在您的 *.gs 文件之一中调用它。要执行服务器端功能,需要使用google.script.run(还有一个guide available)。

例如,在您的code.gs 文件中添加此函数:

function savePositions(propertyName, value) {
  PropertiesService.getUserProperties().setProperty(propertyName, value);
}

然后,在您的index.html 文件中将saveList() 修改为:

function saveList() {
  google.script.run.savePositions("myProperty", JSON.stringify(positions));
}  

【讨论】:

  • 我确实知道该指南,这就是我使用 google.script.run 的原因,但我不知道服务器端函数需要返回什么(不确定如何获取 @987654330 @ 从客户端到服务器端)以使 PropertiesService 实际运行,因此我尝试直接从客户端执行此操作。我明天试试这个(现在是午夜,我需要在 6 小时内起床工作。感谢您的帮助!
  • @NateDavis 服务器端函数不需要返回任何内容,但您可以使用各种withFailureHandler(function)/withSuccessHandler(function)/withUserObject(object) 方法来返回。
  • 我按照你的建议应用了这个函数,但我仍然没有得到我想要的结果。最好开一个新线程吗?我在控制台中收到此错误:“未捕获的 ReferenceError:未在 HTMLButtonElement.onclick (userCodeAppPanel:1) 的 saveList (userCodeAppPanel:3) 中定义位置。”
  • 当我在 index.html 中更改 saveList 函数的位置时,仍然出现错误:未捕获的 ReferenceError: saveList is not defined at HTMLButtonElement.onclick (userCodeAppPanel:1)
  • @NateDavis 是的,这是与您的 jQuery 相关的单独问题。您拥有的定义 position 的函数不与 saveList() 对话,因此您没有保存任何内容。
【解决方案2】:

google.script.run 可用于调用服务器端函数,而不是直接调用属性服务。

更多详情请见https://developers.google.com/apps-script/guides/html/reference/run

【讨论】:

  • 好的,这有助于了解(并且易于理解!)。我确实浏览了该指南,但对于对此相对较新的人来说,它还不如使用另一种语言。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
  • 2018-10-04
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多