取消设置文件的标签字段

本页介绍了如何取消设置单个 Google 云端硬盘文件上的标签 Field

如需通过取消设置文件标签来移除文件中的元数据,请使用 files.modifyLabels 方法。请求正文包含一个 实例,用于修改文件中的标签集。ModifyLabelsRequest请求可能包含多个以原子方式应用的修改。也就是说,如果任何修改无效,则整个更新都会失败,并且不会应用任何(可能依赖于其他修改的)更改。

The ModifyLabelsRequest 包含一个 LabelModification 实例,该实例是对文件标签的修改。它还可能包含一个 实例 FieldModification ,该实例是对标签字段的修改。如需取消设置字段的值,请将 FieldModification.unsetValues 设置为 True

如果成功,响应 正文会包含 请求添加或更新的标签。这些标签存在于 modifiedLabels 对象(类型为 Label)中。

示例

以下代码示例展示了如何使用 fieldIdlabelId 取消设置关联 fileId 上的字段值。例如,如果标签同时包含文本字段和用户字段,则取消设置文本字段会将其从标签中移除,但用户字段保持不变。而移除标签会删除与该标签关联的文本字段和用户字段。 如需了解详情,请参阅 从文件中移除标签

Java

LabelFieldModification fieldModification =
  new LabelFieldModification().setFieldId("FIELD_ID").setUnsetValues(true);

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','unsetValues':True}
label_modification = {'labelId':'LABEL_ID', 'fieldModifications':[field_modification]}

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

Node.js

/**
* Unset a label with a field on a Drive file
* @return{obj} updated label data
**/
async function unsetLabelField() {
  // 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',
    'unsetValues': True,
  };
  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 云端硬盘 Labels API检索标签。
  • LABEL_ID:要修改的标签的 labelId
  • FILE_ID:要修改标签的文件对应的 fileId