gpt4 book ai didi

javascript - 等待 GAS 中的函数执行

转载 作者:行者123 更新时间:2023-12-02 20:57:40 28 4
gpt4 key购买 nike

我有一个 Google App Script Code 代码,要求用户通过 html 自定义对话框输入一些值。对话框中的按钮触发 .gs 文件中的一个函数,我想等待该触发的函数返回,然后再继续其余代码。

  function main () {
selectSheets ();
//Below are the functions that I need openSheets to return before calling them (I'm not including their implementation here, as I don't think it's needed):
categoriesAndScoresDictionary = getCategoriesDictionary();
categoriesEntries = getCategoriesEntries(categoriesAndScoresDictionary);
//and other functions..

}

function selectSheets () {

var htmlDialog = HtmlService.createTemplateFromFile("sheets_menu")
var spreadsheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var sheetsArray = [];

for (index in spreadsheets) {
sheetsArray.push(spreadsheets [index].getSheetName());
}

htmlDialog.sheetsArray = sheetsArray;

var html = htmlDialog.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setWidth(300);

SpreadsheetApp.getUi().showModalDialog(html, "Select sheets names");
return html
}

下面的函数是通过html中的onclick调用的,我想等待它返回,然后再继续在主函数中执行:

function openSheets (appsSelection, rubricsSelection) {

var spreadsheetFile = SpreadsheetApp.getActiveSpreadsheet();
Logger.log(appsSelection + " " + rubricsSelection);

//Open the sheet by name inside the opened Spreadsheet (inside the file)
//Open applications sheet:
applicationsSheet = spreadsheetFile.getSheetByName(appsSelection);

//Open rubrics sheet:
rubricsSheet = spreadsheetFile.getSheetByName(rubricsSelection);
//This function gets called from the html side and it runs successfully, I need to wait for it to return before executing other functions called in main ()

}

这是 html:

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<input type = "hidden" value="<?!= sheetsArray ?>" id= "sheetsArray"/>

<br>
<p>Select the name of the applications sheet:</p>
<select id = "sheetsList">

</select>
<br>
<p>Select the name of the rubrics sheet:</p>

<select id = "sheetsList2">

</select>

<br><br><br>
<center><input type= "button" value = "Confirm" onclick ="sendSelectionToJs()"/></center>

<script>
// Create the list element:
function fillSelection () {
var firstMenu = document.getElementById("sheetsList");
var secondMenu = document.getElementById("sheetsList2");

var holderArray = document.getElementById ("sheetsArray").value;
var sheetsArray = holderArray.split(",");

for (var i = 0; i<sheetsArray.length; i++){
var option = document.createElement("option");
option.value = sheetsArray[i];
option.innerHTML = sheetsArray[i];
firstMenu.appendChild(option);
}

for (var i = 0; i<sheetsArray.length; i++){
var option = document.createElement("option");
option.value = sheetsArray[i];
option.innerHTML = sheetsArray[i];
secondMenu.appendChild(option);
}
}


function sendSelectionToJs() {

var appsSelection = document.getElementById ("sheetsList").value;
var rubricsSelection = document.getElementById ("sheetsList2").value;
google.script.run.openSheets(appsSelection, rubricsSelection);
google.script.host.close();

}

fillSelection ();

</script>
</body>
</html>

我尝试过让 openSheets 更改标志值的想法,并且在执行 main 内的其余函数之前不断检查标志值,但是尽管 openSheets 内部的值发生变化,但它永远不会全局更改。

编辑:我想做的是要求用户通过 html 对话框选择要处理的工作表名称(他们应该选择 2),然后通过 openSheets 打开这些工作表。 main () 中调用的其余函数需要知道工作表名称,这就是为什么我需要等待 openSheets 返回。

谢谢。

最佳答案

不可能让服务器端代码等待客户端代码完成。恕我直言,您应该更改脚本的逻辑:

  1. 使用服务器端功能打开对话框/侧边栏。通常此类功能不会包含其他内容,因为对话框/侧边栏打开是异步的。
  2. 客户端代码应调用服务器端函数,该函数可能返回受支持的 JavaScript 对象。此服务器函数可用于调用应同步运行(以特定顺序)的其他服务器函数。

相关

关于javascript - 等待 GAS 中的函数执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61432591/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com