变量 returns 未定义
Variable returns undefined
我收到名为 names 的变量未定义
关于为什么不显示结果的任何帮助。按下搜索后,它会显示在记录器中,但不会显示在 index.html 或网页端。
代码:
// 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>
您可以尝试通过这种方式将名称传递给您的 google 脚本。
在 SearchFiles(searchTerm) 中,您 return 命名(可以是空白数组或包含名称的值数组)。
// 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>
我认为你做错了。如果你在 SearchFiles
的末尾 return returnNames(names)
并且你只是在你的 index.html
中调用 google.script.run.withSuccessHandler(handleResults).processForm(searchTerm);
就像这样:
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>
我的文件使用术语"test"
的结果截图:
我收到名为 names 的变量未定义 关于为什么不显示结果的任何帮助。按下搜索后,它会显示在记录器中,但不会显示在 index.html 或网页端。
代码:
// 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>
您可以尝试通过这种方式将名称传递给您的 google 脚本。
在 SearchFiles(searchTerm) 中,您 return 命名(可以是空白数组或包含名称的值数组)。
// 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>
我认为你做错了。如果你在 SearchFiles
的末尾 return returnNames(names)
并且你只是在你的 index.html
中调用 google.script.run.withSuccessHandler(handleResults).processForm(searchTerm);
就像这样:
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>
我的文件使用术语"test"
的结果截图: