我是Google Apps脚本编程的新手。最近,我试图关注this tutorial by Google,但看起来他们的代码已经过时了。但是,我仍然希望按照教程执行的操作,但它现在将在Google云端硬盘中搜索文件。
以下是我的代码:
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var searchMenuEntries = [
{
name: "Search in all files",
functionName: "search"
}
];
spreadsheet.addMenu("Search Document", searchMenuEntries);
}
function search() {
// Prompt the user for a search term
var searchTerm = Browser.inputBox("Enter the string to search for:");
// Get the active sheet
var sheet = SpreadsheetApp.getActiveSheet();
// Set up the spreadsheet to display the results
var headers = [
["File Name",
"File Type",
"URL"
]
];
sheet.clear();
sheet.getRange("A1:C1").setValues(headers);
// Search the files in the user's Drive for the search term
var files = DriveApp.getFilesByName(searchTerm);
// Loop through the results and display the file name, file type, and URL
var i = 0;
while (files.hasNext()) {
files = files.next();
sheet.getRange(i+2, 1, 1, 1).setValue(files.getName());
sheet.getRange(i+2, 2, 1, 1).setValue(files.getMimeType());
sheet.getRange(i+2, 3, 1, 1).setValue(files.getDownloadUrl());
i++;
}
}
当我尝试运行代码时,没有任何重大错误给我一个警告。但是,代码不起作用,Google表格中的输出如下:
+---------------+----------------+----------------+
| File name | File type | URL |
+---------------+----------------+----------------+
| | | |
+---------------+----------------+----------------+
即使我非常确定我的云端硬盘中存在文件匹配项。我试图调试代码但是while循环似乎没有执行。任何帮助将不胜感激!
答案 0 :(得分:3)
此代码中有一些拼写错误,我不知道你在哪里或如何复制它...这是工作代码(见代码中的注释):
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var searchMenuEntries = [
{
name: "Search in all files",
functionName: "search"
}
];
spreadsheet.addMenu("Search Document", searchMenuEntries);
}
function search() {
// Prompt the user for a search term
var searchTerm = Browser.inputBox("Enter the string to search for:");
// Get the active sheet
var sheet = SpreadsheetApp.getActiveSheet();
// Set up the spreadsheet to display the results
var headers = [
["File Name",
"File Type",
"URL"
]
];
sheet.clear();
sheet.getRange("A1:C1").setValues(headers);
// Search the files in the user's Drive for the search term
var files = DriveApp.getFilesByName(searchTerm);
// Loop through the results and display the file name, file type, and URL
var i = 0;
while (files.hasNext()) { // files is the iterator
var file = files.next();// file is the drive document object
sheet.getRange(i+2, 1, 1, 1).setValue(file.getName());// file is the drive document object
sheet.getRange(i+2, 2, 1, 1).setValue(file.getMimeType());// file is the drive document object
sheet.getRange(i+2, 3, 1, 1).setValue(file.getDownloadUrl());// file is the drive document object
i++;
}
}
顺便说一下,这个脚本实现得很糟糕,它在一个非常低效的循环中使用API调用...... 下面是一个更快的版本(也更紧凑)
function search() {
// Prompt the user for a search term
var searchTerm = Browser.inputBox("Enter the string to search for:");
// Get the active sheet
var sheet = SpreadsheetApp.getActiveSheet();
// Set up the spreadsheet to display the results
var result = [];
result.push(["File Name","File Type","URL"])
// Search the files in the user's Drive for the search term
var files = DriveApp.getFilesByName(searchTerm);
// Loop through the results and display the file name, file type, and URL
while (files.hasNext()) {
var file = files.next();
result.push([file.getName(),file.getMimeType(),file.getDownloadUrl()]);
}
sheet.getRange(1,1,result.length,result[0].length).setValues(result);
}
要更改搜索条件,只需使用DriveApp.search调用并添加参数as specified in the documentation.
查看文件内容的示例:
var files = DriveApp.searchFiles('fullText contains "'+searchTerm+'"');