0

I have a Google Script that Displays List of Data from Google Drive and this is what it looks like.

enter image description here

This code is working so perfect you just need index.html and code.gs and here is the code for both of them. (So for those who are using google app script in your site this will help.)

index.html

<!DOCTYPE html>
<html>
<head>
<style>
  .my_text
     {
        font-family:    Tahoma;
        font-size:      13px;
        font-weight:    normal;
     }
</style>

<base target="_top">

<script>
function displayMessage() 
{
        var searchTerm;
        searchTerm = document.getElementById('idSrchTerm').value;
        console.log('searchTerm: ' + searchTerm );
        google.script.run.withSuccessHandler(handleResults).processForm(searchTerm.replace("'","\'")); 
}            
        function handleResults(results){      
        var length=results.length;
        var table = document.getElementById("output");
        var count = document.getElementById("count");

             for(var i=0;i<length;i++)
             {
                var item=results[i];
                item=item.split("|~|");
                count.innerHTML = "Total Records Found : " + length;
                table.innerHTML  +="</br><a href='"+item[1]+"' target='_blank'>"+item[0]+"</a></br>  <B>Owner: </B>" +item[3]+ "</br><B>Last modified: </B>"+item[2]+ "</br> <B>File Size: </B>"+item[4]+"</br>";           
             }

}

function clearBox(elementID)
{
        document.getElementById("output").innerHTML = "";
        document.getElementById("count").innerHTML = "";
}
</script>

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

</head>

<body>


<div class="container">
       <p class = "my_text">Search File : <input type="text" id="idSrchTerm" name="search" class = "my_text">
       <input type="button" value="Search Drive" name="submitButton" class = "my_text" onClick="clearBox(); displayMessage();"/>
</div> 

<div id = "count" class = "my_text"></div>
<div id ="output" class = "my_text">

</div>

</body>
</html>

code.gs

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 + "'"; 
  var names = [];
  var files = DriveApp.searchFiles(searchFor); 
  while (files.hasNext()) {
    var file = files.next();
    var fileId = file.getId();
    var lm = file.getLastUpdated();
    var OType = file.getOwner().getName();
    var fsize = file.getSize()
    var name = file.getName()+"|~|"+file.getUrl()+"|~|"+lm+"|~|"+OType+"|~|"+fsize; 
    names.push(name); 
    Logger.log(file.getUrl());
  }
  return names; 

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

My only target here is how can I display the result in New Window? I mean I will type any string in textbox and click the button then thats it! the output will display in a new window like for example output.html

TYSM for future help.

  • and here is the other questions that I`ve posted that is related to this one. https://stackoverflow.com/questions/44428526/prevent-blank-page-and-display-data-in-table?noredirect=1#comment75868717_44428526 and https://stackoverflow.com/questions/44407931/display-list-of-files-inside-table/44413025#44413025 –  Jun 09 '17 at 10:44
  • Do you want a new ***browser*** tab to open? Or a new browser window? As opposed to a new tab inside of the HTML. – Alan Wells Jun 09 '17 at 12:49
  • Sorry for the late reply its weekend here so its hard open a PC here in my place, Anyway a New Tab will do. I mean a new tab next to my current tab (where Im running this code) in the same `Browser (G Chrome)` –  Jun 11 '17 at 02:53
  • I will edit my question header for a better understanding –  Jun 11 '17 at 02:53
  • [try this](https://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript) – Alok Deshwal Jun 11 '17 at 03:07
  • Sir please take a look at my question in `code.gs` looks like what you advise does not fit here or if you have any way to accomplish please let me know i can try it now. TYSM for the effort. –  Jun 11 '17 at 03:17
  • AFAIK, I don't think it's possible to move from one .html page to another .html like navigating a regular website. Although, it's possible to display the contents of somePage.html file into your mainPage.html file (the one specified in doGet(e) ) using scriptlets, != HtmlService.createHtmlOutputFromFile('NAME_OF_OTHER_HTML_FILE').getContent(); ?>. – ReyAnthonyRenacia Jun 11 '17 at 15:17
  • even in new tab in same browser is not possible? whats the best way to solve this? –  Jun 11 '17 at 16:12
  • This [link](https://stackoverflow.com/questions/15668119/linking-to-another-html-page-in-google-apps-script/16697525#16697525) answers my question. :) –  Jun 13 '17 at 00:40

0 Answers0