我应该在哪里调用getUserProperties函数?

我正在尝试创建一个按钮,在单击时,“保存”连接可排序的多个列表的位置。 我希望脚本能够在刷新页面时保留每个列表项的最新位置( 即如果列表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 ? 我一直在研究这里提供的例子。

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

  $( 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.PropertiesService.getUserProperties().setProperty('myProperty', JSON.stringify(positions)); var returnedObj = PropertiesService.getUserProperties("myProperty"); returnedObj = JSON.parse(returnedObj); }     

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

例如,在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可用于调用服务器端函数,而不是直接调用Properties Service。

有关详细信息,请参阅https://developers.google.com/apps-script/guides/html/reference/run