从webapp传递searchterm显示结果

时间:2017-02-28 14:37:04

标签: javascript google-app-engine google-apps-script google-apps

我有一个自定义功能,可以搜索云端硬盘。我试图让Web应用程序将searchterm传递给自定义函数并返回结果。

目前,当我在记录器中测试该功能时,它可以工作并返回文件。

在下面的示例中,我有一个hello world工作 - 但如前所述,我还没有能够从输入表单中传递searchterm。

CODE.GS



function SearchFiles() {

var searchterm ="'YOUR SEARCHTERM'";  // this would be the variable that is passed from the form on index.html
var searchFor ="title contains " + searchterm;
var owneris ="and 'emailaddress@goeshere.com' in Owners";
var names =[];
var fileIds=[];
  Logger.log(searchFor + " " + owneris);
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(e) {
  var template = HtmlService.createTemplateFromFile('Index');
  return template.evaluate()
      .setTitle('Hello World')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function parseDataFromAppscript()
{
  return "Hello World!";
}

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

INDEX.HTML

&#13;
&#13;
<!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="text" name="search">   
    <input type="button" value="submitButton" name="submitButton" onclick="displayMessage()"/>
 
  </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

在输入文本框中添加一个id,然后使用DOM获取值。将搜索项放在要调用的gs函数的括号中。

<强>的index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      function displayMessage() {
        var searchTerm;

        searchTerm = document.getElementById('idSrchTerm').value;

        console.log('searchTerm: ' + searchTerm );
        google.script.run
        .processForm(searchTerm);
}

    </script>
  </head>
  <body>
    <input type="text" id="idSrchTerm" name="search">   
    <input type="button" value="submitButton" name="submitButton" onclick="displayMessage()"/>

  </body>
</html>

<强> Code.GS

function SearchFiles() {
var searchTerm = "";
var searchFor ="title contains " + searchTerm;
var owneris ="and 'youremail@youremail.com' in Owners";
var names =[];
var fileIds=[];
  Logger.log(searchFor + " " + owneris);
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);
  var endResult, fromSearchingFiles;

  fromSearchingFiles = SearchFiles(searchTerm);//Run SearchFiles function
  //and pass the search term to the function

  endResult = parseDataFromAppscript(fromSearchingFiles);

  return endResult;

}

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(e) {
  var template = HtmlService.createTemplateFromFile('Index');
  return template.evaluate()
      .setTitle('Hello World')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}



function processForm(searchTerm) {
  var resultToReturn;
  Logger.log('processForm was called! ' + searchTerm);
   // here is where I would like to display results of searchterm.

  resultToReturn  = SearchFiles(searchTerm);

  Logger.log('resultToReturn: ' + resultToReturn)

  return resultToReturn;
}