繁体   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