このページでは、単一の Google ドライブ ファイルにラベル Field
を設定する方法について説明します。
ファイルラベルを設定してファイルにメタデータを追加するには、files.modifyLabels
メソッドを使用します。リクエスト本文には、ファイルのラベルセットを変更する ModifyLabelsRequest
のインスタンスが含まれています。リクエストには、アトミックに適用される複数の変更が含まれている場合があります。つまり、いずれかの変更が有効でない場合、更新全体が失敗し、(依存する可能性のある)変更はいずれも適用されません。
ModifyLabelsRequest
には、ファイルのラベルの変更である LabelModification
のインスタンスが含まれます。また、ラベルのフィールドに対する変更である FieldModification
のインスタンスを含めることもできます。
成功した場合、レスポンスの本文には、リクエストによって追加または更新されたラベルが含まれます。これらは 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
。
メモ
- フィールドのないラベルを設定するには、
fieldModifications
を指定せずにlabelModifications
を適用します。 - 選択フィールドのオプションの値を設定するには、Drive Labels API でラベルスキーマを取得することで取得できる値の
Choice
ID を使用します。 - 値のリストをサポートする
Field
にのみ、複数の値を設定できます。設定しない場合は、400: Bad Request
エラー レスポンスが返されます。 - 選択した
Field
に適切な値の型(整数、テキスト、ユーザーなど)を設定します。値を設定しないと、400: Bad Request
エラー レスポンスが返されます。フィールドのデータ型は、Drive Labels API を使用して取得できます。