谷歌脚本中有没有办法通过文件夹名称的文件夹快捷方式访问文件夹中的文件?



我添加了驱动器中文件夹的快捷方式,指向工作环境中的共享文件夹。在脚本中,我可以访问共享文件夹,但folder.searchFolders找不到快捷方式文件夹。

编辑:很抱歉缺少细节,这是我用例的概述:

我有一个名为 Root 的根文件夹,它在我的应用程序中是静态的。 在此内部,有很多指向外部文件夹的快捷方式。我的应用程序知道特定快捷方式的名称(文件夹名称(,并希望通过搜索名称来获取对文件夹的引用,以便访问文件夹中的所有文件。

---快捷键:aaa

---快捷键:bbb

目前,我可以通过getFilesByName("aaa")访问shurtcut,但这是我所能得到的,找不到其中的文件。

我相信你的目标如下。

  • 您希望使用 Google Apps 脚本在特定文件夹中检索快捷方式的文件夹。

为此,这个答案怎么样?

模式 1:

在此模式中,快捷方式文件是使用云端硬盘服务检索的。在这种情况下,请使用searchFiles而不是searchFolders。并且包括 mimeType 的application/vnd.google-apps.shortcut,用于在搜索查询中搜索快捷方式文件。

示例脚本:

const folderId = "###";  // Please set the folder ID.
const params = "title = 'samples_for_SlideSearcher' and mimeType = 'application/vnd.google-apps.shortcut'";
const files = DriveApp.getFolderById(folderId).searchFiles(params);
while (files.hasNext()) {
const f = files.next();
console.log(f.getName())
}
  • 在这种情况下,将检索快捷方式名称。

模式 2:

在此模式中,快捷方式文件是使用驱动器 API 检索的。使用云端硬盘服务检索快捷文件时,在当前阶段,似乎无法包含快捷键的目标信息。因此,为了检索目标信息,我想提出这种模式作为另一种方法。

示例脚本:

使用此脚本时,请在高级 Google 服务中启用云端硬盘 API。

const folderId = "###";  // Please set the folder ID.
const params = `title = 'samples_for_SlideSearcher' and mimeType = 'application/vnd.google-apps.shortcut' and '${folderId}' in parents`;
const items = Drive.Files.list({fields: "items(id,title,shortcutDetails)", q: params, includeItemsFromAllDrives: true, supportsAllDrives: true}).items;
const res = items.map(e => ({id: e.id, name: e.title, shortcutDetails: e.shortcutDetails}));
console.log(JSON.stringify(res))
  • 在这种情况下,将检索以下值。

    [
    {
    "id": "###",  // File ID of the shortcut.
    "name": "###",  // Filename of the shortcut.
    "shortcutDetails": {
    "targetMimeType": "application/vnd.google-apps.folder",  // Folder ID of the target folder.
    "targetId": "###"  // Folder ID of the target folder.
    }
    }
    ]
    
  • 关于const items = Drive.Files.list({fields: "items(id,title,shortcutDetails)", q: params, includeItemsFromAllDrives: true, supportsAllDrives: true}).items;,当无法检索文件列表时,请通过添加驱动器 ID 修改为const items = Drive.Files.list({fields: "items(id,title,shortcutDetails)", q: params, includeItemsFromAllDrives: true, supportsAllDrives: true, corpora: "drive", driveId: "###"}).items;并再次测试。

注意:

  • 在当前阶段,云端硬盘服务可以使用共享云端硬盘。
  • 如果要通过在文件名中包含值进行搜索,请将title = 'samples_for_SlideSearcher'修改为title contains 'samples_for_SlideSearcher'

引用:

  • 驱动器服务
  • 搜索查询词
  • 高级谷歌服务
  • 文件:列表

最新更新