使用谷歌脚本在谷歌云端硬盘的文件夹中按名称搜索文件 [英] Searching for file by name within a folder in google Drive using google scripts

查看:34
本文介绍了使用谷歌脚本在谷歌云端硬盘的文件夹中按名称搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Google 云端硬盘中,我正在尝试在当前目录中搜索文件.该脚本链接到电子表格,因此它会找到电子表格的父目录,然后使用其 id 指定在文件夹中搜索名为 Title 的文件.

In Google Drive I'm trying to search for a file in the current directory. The script is linked to a spreadsheet, so it finds the spreadsheet's parent directory, then uses its id to specify a search within the folder for a file named Title.

我目前的代码是:

function searchFolder(){

//get current folder id
var ss = SpreadsheetApp.getActive(); //current spreadsheet
var directParents = DriveApp.getFileById(ss.getId()).getParents();

while( directParents.hasNext() ) {
  var folder = directParents.next();
  var folderId = folder.getId();
  Logger.log(folder.getName() + " has id " + folderId);
}

//get files in folder
var parent = DriveApp.getFolderById(folderId); // folder Id
var search = 'name contains "Title"';
var specificFolder = parent.searchFiles(search);

while (specificFolder.hasNext()) {
  var file = specificFolder.next();
  Logger.log(file.getName());
  Logger.log(file.getId());
}
}

我在 while 循环开始时收到错误无效参数:查询",这表明没有找到文件.

I get the error "Invalid argument: query" at the beginning of the while loop, which suggests that no files were found.

此错误似乎仅在我按名称搜索时发生.如果我将搜索变量更改为'fullText contains "hello"'(文件标题中有'hello'),那么就没有问题了.怎样才能使按名称进行搜索?

This error seems to only occur when I search by name. If I change the search variable to 'fullText contains "hello"' (the file Title has 'hello' in it) then there are no problems. What can be done to make the search by name work?

要复制错误,请在 google 驱动器上的文件夹中制作两个电子表格.一个电子表格有链接到它的脚本,另一个被命名为标题"并且正在被搜索.谢谢.

To replicate the error, make two spreadsheets in a folder on google drive. One spreadsheet has the script linked to it, the other is named "Title" and is being searched. Thanks.

推荐答案

正如 Srikanth 指出的,它是 title contains 而不是 name contains.代码可以更简单,请尝试以下代码.您不需要电子表格,只需使用简单的 Google Apps 脚本项目文件即可.右键单击 Drive ---> More ---> Google Apps Script

As Srikanth pointed, it is title contains not name contains. The code could be made simpler, please try the following code. You don't need a spreadhseet for this, just use simple Google Apps Script project file. Right click on empty space of your Drive ---> More ---> Google Apps Script

function SearchFiles() {
  //Please enter your search term in the place of Letter
  var searchFor ='title contains "Letter"';
  var names =[];
  var fileIds=[];
  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++){
    Logger.log(names[i]);
    Logger.log("https://drive.google.com/uc?export=download&id=" + fileIds[i]);

  }

}

同时检查这些链接:

1) 搜索文件夹中的所有文档2)搜索驱动器文件

这篇关于使用谷歌脚本在谷歌云端硬盘的文件夹中按名称搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆