首页 > 解决方案 > 将一个数组从客户端发送到服务器端,并使用 PropertiesService 将其返回到客户端

问题描述

我希望脚本能够在刷新页面时保留每个列表项的最新位置(即如果列表 1 以 A、B、C 开头,我将其更改为 B、A、C 并刷新页面,它保持为 B、A、C。)。

我正在尝试使用PropertiesService从网络应用程序中获取一个数组,将其保存,然后将其返回到网络应用程序,这样当我刷新页面时,所有列表项都处于它们的最新位置。

您可以在此处查看我正在处理的上一个问题。(我开始了一个新线程,因为这是相关的,但这是一个单独的问题。)离开那个并在那里提供的评论/答案,我在下面的内容没有按预期工作。我从控制台得到的一个错误是Uncaught ReferenceError: saveList is not defined at HTMLButtonElement.onclick.

如何PropertiesService在刷新页面时保存列表并返回它?

如何让客户端和服务器端相互“通信”?

正如 Diego 指出的那样,“您拥有的定义 position 的函数不会与 对话saveList(),因此您不会保存任何东西。” 我只是不确定如何做到这一点。

如何positions从 index.html 获取数组到 code.gs 并返回到 index.html?

来自 CODE.GS:

function doGet() {
  return HtmlService.createHtmlOutputFromFile('index');
}

function savePositions(myProperty, positions) {
 PropertiesService.getUserProperties().setProperty("myProperty", positions);
}

从索引.HTML:

<script>
      $(function() {
        google.script.run.withSuccessHandler(buildOptionsList)
            .getTeamArray();    
      });


      function buildOptionsList(uniqueArray) {
        var div = document.getElementById('myList');
          for (var i = 0; i < uniqueArray.length; i++) {
            var ul = document.createElement('ul');
            var li = document.createElement('li');
            var cLass = li.setAttribute('class','ui-state-default');
            var iD = li.setAttribute('id',uniqueArray[i]);


        li.appendChild(document.createTextNode(uniqueArray[i]));
        div.appendChild(ul);
        div.appendChild(li);
          }
       }
</script>

<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

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

  JSON.parse("myProperty");
} 
}
    })
   });

</script>

  </head>

  <body>

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

标签: google-apps-script

解决方案


推荐阅读