【发布时间】: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