ファイルのラベル フィールドの設定を解除する

このページでは、単一の Google ドライブ ファイルでラベル Fieldの設定を解除する方法について説明します。

ファイルラベルの設定を解除してファイルからメタデータを削除するには、 files.modifyLabels メソッドを使用します。リクエストの本文には、ファイルのラベルセットを変更するための のインスタンス ModifyLabelsRequest が含まれています。リクエストには、アトミックに適用される複数の変更が含まれる場合があります。つまり、変更が無効な場合、更新全体が失敗し、依存関係のある変更は適用されません。

ModifyLabelsRequest には、ファイルのラベルの変更である LabelModification のインスタンスが含まれています。ラベルのフィールドの変更である FieldModification のインスタンスが含まれる場合もあります。フィールドの値を設定解除するには、FieldModification.unsetValuesTrue に設定します。

成功した場合、レスポンス の本文には、リクエストによって追加または更新されたラベルが含まれます。これらは、 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',
      requestBody: labelModificationRequest,
    });
    return updateResponse;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

次のように置き換えます。

  • FIELD_ID: 変更するフィールドの fieldId。 を確認するには、fieldId Google Drive Labels API を使用してラベルを取得します。
  • LABEL_ID: 変更するラベルの labelId
  • FILE_ID:ラベルが 変更されるファイルのfileId