Google 代码无效
Google code does not work
我是 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 Sheets 中的输出如下:
+---------------+----------------+----------------+
| File name | File type | URL |
+---------------+----------------+----------------+
| | | |
+---------------+----------------+----------------+
尽管我很确定我的云端硬盘中有匹配的文件。我试图调试代码,但 while 循环似乎没有执行。如有任何帮助,我们将不胜感激!
此代码中有一些拼写错误,我不知道您在哪里/如何复制它...这是工作代码(请参阅代码中的注释):
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+'"');
我是 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 Sheets 中的输出如下:
+---------------+----------------+----------------+
| File name | File type | URL |
+---------------+----------------+----------------+
| | | |
+---------------+----------------+----------------+
尽管我很确定我的云端硬盘中有匹配的文件。我试图调试代码,但 while 循环似乎没有执行。如有任何帮助,我们将不胜感激!
此代码中有一些拼写错误,我不知道您在哪里/如何复制它...这是工作代码(请参阅代码中的注释):
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+'"');