本頁面說明如何為單一 Google 雲端硬碟檔案設定標籤 Field
。如要透過設定檔案標籤為檔案新增中繼資料,請使用 files.modifyLabels
方法。
您還需要指定:
待修改欄位的
fieldId
。這個欄位的新
value
。待修改標籤的
labelId
。修改標籤的檔案
fileId
。
這個範例會使用文字欄位的 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]});
Node.js
/**
* Set a label with a text field on a Drive file
* @return{obj} updated label data
**/
async function modifyLabelTextField() {
// 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;
}
}
Notes
- 如要設定沒有欄位的標籤,請套用
labelModifications
,此屬性不含fieldModifications
。 - 如要為選取欄位設定值,請使用在 Drive Drive Labels API 中擷取標籤結構定義後,取得的值選擇 ID。
- 只有支援值清單的
Field
可以設定多個值,否則您會收到400: Bad Request
錯誤回應。 - 如要找出
fieldId
,請使用 Drive Drive Labels API 擷取標籤。 - 請為所選的
field
設定適當的值類型 (例如整數、文字、使用者等),否則您會收到400: Bad Request
錯誤回應。您可以使用 Drive Drive Labels API 擷取欄位資料類型。