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

编程入门 行业动态 更新时间:2024-10-18 20:30:46
本文介绍了我应该在哪里调用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 函数,然后在下次打开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函数?

本文发布于:2023-11-14 00:26:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585715.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   getUserProperties

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!