本頁面說明如何在單一 Google 雲端硬碟檔案上設定 Field
標籤。
如要透過設定檔案標籤將中繼資料新增至檔案,請使用 files.modifyLabels
方法。要求主體包含 ModifyLabelsRequest
執行個體,用於修改檔案的標籤組合。要求可能包含某些會自動套用的修改內容。也就是說,如果任何修改無效,則整個更新都會失敗,且不會套用任何 (可能相依) 的變更。
ModifyLabelsRequest
包含 LabelModification
的執行個體,可用來修改檔案標籤。也可能包含 FieldModification
的執行個體,這是為了修改標籤欄位。
如果成功,回應主體會包含要求新增或更新的標籤。這些函式存在於 Label
類型的 modifiedLabels
物件中。
範例
以下程式碼範例說明如何使用文字欄位的 fieldId
,在檔案上設定此 Field
的值。檔案一開始設定 Field
標籤時,會將標籤套用至檔案。接著,您可以取消設定單一欄位,或移除與標籤相關聯的所有欄位。詳情請參閱「取消設定檔案的標籤欄位」和「從檔案移除標籤」。
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 擷取欄位資料類型。