2016-03-30 60 views
2

如何從code.gs返回index.html。Google腳本從code.gs返回到index.html

我在「Index.html」中試過這個,但是這不起作用。

的index.html

$('#test').click(function() { 
     alert(google.script.run.getAmountOfImages()); 
    }); 

Code.gs

function getAmountOfImages() 
{ 
    return "Tom" 
} 

請幫助。

回答

1

按照documentation

「google.script.run是一個異步客戶端的JavaScript API 提供HTML的服務頁面,可以調用服務器端的應用腳本 功能。」

因此您的結果將在回調中返回。所以,你將不得不使用處理函數即withFailureHandlerwithSuccessHandler

google.script.run 
    .withFailureHandler(function(err){ 
     console.error("error occured", e); 
    }) 
    .withSuccessHandler(function(res){ 
     alert(res); 
    }) 
    .getAmountOfImages(); 

res參數在成功處理程序的回調函數將舉行從谷歌應用程序腳本的響應。