簡體   English   中英

Google雲端硬盤應用:如何獲取HTML文件以讀取code.gs中定義的變量?

[英]Google drive app: How can i get my HTML file to read a varitable defined in code.gs?

新手程序員在這里,請保持溫柔。

我正在嘗試在HTML文件中創建一系列搜索功能,這些功能可以搜索Google Drive數據庫的內容,並在彈出窗口中顯示結果。

以下是相關代碼:

代碼

function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('page')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setTitle('My custom sidebar')
      .setWidth(300);
  SpreadsheetApp.getUi() 
      .showSidebar(html);
}

function load() {
data = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
};

page.html

google.script.run.load()


function poponload() {
    testwindow = window.open("", "mywindow", 'location=1,status=1,scrollbars=1,width=400,height=200');
    testwindow.moveTo(0, 0);
    testwindow.document.write('<br>Date = ', data[1][1]);

}

當從html文件調用可變的“數據”時,javascript控制台將報告未定義該數據。 我已閱讀並重新閱讀了以下文檔

https://developers.google.com/apps-script/guides/html/communication

https://developers.google.com/apps-script/guides/html/templates

但是答案仍然使我難以理解。 我究竟做錯了什么? 還是不這樣做,以獲取page.html文件讀取的“數據”變量?

這是關於此的官方文檔

簡而言之,使用成功或失敗處理程序調用服務器功能。 如果您正在調用的函數返回任何內容,那么它將被傳遞給您指定的回調函數。

客戶

$(function(){ 
  google.script.run.withSuccessHandler(onSuccess).load();
}); //this is jQuery lib, and the function will run when the document is ready, not when the script is loaded, which is usually a better practice
function poponload(data) {
  testwindow = window.open("", "mywindow",'location=1,status=1,scrollbars=1,width=400,height=200');
  testwindow.moveTo(0, 0);
  testwindow.document.write('<br>Date = ', data[1][1]);
}

服務器

function load(){
  var data;
  ...get data somehow...
  return data;
}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM