为文件设置标签字段

本页面介绍了如何设置标签 Field Google 云端硬盘文件。

如需通过设置文件标签向文件添加元数据,请使用 files.modifyLabels 方法。通过 请求正文 包含 ModifyLabelsRequest 修改文件上的一组标签。该请求可能包含 以原子方式应用的修改也就是说, 则整个更新会失败, 依赖性)应用更改。

ModifyLabelsRequest 包含 LabelModification 修改文件标签它还可能包含 / FieldModification 这是对标签字段的修改

如果成功,响应将 body包含 请求添加或更新的标签。它们都存在于 类型为 LabelmodifiedLabels 对象。

示例

以下代码示例展示了如何使用文本字段的 fieldId 来设置 此Field的值(在 文件。最初在文件上设置标签 Field 时,它会将该标签应用于 文件。然后,您可以取消设置单个字段或移除所有与 标签。有关详情,请参阅取消为 文件从单个文件中移除标签 文件

Java

LabelFieldModification fieldModification =
new LabelFieldModification().setFieldId("FIELD_ID").setSetTextValues(ImmutableList.of("VALUE"));

ModifyLabelsRequest modifyLabelsRequest =
  new ModifyLabelsRequest()
      .setLabelModifications(
          ImmutableList.of(
              new LabelModification()
                .setLabelId("LABEL_ID")
                .setFieldModifications(ImmutableList.of(fieldModification))));

ModifyLabelsResponse modifyLabelsResponse = driveService.files().modifyLabels("FILE_ID", modifyLabelsRequest).execute();

Python

field_modification = {'fieldId':'FIELD_ID','setTextValues':['VALUE']}
label_modification = {'labelId':'LABEL_ID', 'fieldModifications':[field_modification]}

modified_labels = drive_service.files().modifyLabels(fileId="FILE_ID", body = {'labelModifications' : [label_modification]}).execute();

Node.js

/**
* Set a label with a text field on a Drive file
* @return{obj} updated label data
**/
async function setLabelTextField() {
  // 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});
  const fieldModification = {
    'fieldId': 'FIELD_ID',
    'setTextValues': ['VALUE'],
  };
  const labelModification = {
    'labelId': 'LABEL_ID',
    'fieldModifications': [fieldModification],
  };
  const labelModificationRequest = {
    'labelModifications': [labelModification],
  };
  try {
    const updateResponse = await service.files.modifyLabels({
      fileId: 'FILE_ID',
      resource: labelModificationRequest,
    });
    return updateResponse;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

替换以下内容:

  • FIELD_ID:要修改的字段的 fieldId。查找 fieldId,请使用 Google 云端硬盘标签 API
  • VALUE:此字段的新 value
  • LABEL_ID:要修改的标签的 labelId
  • FILE_ID:标签所属文件的 fileId

备注

  • 要设置不包含字段的标签,请应用 labelModifications 而不应用任何字段 有 fieldModifications 人。
  • 要为选择字段选项设置值,请使用 值的 Choice ID 可通过从 Cloud Shell 中提取标签架构 Drive Labels API
  • 只有支持值列表的 Field 才能设置多个值, 否则,您将收到 400: Bad Request 错误响应。
  • 为所选 Field 设置适当的值类型(例如整数、文本、 user 等),否则您将收到 400: Bad Request 错误响应。 您可以使用 Drive Labels API