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

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

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

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

成功した場合、レスポンスの本文には、リクエストによって追加または更新されたラベルが含まれます。これらは、Label タイプの modifiedLabels オブジェクト内に存在します。

次のコードサンプルは、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: 変更するフィールドの fieldIdfieldId を見つけるには、Google Drive Labels API を使用してラベルを取得します。
  • LABEL_ID: 変更するラベルの labelId
  • FILE_ID: ラベルが変更されるファイルの fileId