将数组从客户端发送到服务器端,并使用PropertiesService将其返回给客户端

时间:2018-07-26 04:20:08

标签: google-apps-script

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

我正在尝试使用PropertiesService从Web应用程序中获取一个数组,将其保存,然后将其返回给Web应用程序,以便在刷新页面时,所有列表项都位于它们的最大位置。最近的位置。

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

如何获取PropertiesService来保存列表并在刷新页面时将其返回?

如何使客户端和服务器端彼此“通信”?

正如迭戈指出的那样,“您所定义的位置函数不与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);
}

FROM INDEX.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>

0 个答案:

没有答案