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

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

问题描述

在Google云端硬盘中,我试图在当前目录中搜索文件。脚本链接到电子表格,因此它找到电子表格的父目录,然后使用它的ID在文件夹中指定一个名为Title的文件。



代码我到目前为止是:

 函数searchFolder(){

//获取当前文件夹ID
var ss = SpreadsheetApp.getActive(); //当前电子表格
var directParents = DriveApp.getFileById(ss.getId())。getParents(); $(b)b
while(directParents.hasNext()){
var folder = directParents.next();
var folderId = folder.getId();
Logger.log(folder.getName()+has id+ folderId);
}

//获取文件夹
中的文件var parent = DriveApp.getFolderById(folderId); //文件夹Id
var search ='name contains标题';
var specificFolder = parent.searchFiles(search); $(b)b
while(specificFolder.hasNext()){
var file = specificFolder.next();
Logger.log(file.getName());
Logger.log(file.getId());


$ / code $ / pre

我得到错误Invalid argument:queryat while循环的开始,这表明没有找到文件。



这个错误似乎只发生在我按名称搜索时发生。如果我将搜索变量更改为'fullText contains'hello''(文件Title中包含'hello'),那么就没有问题了。可以通过名称进行搜索工作?



要复制错误,请在Google Drive上的文件夹中制作两个电子表格。一个电子表格将脚本链接到它,另一个名为标题并正在被搜索。正如Srikanth指出的那样,它是标题包含不是 code> name包含。代码可以变得更简单,请尝试下面的代码。您不需要使用扩展程序,只需使用简单的Google Apps脚本项目文件即可。右键点击您的驱动器 ---> 更多 - > Google的空白区域Apps脚本

 函数SearchFiles(){
//请输入您的搜索字词Letter
var searchFor ='title包含Letter'的地方;
var names = [];
var fileIds = [];
var files = DriveApp.searchFiles(searchFor);
while(files.hasNext()){
var file = files.next();
var fileId = file.getId(); //获取文件的FileId
fileIds.push(fileId);
var name = file.getName();
names.push(name);


$ b 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)搜索云端硬盘文件


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.

The code I have so far is:

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());
}
}

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

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?

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.

解决方案

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]);

  }

}

Also check these links:

1) Search all documents in a folder 2)Search Drive Files

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

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