【发布时间】:2016-06-15 18:09:22
【问题描述】:
我正在编写一个 Web 应用程序,并希望将更新实时写回服务器。在客户端,我运行此功能并捕获每个输入。输入当前都是复选框。问题是我可以比脚本更快地检查这些框,因此最终会得到意想不到的结果。所以我需要在选择之间减慢用户的速度,或者确保每个服务器调用在下一个调用开始之前完成。我该怎么做?
这是客户端脚本部分的开始。还有其他功能,例如成功和失败处理程序。
<script>
$("form").change(function(e) {
if (e.target.type && e.target.type === 'checkbox') {
//A checkbox was changed, so act on it
var name = e.target.value.substr(0,e.target.value.lastIndexOf("_"));
var position = e.target.value.substr(e.target.value.lastIndexOf("_") + 1);
var passArray = [name, position];
if (e.target.checked) {
//Add the value to the person's Assigned Position
google.script.run.withSuccessHandler(editChangeReturnedFromServer).withFailureHandler(editFailed).setWSAssignedPosition(passArray);
} else {
//Remove the value from the peson's Assigned Position
google.script.run.withSuccessHandler(editChangeReturnedFromServer).withFailureHandler(editFailed).clearWSAssignedPosition(passArray);
}
return;
}
...(other functions)...
</script>
如果用户快速选中复选框,则对服务器端 setWSAssignedPosition() 函数的调用似乎会交叉甚至重复,因为所有正确的值都没有添加到电子表格中,或者添加了相同的多个副本。如果我在google.script.run... 行之前使用alert('I am changing that'); 减慢最终用户的速度,那么一切正常。实际上,我看到电子表格中的一行被后来出现的条目替换。但这很烦人,很快就变得烦人了。
我对提交或应用按钮不感兴趣。我真的希望这能即时发生。
服务端函数是:
//Set the Assigned Position for the person passed to the function
function setWSAssignedPosition(passedArray){
var name = passedArray[0];
var position = passedArray[1];
//Get the entries from the filled out Position Requests in the appropriate sheet
var validWSRequests = getValidWSRequests();
var foundWSRequest = false;
for (i in validWSRequests) {
if (validWSRequests[i].participantName === name){
ws_sheet.getRange(validWSRequests[i].sheetrow + 2 , ws_headers[0].indexOf("Assigned Position") + 1).setValue(position);
foundWSRequest = true;
break;
}
}
if (!foundWSRequest) {
var WSOthersAssigned = getRowsData(ws_norequests_sheet);
var WSOthersAssigned_headers = ws_norequests_sheet.getRange(1, 1, 1, ws_norequests_sheet.getLastColumn()).getValues();
//Get the first empty row on the sheet for those who didn't fill out the form in case we need it.
var firstEmptyRow = getFirstEmptyRowWholeRow(ws_norequests_sheet);
if (WSOthersAssigned.length < 1){
//No records exist at all, so add the first
ws_norequests_sheet.getRange(firstEmptyRow, WSOthersAssigned_headers[0].indexOf("Assigned Position") + 1).setValue(position);
ws_norequests_sheet.getRange(firstEmptyRow, WSOthersAssigned_headers[0].indexOf("PARTICIPANT NAME") + 1).setValue(name);
}else {
for (i in WSOthersAssigned){
var seeme = i;
if (WSOthersAssigned[i].participantName === name) {
//Found a record so edit it
ws_norequests_sheet.getRange(WSOthersAssigned[i].sheetrow + 2 , WSOthersAssigned_headers[0].indexOf("Assigned Position") + 1).setValue(position);
break;
} else {
//No record found, so append it
ws_norequests_sheet.getRange(firstEmptyRow, WSOthersAssigned_headers[0].indexOf("Assigned Position") + 1).setValue(position);
ws_norequests_sheet.getRange(firstEmptyRow, WSOthersAssigned_headers[0].indexOf("PARTICIPANT NAME") + 1).setValue(name);
break;
}
}
}
}
// This didn't help
// SpreadsheetApp.flush();
return [name, true, position];
}
ws_norequests_sheet 和 ws_norequests_sheet 是全局定义的,以便在存储这些项目的电子表格中获取适当的工作表。根据初始来源,数据保存在两张纸中的一张中。我目前正在测试所有数据进入第二张表的位置,因为这些复选框都堆叠在一起,因此可以快速访问
【问题讨论】: