英文:
Determining which Drive a File is in using Google App Script
问题
I can help you with the translation. Here's the translated text:
我有一个Google App Script,用于查找符合各种搜索参数的Google Workspace组织中的文件。目前一切顺利。
但是,我遇到的问题是,它不仅找到了共享驱动器中的文件,还找到了明显位于个别用户的“我的驱动器”中的文件,我似乎无法在脚本中确定到底是哪一个。
我正在使用DriveApp.searchFiles
来获取找到的文件的文件迭代器。我在做类似这样的事情:
var files = DriveApp.searchFiles(searchParams);
while (files.hasNext()) {
var file = files.next();
// 对文件进行某些操作
}
我尝试过的一件事是使用文件进行file.getParents()
的递归调用,以确定文件的完整路径。我希望能够查看顶层文件夹,并看到“我的驱动器”或实际共享驱动器的名称。但是,似乎无论文件实际上位于某人的“我的驱动器”中还是位于共享驱动器中,根文件夹都始终报告为“Drive”,路径中没有“我的驱动器”或共享驱动器文件夹名称。
我还尝试调用file.getOwner()
。出于某种原因,即使文件不在共享驱动器中,这也会返回null
,这与我的理解相矛盾,即它应该是文件所在的“我的驱动器”的所有者。
我还寻找了通过API确定文件所在驱动器的任何方法。但是,在驱动器本身方面没有任何信息。
我还尝试向我的searchParams
字符串中添加一些内容,以仅限于搜索共享驱动器中的文件。但是,搜索指南似乎没有提供任何这样做的方法。确实有一个关于“共享驱动器特定查询术语”的部分,但没有任何关于如何访问它的指示;例如,“name” QueryTerm似乎提供了一种根据“共享驱动器的名称”搜索的机制,但没有说明当已经有一个“name” QueryTerm在“文件特定查询术语”部分中尝试匹配文件名时,应该如何使用它。
使用文件的此定义,我想在Google App Script中了解以下任一信息,按重要性递减排列:
- 如何确定文件所在的共享驱动器的名称(如果适用)
- 如何确定文件是否在共享驱动器中
- 将要添加到我的searchParams的搜索字符串,以过滤掉不在共享驱动器中的任何内容。
此外,我看到了这个问题,其中有一个答案建议使用“高级驱动服务”,但我找不到任何显示如何实际使用它的文档(例如,Drive.Drives的JavaScript参考)。我最接近的是这个页面,其中隐藏了v2,但它显示了REST API的JSON表示,没有说明它与预计是JavaScript对象Drive.Drives的对应关系,也没有说明如何使用它。
由于我的searchFiles调用已经返回共享驱动器中的文件,我也不确定这个答案是否仍然是最新的。
英文:
I have a Google App Script which finds files in Google Workspace for my organization which match various search parameters. So far so good.
But, the issue that I am running into is that it is finding not only files in Shared Drives but also files which are apparently in individual users' "My Drive" and I cannot seem to tell which it is in the script.
I'm using DriveApp.searchFiles
to get the file iterator for the files found. I'm doing something like this:
var files = DriveApp.searchFiles(searchParams);
while (files.hasNext()) {
var file = files.next();
<do something with file here>
}
One of the things I tried to do with the file was to call file.getParents()
recursively to determine the full path to the file. My hope was that I could look at the top-level folder and see either "My Drive" or the name of the actual Shared Drive. However, it seems like the root folder is always reported as "Drive" regardless of whether the file is actually in somebody's "My Drive" or is in one of the Shared Drives. There is no "My Drive" or Shared Drive folder name anywhere in the path.
I also tried calling file.getOwner()
. For some reason this is returning null
, even when the file is not in a Shared Drive, which contradicts my understanding that it should be the owner whose "My Drive" the file is in.
I also looked for any way to determine which drive the file is in via the API. However, there is nothing regarding the drive itself.
I also tried adding something to my searchParams
string to would limit the search to only files in a Shared Drive. However, the search guide doesn't seem to provide any way to do this. There is a section on "Shared drive-specific query terms" but no indication on how to access this; for instance, the "name" QueryTerm appears to provide a mechanism to search based on the "Name of the shared drive" however there is no indication how one would use this when there is already a "name" QueryTerm in the "File-specific query terms" section which tries to match the file name.
Using this definition of a file, I would like to know any one of these in a Google App Script, in decreasing order of importance:
- How to determine the name of the Shared Drive the file is in (if applicable)
- How to determine if the file is in a Shared Drive or not
- The search string to add to my searchParams to filter out anything not in a Shared Drive.
Also, I saw this question which has an answer suggesting to use the "Advanced Drive Service" but I couldn't find any documentation showing how to actually use it (e.g. a javascript reference for Drive.Drives). The closest I came was this page which does have v2 hidden under there, but it shows JSON representation for the REST API and no indication how that corresponds to what is presumably a Javascript object Drive.Drives nor how to use it.
Since my call to searchFiles is already returning files from Shared Drives I'm also not sure if this answer is still up to date.
答案1
得分: 2
function getPathAllDrivesFromId(fileid) {
var ids = [{id:fileid,name:DriveApp.getFileById(fileid).getName()}];
let r;
do {
r = Drive.Files.get(fileid,{supportsAllDrives:true,supportsTeamDrives:true});
if(r.parents.length > 0) {
//Logger.log(JSON.stringify(r.parents[0]));
ids.push({id:r.parents[0].id,name:DriveApp.getFolderById(r.parents[0].id).getName()});
fileid = r.parents[0].id
}
}while (r.parents.length > 0);
if(ids[ids.length - 1].name == "Drive") {
ids[ids.length - 1].name = Drive.Drives.get(ids[ids.length - 1].id).name;
}
//Logger.log(JSON.stringify(ids));
let path = ids.map(obj => obj.name).flat().reverse().join(' / ')
//Logger.log(path);
return path;
}
英文:
function getPathAllDrivesFromId(fileid) {
var ids = [{id:fileid,name:DriveApp.getFileById(fileid).getName()}];
let r;
do {
r = Drive.Files.get(fileid,{supportsAllDrives:true,supportsTeamDrives:true});
if(r.parents.length > 0) {
//Logger.log(JSON.stringify(r.parents[0]));
ids.push({id:r.parents[0].id,name:DriveApp.getFolderById(r.parents[0].id).getName()});
fileid = r.parents[0].id
}
}while (r.parents.length > 0);
if(ids[ids.length - 1].name == "Drive") {
ids[ids.length - 1].name = Drive.Drives.get(ids[ids.length - 1].id).name;
}
//Logger.log(JSON.stringify(ids));
let path = ids.map(obj => obj.name).flat().reverse().join(' / ')
//Logger.log(path);
return path;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论