google-apps-script - 将一个数组从客户端发送到服务器端,并使用 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>
解决方案
推荐阅读
- python-3.x - 在 Jupyter Notebook 中使用 argparse 模块的正确方法
- rest - 如何创建 API 端点以自动生成图像并将其索引到 Google?
- r - 使用 summary_row() 值来计算 {gt} 包的组百分比?
- python - Tkinter——root如何连接到Frame
- r - 为什么我的 PCA 和 MDS 图显示彼此的镜像,而不是显示相同的结果?
- json - 如何获取 Redis JSON 数据
- javascript - 通过使用提示向 javascript DateTime 对象添加秒数
- algorithm - 体素算法中的角点数组是什么?
- canvas - 将 Konva 缩放到可见区域的中心
- graph - Coldfusion:水平条形图上的 CFChart 格式标签