【问题标题】:Variable returns undefined变量返回未定义
【发布时间】:2017-03-03 18:46:28
【问题描述】:

我收到未定义的变量名为:名称 关于为什么不显示结果的任何帮助。按下搜索后,它将显示在记录器中,但不会显示在 index.html 或 web 端。

代码:

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

【问题讨论】:

  • 您在哪一行出现错误?这可能有助于调试问题。
  • 我在函数 returnNames(names) 行上未定义 - 你可以看到我评论了该行
  • 多个问题。首先在索引调用中,您没有将任何内容传递给 returnNames()。因此,当函数运行时,它变得未定义并且可能打印未定义。其次,我认为您假设名称将在全球范围内可用,但我没有看到。
  • 谢谢,我看到了 - 但是,如何从索引页面传递变量名称?这不起作用: google.script.run.withSuccessHandler(handleResults).returnNames(names);另外,如何通过取消注释顶行来获取全局名称?

标签: javascript gwt google-apps-script


【解决方案1】:

我认为你做错了。如果您在 SearchFiles 末尾返回 returnNames(names) 并且您只需在您的 index.html 中调用 google.script.run.withSuccessHandler(handleResults).processForm(searchTerm); 就像这样:

代码.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>

我的文件使用术语"test"的结果截图:

【讨论】:

    【解决方案2】:

    您可以尝试通过这种方式将名称传递给您的 google 脚本。

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

    【讨论】:

    • 它为我返回 null,有什么想法吗?
    • 我稍微修改了代码,看看它是否可以运行。如果您遵循上面发布的相同代码,那么您的文件读取机制可能遇到问题。将调试器放在那里找出答案。
    • 显示控制台日志中没有定义processForm。
    • 嗨@rikin 是的,我看到它在这里运行,但是当尝试在谷歌脚本中执行它时,网络部署它不起作用。任何帮助表示赞赏 - 谢谢!
    • 您在上面提供的代码在通过谷歌运行时给出了 processForm 未定义的控制台错误。但是,如果我在上面的“运行代码片段”中运行它,它就会完成。
    猜你喜欢
    • 2012-01-16
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2020-08-21
    • 2016-05-03
    • 2016-05-13
    • 2020-06-07
    相关资源
    最近更新 更多