5

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@email.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++) {
    //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>
OblongMedulla
  • 1,471
  • 9
  • 21
  • 1
    at what line are you getting error? That could help debugging the issue. – Rikin Mar 03 '17 at 18:57
  • I am getting undefined on the function returnNames(names) line- you can see I commented the line – OblongMedulla Mar 03 '17 at 18:59
  • 2
    multiple issues. First in index call you are not passing anything to returnNames(). Thus when the function runs it gets undefined and probably prints undefined. Second I think you are assuming names will be available globally which I dont see it. – Rikin Mar 03 '17 at 19:04
  • Thanks I see this- however, how can I pass the variable names from the index page? This doesn't not work: google.script.run.withSuccessHandler(handleResults).returnNames(names); Also how can I get the names globally by using uncommenting the top line? – OblongMedulla Mar 03 '17 at 19:12

2 Answers2

3

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@email.com' 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

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
1

You can try it this way to pass around names to your google script.

In SearchFiles(searchTerm) you return names (which can be either blank array or valued array with names in it).

// var names =[]; //I tried using a global variable but with no luck
var Logger = {
  log: function(){
    console.log(arguments[0]);
  }
};

function SearchFiles(searchTerm) {
  var searchFor = "title contains '" + searchTerm + "'";
  var owneris = "and 'Email@email.com' in Owners";

  var names = ["file1","file2","file3"];
  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]);
  }

  return names;
}

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 = "DUMMY TEXT";//document.getElementById('idSrchTerm').value;

      console.log('searchTerm: ' + searchTerm);

      //google.script.run.processForm(searchTerm);
      //google.script.run
        //.withSuccessHandler(handleResults)
        //.returnNames(google.script.run.processForm(searchTerm));
        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>
Rikin
  • 5,351
  • 2
  • 15
  • 22
  • its returning null for me, any thoughts? – OblongMedulla Mar 03 '17 at 22:49
  • 1
    I modified code a bit to see if it runs and it does. If you are following the same code posted above then may be your file reading mechanism is encountering issue. Place debugger over there to find out. – Rikin Mar 04 '17 at 02:49
  • It shows processForm is not defined in the console log. – OblongMedulla Mar 06 '17 at 17:01
  • Hi @rikin Yes I see that it runs here, but when trying to execute it within google scripts web deploy it is not working. Any help is appreciated- thanks! – OblongMedulla Mar 06 '17 at 20:23
  • The code you supplied above is giving the processForm not defined console error when run via google. However if I run it in the above "Run code Snippet" it complets. – OblongMedulla Mar 06 '17 at 21:24
  • In that case issue seems like in wiring up google script code. – Rikin Mar 07 '17 at 01:48