[英]How to fix Google Apps Script Function Running in random sequence
I have a project that will generate a dynamic Listbox from GSheet and it will update the customized form i've created in Google Apps Script.我有一个项目,它将从 GSheet 生成一个动态列表框,它将更新我在 Google Apps 脚本中创建的自定义表单。
The code will get the input from the URL when it is loaded and generate 2 ListBox first before getting the input data to pre-fill the form.代码在加载时会从 URL 中获取输入,并在获取输入数据以预填充表单之前先生成 2 个 ListBox。
So the Sequence should be所以序列应该是
But what I'm getting is whenever I refresh the page, the sequence of function loading is random.但是我得到的是每当我刷新页面时,函数加载的顺序是随机的。 Sometimes it is working fine, sometimes it is loading 3 > 1 > 2 or 2 > 3 > 1 or any other random sequence.
有时它工作正常,有时它正在加载3 > 1 > 2或2 > 3 > 1或任何其他随机序列。
Please suggest anyway that we can make sure the sequence is running as per design.无论如何,请建议我们可以确保序列按设计运行。
Below is the sample code下面是示例代码
Code.gs代码.gs
var SHEET_CAR_URL = 'https://docs.google.com/spreadsheets/d/{sheet1ID}/edit#gid=0';
var SHEET_COLOUR_URL = 'https://docs.google.com/spreadsheets/d/{sheet2ID}/edit#gid=0';
var SHEET_CUSTOMER_URL = 'https://docs.google.com/spreadsheets/d/{sheet2ID}/edit#gid=0';
function doGet(request) {
return HtmlService.createTemplateFromFile('CustomerForm').evaluate().setTitle("Demo Form");
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
function loadCarList(){
//load Cars GSheet
var carsspreadsheet = SpreadsheetApp.openByUrl(SHEET_CAR_URL).getSheetByName('Cars');
numItemCars = carsspreadsheet.getLastRow()-1;// get the number of rows in the sheet
colItemCars = carsspreadsheet.getLastColumn();// get the number of rows in the sheet
listCarsArray = carsspreadsheet.getRange(2,1,numItemCars,colItemCars).getValues();
var listCar = "<option value=''></option>";
for(var i=0; i<numItemCars; i++){
listCar += "<option value='"+listCarsArray[i][0]+"'>"+listCarsArray[i][0]+"</option>";
}
Logger.log(listCar);
return listCar;
}
function loadColourList(){
//load Colour GSheet
var colourspreadsheet = SpreadsheetApp.openByUrl(SHEET_COLOUR_URL).getSheetByName('Colour');
numItemColour = colourspreadsheet.getLastRow()-1;// get the number of rows in the sheet
colItemColour = colourspreadsheet.getLastColumn();// get the number of rows in the sheet
listColourArray = colourspreadsheet.getRange(2,1,numItemColour,colItemColour).getValues();
var listColour = "<option value=''></option>";
for(var i=0; i<numItemColour; i++){
listColour += "<option value='"+listColourArray[i][0]+"'>"+listColourArray[i][0]+"</option>";
}
Logger.log(listColour);
return listColour;
}
function loadCustomer(inputID){
//load Customer GSheet
var customerspreadsheet = SpreadsheetApp.openByUrl(SHEET_CUSTOMER_URL).getSheetByName('Customer');
numItemCust = customerspreadsheet.getLastRow()-1;// get the number of rows in the sheet
colItemCustr = customerspreadsheet.getLastColumn();// get the number of rows in the sheet
listCustArray = customerspreadsheet.getRange(2,1,numItemCust,colItemCustr).getValues();
var custDetails = [];
for(var i=0; i<numItemCust; i++){
var custID = listCustArray[i][0];
var custName = listCustArray[i][1];
var custArea = listCustArray[i][2];
var custCar = listCustArray[i][2];
var custCarColour = listCustArray[i][2];
if(custID == inputID){
custDetails = [custID,custName,custArea,custCar,custCarColour];
}
}
Logger.log(custDetails[0]);
return custDetails;
}
CustomerForm.html CustomerForm.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body onload="myLoadFunction()">
<table>
<tr>
<td> Customer ID : </td>
<td> <input type="text" id="CustID"> </td>
</tr>
<tr>
<td> Customer Name : </td>
<td> <input type="text" id="CustName"></td>
</tr>
<tr>
<td> Customer Area : </td>
<td> <input type="text" id="CustArea"></td>
</tr>
<tr>
<td> Car Brand : </td>
<td><select class='listbox' id='listCar' required></select> </td>
</tr>
<tr>
<td> Car Colour : </td>
<td><select class='listbox' id='listColour' required></select> </td>
</tr>
</table>
</body>
</html>
<script>
function myLoadFunction(){
// Get the URL parameter
google.script.url.getLocation(inputstrings => {
let inputjson = JSON.stringify(inputstrings.parameter);
let inputparameters = JSON.parse(inputjson)
var in_custID = inputparameters.id;
alert('This is the ID '+in_custID);
google.script.run.withSuccessHandler(initializeCar).loadCarList();
google.script.run.withSuccessHandler(initializeColour).loadColourList();
google.script.run.withSuccessHandler(initializeForm).loadCustomer(in_custID);
})
function initializeCar(inputList){
alert('Loading Cars')
document.getElementById('listCar').innerHTML = inputList;
}
function initializeColour(inputList){
alert('Loading Colour')
document.getElementById('listColour').innerHTML = inputList;
}
function initializeForm(inputDetails){
alert('Loading Form')
document.getElementById('CustID').value = inputDetails[0];
document.getElementById('CustName').value = inputDetails[1];
document.getElementById('CustArea').value = inputDetails[2];
document.getElementById('listCar').value = inputDetails[3];
document.getElementById('listColour').value = inputDetails[4];
}
}
</script>
Sample Customer Data样本客户数据
ID No![]() |
Customer Name![]() |
Customer Area![]() |
Car Brand![]() |
Car Colour![]() |
---|---|---|---|---|
1001 ![]() |
Alice![]() |
IN![]() |
Toyota![]() |
Blue![]() |
1002 ![]() |
Bob![]() |
OH![]() |
Honda![]() |
Red![]() |
1003 ![]() |
Charlie![]() |
WD![]() |
BMW![]() |
Brown![]() |
Sample Colour样品颜色
Colour![]() |
---|
Blue![]() |
Red![]() |
Brown![]() |
Green![]() |
Yellow![]() |
Sample Car Brand样车品牌
Brand![]() |
---|
BMW![]() |
Toyota![]() |
Honda![]() |
Tesla![]() |
VW![]() |
I've tried to use If Else to make sure the ListBox is already populated before running the 3rd function but no luck with that.我尝试使用 If Else 来确保在运行第三个函数之前已经填充了 ListBox,但没有运气。
Thanks in Advance to anybody that can help in this.在此先感谢任何可以提供帮助的人。
Because google.script.run runs asynchronously, which means the second one doesn't wait for the first to return before running.因为 google.script.run 是异步运行的,这意味着第二个在运行之前不会等待第一个返回。 You need to nest them.
你需要嵌套它们。 Then in the html
<script>
simply run the first only.然后在html
<script>
中简单地运行第一个而已。 I've moved in_custID
outside of the setLocation call so its available to the other functions.我已将
in_custID
移到 setLocation 调用之外,因此它可用于其他函数。
function myLoadFunction(){
// Get the URL parameter
var in_custID = null;
google.script.url.getLocation(inputstrings => {
let inputjson = JSON.stringify(inputstrings.parameter);
let inputparameters = JSON.parse(inputjson)
in_custID = inputparameters.id;
alert('This is the ID '+in_custID);
google.script.run.withSuccessHandler(initializeCar).loadCarList();
})
function initializeCar(inputList){
alert('Loading Cars')
document.getElementById('listCar').innerHTML = inputList;
google.script.run.withSuccessHandler(initializeColour).loadColourList();
}
function initializeColour(inputList){
alert('Loading Colour')
document.getElementById('listColour').innerHTML = inputList;
google.script.run.withSuccessHandler(initializeForm).loadCustomer(in_custID);
}
function initializeForm(inputDetails){
alert('Loading Form')
document.getElementById('CustID').value = inputDetails[0];
document.getElementById('CustName').value = inputDetails[1];
document.getElementById('CustArea').value = inputDetails[2];
document.getElementById('listCar').value = inputDetails[3];
document.getElementById('listColour').value = inputDetails[4];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.