[英]How to return values correctly using withSuccessHandler
我又遇到了 google.script.run 的問題,這一次,我想從電子表格中檢索數據。 如果您發現錯誤,請隨時將其寫在答案中。
順便說一下,這是我使用的一些代碼:
Html 代碼:
alert(GetData(0, 1, 1));
function GetData(Page, GetRow, GetColumn)
{
var DataExtracted;
google.script.run.withSuccessHandler(function(Data){DataExtracted = Data;}).GetCellValue({PageNum: Page, Row: GetRow, Column: GetColumn});
return DataExtracted;
}
GS代碼:
var Server = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/...');
//I'm not writing the Url, because of privacy
var Pages = Server.getSheets();
function GetCellValue(Object)
{
return Pages[Object.PageNum].getRange(Object.Row, Object.Column).getValue();
}
Html:
<script>
var DataExtracted;
function GetData(Page, GetRow, GetColumn) {
google.script.run
.withSuccessHandler(function(Data){
DataExtracted=Data;//DataExtracted is global and available to other functions
window.alert(Data);
})
.GetCellValue({PageNum: Page, Row: GetRow, Column: GetColumn});
}
</script>
GS:
function GetCellValue(Object) {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheets()[Object.PageNum];//assuming numbers start at zero
return sh.getRange(Object.Row, Object.Column).getValue();
}
通常在調用服務器 function 和調用 withSuccessHandler 之間至少有幾秒鍾的時間。 您可以在其他功能中使用 gData。 是服務器返回的值 function getCellValue();
var DataExtracted;
GetData(0, 1, 1);
alert(DataExtracted);
function GetData(Page, GetRow, GetColumn)
{
google.script.run.withSuccessHandler(function(Data){DataExtracted = Data;}).GetCellValue({PageNum: Page, Row: GetRow, Column: GetColumn});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.