简体   繁体   English

变量返回undefined

[英]Variable returns undefined

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. 在按下搜索后,它将显示在记录器中,但不会显示在index.html或Web端。

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> 

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); 如果你在SearchFiles的末尾返回returnNames(names) ,你只需要调用google.script.run.withSuccessHandler(handleResults).processForm(searchTerm); inside your index.html like this: 你的index.html里面是这样的:

Code.gs 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 的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" : 使用术语"test"的我的文件的结果屏幕截图:

截图工作

You can try it this way to pass around names to your google script. 您可以通过这种方式尝试将名称传递给Google脚本。

In SearchFiles(searchTerm) you return names (which can be either blank array or valued array with names in it). 在SearchFiles(searchTerm)中,您返回名称(可以是空白数组或带有名称的值数组)。

 // 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM