ファイルにラベル フィールドを設定する

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

ファイルラベルを設定してファイルにメタデータを追加するには、 files.modifyLabels メソッドを使用します。「 リクエスト本文 含まれる ModifyLabelsRequest ファイルのラベルセットを変更できます。リクエストには アトミックに適用されます。つまり、変更が加えられていない場合は 更新全体が失敗し、 適用される。

ModifyLabelsRequest には、次のインスタンスが含まれます。 LabelModification これはファイルのラベルに対する変更ですまた、インスタンスが含まれる場合もあります。 / FieldModification これはラベルのフィールドに対する変更です。

成功すると、レスポンスは body に含まれる文字列 リクエストによって追加または更新されたラベルです。これらは同じ Label 型の modifiedLabels オブジェクト。

次のコードサンプルは、テキスト フィールドの fieldId を使用して、 この Field の 表示されます。ファイルに Field というラベルを初期設定すると、ラベルの適用対象 表示されます。その後、1 つのフィールドの設定を解除したり、関連付けられたすべてのフィールドを削除したりできます。 できます。詳しくは、 ファイルラベルを できます

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 Drive Labels API
  • VALUE: このフィールドの新しい value
  • LABEL_ID: 変更するラベルの labelId
  • FILE_ID: ラベルが付与されているファイルの fileId あります。

  • フィールドのないラベルを設定するには、フィールドなしで labelModifications を適用します。 fieldModifications が存在します。
  • 選択フィールドのオプションの値を設定するには、 値の Choice ID ラベルスキーマをフェッチすることで、 Drive Labels API
  • 複数の値を設定できるのは、値のリストをサポートする Field のみです。 そうしないと、400: Bad Request エラー レスポンスが返されます。
  • 選択した Field に適切な値の型(整数、テキスト、 ユーザーなど)を渡すと、400: Bad Request エラー レスポンスが返されます。 フィールドのデータ型は、 Drive Labels API