本頁說明如何在單一 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 擷取欄位資料類型。