显示表格内的文件列表

时间:2017-06-07 08:53:42

标签: javascript html web-applications google-apps-script google-drive-api

截至目前,这是我的代码。

的index.html

<!DOCTYPE html>
<html>
<head>
<base target="_top">

<script>
function handleFormSubmit(formObject) 
{
google.script.run.testSearch(formObject);

} 
</script>

<style>
table, th, td {
    border: 1px solid black;
}
</style>

</head>

<body>
<script>
      google.script.run.testSearch(formObject);

</script>

 <div class="container">
   <form method="" onsubmit="handleFormSubmit(this)"> 
    <input type="text" id="txtsearch" name="txtsearch" placeholder="Search" Required>    
    <input id= "mybutton" type="submit" value="Search" >
  </form> 

</div> 

</body>
</html>

这里是 code.gs

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


}
function testSearch(formObject){
  var txtsearch = formObject.txtsearch;
  var filename 

  var files = DriveApp.searchFiles('fullText contains "'+txtsearch+'"');
  while (files.hasNext()) {
    var file = files.next();
    Logger.log(file.getName());



  }
}

index.html 的输出只显示一个文本框和按钮,实际上此代码正常工作,它将显示我在Logger.log我的目标中搜索过的任何文件名这里是如何在html表中显示的?我希望我可以在搜索框下面添加表格,不要重定向到任何页面。

TYSM

1 个答案:

答案 0 :(得分:0)

以下是您的脚本的工作示例: 代码:

function doGet(e) { // main function
  var template = HtmlService.createTemplateFromFile('index.html');
  return template.evaluate().setTitle('Search Drive').setSandboxMode(HtmlService.SandboxMode.IFRAME);
}


function SearchFiles(searchTerm) {
  var searchFor ="fullText contains '" + searchTerm + "'"; //single quotes are needed around searchterm
  var names = [];
  var files = DriveApp.searchFiles(searchFor); 
  while (files.hasNext()) {
    var file = files.next();
    var fileId = file.getId();// To get FileId of the file
    var lm = file.getLastUpdated();
    var name = file.getName()+"|~|"+file.getUrl()+"|~|"+lm; // Im concatenating the filename with file id separated by |~|
    names.push(name); // adding to the array
    Logger.log(file.getUrl());
  }
  return names; // return results
}




// Process the form
function processForm(searchTerm) {
  var resultToReturn;
  Logger.log('processForm was called! ' + searchTerm);
  resultToReturn  = SearchFiles(searchTerm); 
  Logger.log('resultToReturn: ' + resultToReturn);
  return resultToReturn; // return the results
}

指数:

<!DOCTYPE html>
<html>
<head>
<base target="_top">

<script>
   function displayMessage() {
        var searchTerm;
        searchTerm = document.getElementById('idSrchTerm').value;
        console.log('searchTerm: ' + searchTerm );
        // Below call means: call to processForm passing the searchTerm value (previously escaped) and after finish call the handleResults function
         google.script.run.withSuccessHandler(handleResults).processForm(searchTerm.replace("'","\'")); 
       } 


          function handleResults(results){      
         var length=results.length; // total elements of results
         for(var i=0;i<length;i++)
         {
         var item=results[i];
         item=item.split("|~|"); // split the line |~|, position 0 has the filename and 1 the file URL
         document.writeln("<b><a href='"+item[1]+"' target='_blank'>"+item[0]+"</b></a> (Last modified: "+item[2]+")<br/><br/>"); // write result
        }
        document.writeln("End of results...");
       }

</script>

<style>
table, th, td {
    border: 1px solid black;
}
</style>

</head>

<body>


 <div class="container">
    Search: <input type="text" id="idSrchTerm" name="search">
    <input type="button" value="Search" name="submitButton" onclick="displayMessage()"/>

</div> 

</body>
</html>