是否可以使用 Google Drive API 使用 Google Apps 脚本列出包含特定分类标签的所有文件?

问题描述 投票:0回答:1

应用程序脚本缺乏文档,chatGPT 的知识不包括 Google Drive 标签功能,Google 支持毫无用处。

我正在尝试在 Google Apps 脚本上实现一个脚本,该脚本会定期扫描 Google Workspace 帐户上的某些特定共享驱动器,以查找应用了特定标签的文件,然后检查文件的上次修改日期,然后在给定时间段后删除它们时间。

最后一部分一切正常,但我无法列出具有特定标签的文件。文档似乎表明这是可能的:https://developers.google.com/drive/api/guides/search-labels

但我无法翻译 Google Apps 脚本的示例。似乎只有 Google Drive API v2 也可用于 Google Apps 脚本,但尚不清楚标签功能是否仅在 API v3 中可用。 Google Drive Labels API 似乎仅在操作域上的标签时有帮助,而不是在 Google Drive 上的文件上。

您知道使用 Google Apps 脚本是否可以实现这一点吗?如果可以,如何实现?

谢谢!

来自 Node.js 中的 Google 文档:

/**
* Search for Drive files with specific labels
* @return{obj} file list with labelInfo
**/
async function searchForFileWithLabels() {
  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app

  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});
  const service = google.drive({version: 'v3', auth});
  try {
    const fileList = await service.files.list({
      includeLabels: 'LABEL_1_ID,LABEL_2_ID',
      q: '\'labels/LABEL_1_ID\' in labels and \'labels/LABEL_2_ID\' in labels',
      fields:'files(labelInfo, id)',
    });
    return file;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }

我已经尝试过:

async function searchForFileWithLabels() {
  try {
    const fileList = await Drive.Files.list({
      includeLabels: 'Test',
      q: '\'labels/Test\' in labels',
      fields:'files(labelInfo, id)',
    });
    return file;
  } catch (err) {
    throw err;
  }
}

但这失败了:GoogleJsonResponseException:对drive.files.list的API调用失败并出现错误:无效查询

google-apps-script google-drive-api label google-workspace
1个回答
0
投票

我也得到了这一点,但问题是每个标签可以有多个徽章(内部、外部、敏感等)。似乎不可能以编程方式查询低于此级别的数据,尽管可以这样做这在用户界面中。

function searchForFileWithLabels() {
  try {
    
    var driveService = DriveApp; // Google Drive service in Google Apps Script
    
    // Search for files with specific labels
    var query = "'labels/YOURLABLEIDHERE' in labels";
    var files = driveService.searchFiles(query);
    
    while (files.hasNext()) {
      var file = files.next();
      Logger.log(file.getName())
    }

  } catch (err) {
    // do something here
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.