Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable returns undefined

Tags:

I am receiving undefined for the variable called: names Any help on why it is not displaying the results. It will display in the logger but not on the index.html or the web side after search is pressed.

code:

// var names =[]; //I tried using a global variable but with no luck

function SearchFiles(searchTerm) {
  var searchFor = "title contains '" + searchTerm + "'";
  var owneris = "and '[email protected]' 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++) {
    //this is showing in the Logger
    Logger.log(names[i]);
    Logger.log("https://drive.google.com/uc?export=download&id=" + fileIds[i]);
  }

}

function returnNames(names) {
  return '<h3><b>returnNames has ran.!</b></h3> <br>' + names; // Why does this names variable return undefined???

}

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


function processForm(searchTerm) {
  var resultToReturn;
  Logger.log('processForm was called! ' + searchTerm);
  resultToReturn = SearchFiles(searchTerm);
  Logger.log('resultToReturn: ' + resultToReturn)
  // shows as undefined in the logger
  return resultToReturn;
}
<!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);
      google.script.run.withSuccessHandler(handleResults).returnNames();
    }


    function handleResults(searchTerm) {

      console.log('Handle Results was called! ');
      document.writeln(searchTerm);
    }
  </script>
</head>

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

</body>

</html>
like image 997
OblongMedulla Avatar asked Mar 03 '17 18:03

OblongMedulla


1 Answers

I think you're doing it in the wrong way. It will work if you return returnNames(names) at the end of SearchFiles and you just call google.script.run.withSuccessHandler(handleResults).processForm(searchTerm); inside your index.html like this:

Code.gs

function SearchFiles(searchTerm) {
  var searchFor = "title contains '" + searchTerm + "'";
  var owneris = "and '[email protected]' in Owners";

  var names = [];
  var fileIds = [];
  Logger.log(searchFor + " " + owneris);
  //Logger.log(searchFor);
  var files = DriveApp.searchFiles(searchFor + " " + owneris);
  //var files = DriveApp.searchFiles(searchFor);
  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++) {
    //this is showing in the Logger
    Logger.log(names[i]);
    Logger.log("https://drive.google.com/uc?export=download&id=" + fileIds[i]);
  }

  return returnNames(names); // Here call directly returnNames and get the wanted result
}

function returnNames(names) {
  var result = '<h3><b>returnNames has ran.!</b></h3> <br>'; // + names; // Why does this names variable return undefined???
  result += '<div>names.length = '+names.length+'</div>';

  for(var i=0; i<names.length; i++) {
    result += '<div>'+names[i]+'</div>';
  }

  return result;
}

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

function processForm(searchTerm) {
  var resultToReturn;
  Logger.log('processForm was called! ' + searchTerm);
  resultToReturn = SearchFiles(searchTerm);
  Logger.log('resultToReturn: ' + resultToReturn)
  // shows as undefined in the logger
  return resultToReturn;
}

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);
      //google.script.run.withSuccessHandler(handleResults).returnNames();
      google.script.run.withSuccessHandler(handleResults).processForm(searchTerm);
    }

    function handleResults(searchTerm) {
      console.log('Handle Results was called! ');
      document.writeln(searchTerm);
    }
  </script>
</head>

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

</html>

The result screenshot of my files using the term "test":

Screenshot working

like image 180
Christos Lytras Avatar answered Sep 23 '22 10:09

Christos Lytras