谷歌驱动器应用程序:我怎么能让我的HTML文件读取一个变量定义在code.gs

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

本文关键字:一个 变量 定义 gs code 读取 应用程序 驱动器 怎么能 文件 HTML      更新时间:2023-09-26

新手在这里,请温柔点。

我试图在一个HTML文件中创建一系列搜索函数,该文件可以搜索谷歌驱动器数据库的内容,并在弹出窗口中显示结果。

相关代码如下:

代码。GS

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文件中调用变量'data'时,javascript控制台报告它没有定义。我已经反复阅读了

上的文档https://developers.google.com/apps-script/guides/html/communication

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

但我仍然想不出答案。我做错了什么?或者也许不这样做,以获得由page.html文件读取的'data'变量?

这是关于这个的官方文档。

简而言之,使用成功或失败处理程序调用服务器函数。如果你调用的函数返回任何值,那么它将被传递给你指定的回调函数。

$(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;
}