![](/img/trans.png)
[英]How to modify html file before HTMLOutput in Google Apps Script
[英]How can I force Google Apps Script to wait for user to select a button on one HtmlOutput before loading the next htmlOutput?
我是新手/業余 javascript/Google Apps 腳本程序員,所以如果答案很明顯,請原諒我。 此外,這可能太明顯了,我什至不知道正確的搜索詞來找到我的答案。 如果是這樣,我很抱歉被騙了。
我正在編寫一個 Google Apps 腳本函數,理想情況下,它會在用戶關閉前一個窗口后加載一系列 HtmlOutput 窗口,並在每個后續窗口打開。 但是,當我嘗試運行它時,腳本會按順序加載所有窗口,而無需等待用戶關閉上一個窗口。
我也有點意識到使用Utilities.sleep()
函數不是最佳實踐。 但是,我想知道這是否是在這種情況下實現這一目標的唯一方法。 似乎會有更明顯、更好的方法。
如何強制程序在加載下一個窗口之前等待用戶的響應?
下面是一些示例代碼:
//THIS FUNCTION SETS UP THE HTMLOUTPUT
function setDialog(userText){
var displayHtml = HtmlService.createHtmlOutput(
'<!DOCTYPE html>'+
'<html>'+
'<head>'+
'<base target="_top">'+
'</head>'+
'<body>'+
'<h1>'+userText+'</h1>'+
'<input type="button" value="Close" '+
'onclick="google.script.host.close()" />'+
'</body>'+
'</html>');
SpreadsheetApp.getUi()
.showModalDialog(displayHtml, 'Here's your HTML!');
}
//THIS FUNCTION DISPLAYS 4 DIALOGS BASED ON VARIABLES
function displayDialogs(){
var uOne = 'One';
var uTwo = 'Two';
var uThree = 'Three';
var uFour = 'Four';
setDialog(uOne);
setDialog(uTwo);
setDialog(uThree);
setDialog(uFour);
}
我想要的行為是:
相反,我得到:
在此先感謝您的幫助,如果這是一個騙局,我很抱歉。 我確實在發布之前搜索了答案。
對您的代碼進行以下修改后,將一個接一個地顯示連續對話框,僅在前一個關閉時顯示,並在沒有更多顯示時停止。
作為 JSON 對象的全局變量有助於控制流。 “關閉”按鈕必須在對話框關閉之前調用google.script.run
函數,以便打開下一個對話框。
var GLOBAL_OBJECT_FOR_NEXT_DISPLAY = {//Use all caps for Global variables
"1":"Two",
"2":"Three",
"3":"Four"
}
//THIS FUNCTION SETS UP THE HTMLOUTPUT
function setDialog(userText, thisLoop){
var displayHtml = HtmlService.createHtmlOutput(
'<!DOCTYPE html>'+
'<html>'+
'<head>'+
'<base target="_top">'+
'</head>'+
'<body>'+
'<h1>'+userText+'</h1>'+
'<input type="button" value="Close" '+
'onclick="google.script.run.showNext(' + thisLoop + ');google.script.host.close(); " />'+
'</body>'+
'</html>');
SpreadsheetApp.getUi()
.showModalDialog(displayHtml, "Here's your HTML!");
}
//THIS FUNCTION DISPLAYS 4 DIALOGS BASED ON VARIABLES
function displayDialogs(){
var uOne = 'One';
setDialog(uOne,1);
}
function showNext(count) {
var whatNext;
count = count.toString();
Logger.log('count: ' + count)
whatNext = GLOBAL_OBJECT_FOR_NEXT_DISPLAY[count];
Logger.log('whatNext: ' + whatNext)
if (!whatNext) {return;}//Stop if there is no next value found
setDialog(whatNext,Number(count) + 1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.