从Web应用程序传递变量并显示对话框

时间:2017-01-16 14:14:43

标签: javascript google-apps-script

我正在尝试从谷歌网络应用程序传递一个searchterm来显示结果。我在提交时遇到麻烦我收到一个空白屏幕。提交表单时,我希望它显示结果。主代码在记录器中工作 - 现在我只是在UI上工作并使表单工作。

任何帮助表示赞赏!

到目前为止,这是我的代码:

CODE:

function SearchFiles() {
//Please enter your search term in the place of Letter
var searchterm ="'mysearchinput'";  \\ this would be the variable that is passed from the form on index.html
var searchFor ="title contains " + searchterm;
var owneris ="and 'youremail@yourdomain.com' in Owners";
var names =[];
var fileIds=[];
var files = DriveApp.searchFiles(searchFor + owneris);
while (files.hasNext()) {
  var file = files.next();
  var fileId = file.getId();// To get FileId of the file
  fileIds.push(fileId);
  var name = file.getName();
  names.push(name);

}

for (var i=0;i<names.length;i++){
Logger.log(names[i]);
Logger.log("https://drive.google.com/uc?export=download&id=" + fileIds[i]);

}

}

function doGet() {
return HtmlService.createHtmlOutputFromFile('index');
}

function processForm(formObject) {
Logger.log('I was called!');
 // here is where I would like to display results of searthterm.
}

HTML:

 <!DOCTYPE html>
 <html>
 <head>
 <base target="_top">
      <script>
    function handleFormSubmit(formObject) {
    google.script.run.withSuccessHandler(updateUrl).processForm(formObject);
  }
    function onFailure(error) {
    var div = document.getElementById('output');
    div.innerHTML = "ERROR: " + error.message;
  }
 </script>
 </head>
 <body>
 Hello, World!
 <form id="myForm" onsubmit="handleFormSubmit(this)">
  <input type="text" name="search">
  <input type="submit" value="Submit" />

</form>
<input type="button" value="Close"
    onclick="google.script.host.close()" />

搜索功能:

function SearchFiles() {
  Logger.log('I Searched Files');
  //Please enter your search term in the place of Letter
  var searchterm ="'Whatevertextisinthesearchbox'";
  var searchFor ="title contains " + searchterm;
  var owneris ="and 'Youremail@yourdomain' in Owners";
  var names =[];
  var fileIds=[];
  var files = DriveApp.searchFiles(searchFor + owneris);
  while (files.hasNext()) {
    var file = files.next();
    var fileId = file.getId();// To get FileId of the file
    fileIds.push(fileId);
    var name = file.getName();
    names.push(name);

  }

  for (var i=0;i<names.length;i++){
  //  Logger.log(names[i]);
  //  Logger.log("https://drive.google.com/uc?export=download&id=" + fileIds[i]);
    var filesreturned = {
      name:names[i],
      urls:"https://drive.google.com/uc?export=download&id=" + fileIds[i]
     
    }
     Logger.log(filesreturned.name + " - " + filesreturned.urls);
     return(filesreturned);
  }

}

1 个答案:

答案 0 :(得分:1)

根据您上面的评论,这里是一个代码,只需在按钮点击时显示hello world。供您参考,我还添加了一个代码,用于在javascript和appscript之间传递数据。

Index.html 文件

<p>

Code.gs 文件

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
    function displayMessage()
    {
    google.script.run.withSuccessHandler(helloWorld).parseDataFromAppscript();
    }

    function helloWorld(stringText)
    {
    document.writeln(stringText);    
    }
    </script>
  </head>
  <body>
    <input type="button" value="submitButton" name="submitButton" onclick="displayMessage()"/>    
  </body>
</html>

要运行此功能,请转到发布 - &gt;部署为Web应用程序 - &gt;更新。然后点击最新代码

如果您想要任何部分的说明,请随时询问。我假设你已经知道html javascript和function doGet(e) { var template = HtmlService.createTemplateFromFile('Index'); return template.evaluate() .setTitle('Hello World') .setSandboxMode(HtmlService.SandboxMode.IFRAME); } function parseDataFromAppscript() { return "Hello World!"; } 方法。 :)