从共享驱动器导出文件夹信息; Pagetoken的问题

发布于 2025-02-06 15:33:06 字数 1749 浏览 3 评论 0 原文

我正在尝试将共享驱动器中的文件夹导出文件夹名称,文件夹ID,文件夹URL和FilePath。这部分有效,但我仅限于100个项目。我正在尝试使用Pagetoken/NextPagetoken功能来提取所有项目,但是它似乎不起作用,并且继续仅返回100个项目。我在此解决方案是类似的问题。

function getFolderURLs() {
  var folders, pageToken;
  var foldersArray = [["Folder Name", "Link", "Folder ID", "Filepath"]]
  do {
    try {
    var optionalArgs  = {
    supportsAllDrives: true,
    includeItemsFromAllDrives: true,
    pageToken: pageToken,
    maxResults: 100,
    q: '"132vqY8oyd6xKpnxxxxxxxxxxx" in parents and trashed = false and mimeType = "application/vnd.google-apps.folder"'
  }
  var folders = Drive.Files.list(optionalArgs);
  var allFolders = folders.items
  if (!allFolders || allFolders.length === 0) {
    Logger.log ('No folders found.');
    return
  }
  for (i = 0; i < allFolders.length; i++) {
    var folder = allFolders[i];
    var name = folder.title
    var link = folder.alternateLink
    var id = folder.id

    var parentID = Drive.Files.get(folder.id,{
      supportsAllDrives: true,
    }).parents[0].id
    var parent = Drive.Files.get(parentID,{
      supportsAllDrives: true,
      includesItemsFromAllDrives: true,
    }).title
    var filepath = `${parent}/${name}`

    foldersArray.push([name, link, id, filepath])
  }
  pageToken = folders.nextPageToken;
} catch (err) {
  Logger.log('Failed with error %s', err.message);
}
  }
   while (pageToken);
    var ss = SpreadsheetApp.create('Clients Folder ID Listing');
    var sheet = ss.getActiveSheet();
    sheet.getRange(1,1,foldersArray.length, 4).setValues(foldersArray)
    Logger.log(foldersArray)
}

I'm attempting to export the folder name, folder id, folder url, and filepath for folders within a Shared Drive. This part works, but I'm limited to 100 items. I'm attempting to use the pageToken/nextPageToken functionality to pull all items, but it doesn't seem to work and continues returning only 100 items. I'm using the same basic structure as google uses in their example code for this purpose and was also referencing this solution to a similar problem.

function getFolderURLs() {
  var folders, pageToken;
  var foldersArray = [["Folder Name", "Link", "Folder ID", "Filepath"]]
  do {
    try {
    var optionalArgs  = {
    supportsAllDrives: true,
    includeItemsFromAllDrives: true,
    pageToken: pageToken,
    maxResults: 100,
    q: '"132vqY8oyd6xKpnxxxxxxxxxxx" in parents and trashed = false and mimeType = "application/vnd.google-apps.folder"'
  }
  var folders = Drive.Files.list(optionalArgs);
  var allFolders = folders.items
  if (!allFolders || allFolders.length === 0) {
    Logger.log ('No folders found.');
    return
  }
  for (i = 0; i < allFolders.length; i++) {
    var folder = allFolders[i];
    var name = folder.title
    var link = folder.alternateLink
    var id = folder.id

    var parentID = Drive.Files.get(folder.id,{
      supportsAllDrives: true,
    }).parents[0].id
    var parent = Drive.Files.get(parentID,{
      supportsAllDrives: true,
      includesItemsFromAllDrives: true,
    }).title
    var filepath = `${parent}/${name}`

    foldersArray.push([name, link, id, filepath])
  }
  pageToken = folders.nextPageToken;
} catch (err) {
  Logger.log('Failed with error %s', err.message);
}
  }
   while (pageToken);
    var ss = SpreadsheetApp.create('Clients Folder ID Listing');
    var sheet = ss.getActiveSheet();
    sheet.getRange(1,1,foldersArray.length, 4).setValues(foldersArray)
    Logger.log(foldersArray)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‖放下 2025-02-13 15:33:06

我相信您的目标如下。

  • 您想在特定文件夹中检索文件夹列表。
  • 特定文件夹具有多个子文件夹。
  • 您想使用Google Apps脚本来实现此目标。

当我看到您的脚本时, drive.files.get 在循环中使用。在这种情况下,过程成本可能很高。而且,在您的脚本中,似乎无法检索子文件夹。

在这种情况下,以下示例脚本怎么样?在此答案中,我使用Google Apps脚本库来检索使用Google Apps脚本的文件和文件夹列表。我为这种情况创建了这个库。

用法:

1。安装库。

请安装Google Apps脚本库。您可以在

2。启用驱动API。

该脚本使用Drive API。因此,请在高级Google Services 上启用驱动器API 。

3.示例脚本:

function myFunction() {
  const folderId = "###"; // Please set the folder ID.

  const obj = FilesApp.getAllFoldersInFolder(folderId);
  if (obj.id.length == 0) return;
  const foldersArray = [["Folder Name", "Link", "Folder ID", "Filepath"], ...obj.name.map((e, i) => {
    const folderName = e.pop();
    const folderId = obj.id[i].pop();
    return [folderName, `https://drive.google.com/drive/folders/${folderId}`, folderId, e.join("/")];
  })];
  const sheet = SpreadsheetApp.create('Clients Folder ID Listing').getActiveSheet();
  sheet.getRange(1, 1, foldersArray.length, 4).setValues(foldersArray);
}
  • 运行此脚本时,从特定文件夹中检索文件夹列表。在这种情况下,包括子文件夹。而且,将“文件夹名称”,“链接”,“文件夹ID”,“ filepath”的值放入创建的电子表格中。
  • 在此脚本中,可以检索共享驱动器中的文件夹列表。当我对此进行测试时,我确认可以检索一个包括100多个文件夹的文件夹列表。

参考:

I believe your goal is as follows.

  • You want to retrieve the folder list in the specific folder.
  • The specific folder has multiple subfolders.
  • You want to achieve this using Google Apps Script.

When I saw your script, Drive.Files.get is used in a loop. In this case, the process cost might be high. And, in your script, it seems that the subfolders cannot be retrieved.

In this case, how about the following sample script? In this answer, I used a Google Apps Script library for retrieving the file and folder list using Google Apps Script. I created this library for like this situation.

Usage:

1. Install library.

Please install the Google Apps Script library. You can see the method for installing it at here.

2. Enable Drive API.

This script uses Drive API. So, please enable Drive API at Advanced Google services.

3. Sample script:

function myFunction() {
  const folderId = "###"; // Please set the folder ID.

  const obj = FilesApp.getAllFoldersInFolder(folderId);
  if (obj.id.length == 0) return;
  const foldersArray = [["Folder Name", "Link", "Folder ID", "Filepath"], ...obj.name.map((e, i) => {
    const folderName = e.pop();
    const folderId = obj.id[i].pop();
    return [folderName, `https://drive.google.com/drive/folders/${folderId}`, folderId, e.join("/")];
  })];
  const sheet = SpreadsheetApp.create('Clients Folder ID Listing').getActiveSheet();
  sheet.getRange(1, 1, foldersArray.length, 4).setValues(foldersArray);
}
  • When this script is run, the folder list is retrieved from the specific folder. In this case, the subfolders are included. And, the values of "Folder Name", "Link", "Folder ID", "Filepath" are put into the created Spreadsheet.
  • In this script, the folder list in the shared drive can be retrieved. When I tested this, I confirmed that a folder list including more than 100 folders could be retrieved.

Reference:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文