我应该在哪里调用getUserProperties函数? [英] Where should I be calling the getUserProperties function?

查看:95
本文介绍了我应该在哪里调用getUserProperties函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a button that, upon clicking, "saves" the positions of multiple lists from a connected-sortable. I want the script to be able to retain the most recent position of each list item when the page is refreshed (i.e. If list 1 starts as A,B,C and I change it to B,A,C and refresh the page, it stays as B,A,C.).

我试图通过从html文档创建数组(发布为Web应用程序)来实现此目的,使用 getUserProperities <保存它/ code>函数,然后在下次打开Web应用程序时返回它。

I'm attempting to accomplish this by creating an array from an html document (published as a web app), save it using the getUserProperities function, and then return it the next time I open the web app.

我能够毫无问题地记录位置,但我无法确定在哪里运行 getUserProperities 功能,或者我可能正在处理的其他问题。

I am able to log the positions without issue, but I'm having trouble figuring out where to run the getUserProperities function, or what other issue I may be dealing with.

在Web应用程序的控制台中,我收到此错误未捕获的TypeError:无法在saveList中读取未定义的属性getUserProperties(userCodeAppPanel:4 )在HTMLButtonElement.onclick(userCodeAppPanel:1)

In the console from the web app, I get this error Uncaught TypeError: Cannot read property 'getUserProperties' of undefined at saveList (userCodeAppPanel:4) at HTMLButtonElement.onclick (userCodeAppPanel:1).

我应该在哪里调用 getUserProperties 函数?从 code.gs index.html ?我一直在处理提供的示例 HERE

Where should I be calling the getUserProperties function? From code.gs or index.html? I have been working off the example provided HERE.

我的代码的相关部分来自 index.html

The relevant portions of my code, from index.html are below.

<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>


推荐答案

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

Properties Service can only be accessed through Google Apps Script, so it must be called in one of your *.gs files. To execute a server-side function, you need to use google.script.run (there's also a guide available).

例如,在你的 code.gs 文件中添加这个函数:

For example, in your code.gs file add this function:

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

然后,在 index.html 文件修改 saveList() to:

Then, in your index.html file modify saveList() to:

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

这篇关于我应该在哪里调用getUserProperties函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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