私はgoogleのアプリケーションスクリプトコードを書いた、それはGoogleの普及したシートを開き、行によって値をリストするが、2つの問題がある: 1.ランダムな順序による出力。 2. "loding"というidのdivテキストが "Finished!"に変わります。すべての値をリストする前に。 私は "withSuccessHandler()"でスクリプトを実行したときに、スクリプトがサーバー側関数の戻りを待つと思ったが、そうではなかった。 どうすれば修正できますか?Google Appsスクリプトのサーバー側の関数を同期して呼び出す方法はありますか。
のindex.html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function jsListValue() {
// Get count.
google.script.run.withSuccessHandler(function(count) {
// List all values.
for(count; count>0; count=count-1) {
// Get a value.
google.script.run.withSuccessHandler(function(content) {
// Shows in "output".
var new_div = document.createElement("div");
new_div.appendChild(document.createTextNode(content));
document.getElementById("output").appendChild(new_div);
}).gsGetValue(count);
}
// Change loding notice.
document.getElementById("loding").innerHTML = "Finished!";
}).gsGetCount();
}
</script>
</head>
<body onload="jsListValue()">
<div id="output"></div>
<div id="loding">Loding now...</div>
</body>
</html>
code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('index').setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function gsOpenSheet() {
// Return sheet of the note data.
return (SpreadsheetApp.openById("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").getSheetByName("sheet1"));
}
function gsGetCount() {
// Return last row index in this sheet.
return (gsOpenSheet().getLastRow());
}
function gsGetValue(index) {
// Return value in the (index,1).
return (gsOpenSheet().getRange(index,1).getValue());
}
を使用します。この場合、個々の値を取得する必要はありません。これは、 'index'が行であり、行がすべて連続しているためです。連続していない行を取得する必要がある場合、それは異なるでしょう。同じサーバー側機能に対して複数の高速呼び出しを行っています。この状況では、サーバー側の関数が値を返す順序は保証されません。同じ機能の複数のインスタンスが同時に実行されています。サーバー側の機能を同時に実行できるため、必ずしも順番に完了するとは限りません。 –
関連:http://stackoverflow.com/questions/35749500/client-javascript-receiving-outdated-values-from-server-side-document-set-and-fe/35752170 –