本頁說明如何取消設定單一 Google 雲端硬碟檔案的標籤 Field。
如要透過取消設定檔案標籤,從檔案中移除中繼資料,請使用 files.modifyLabels 方法。要求主體包含 ModifyLabelsRequest 的例項,可修改檔案的標籤集。要求可能包含多項修改內容,且會以不可分割的方式套用。也就是說,如果任何修改無效,整個更新就會失敗,且不會套用任何 (可能相依的) 變更。
ModifyLabelsRequest 包含 LabelModification 的執行個體,這是對檔案標籤的修改。也可能包含 FieldModification 執行個體,這是對標籤欄位的修改。如要取消設定欄位的值,請將 FieldModification.unsetValues 設為 True。
如果成功,回應主體會包含要求新增或更新的標籤。這些屬性位於 Label 類型的 modifiedLabels 物件中。
範例
以下程式碼範例說明如何使用 fieldId 和 labelId,取消設定相關聯 fileId 的欄位值。舉例來說,如果標籤同時包含文字和使用者欄位,取消設定文字欄位會將其從標籤中移除,但使用者欄位不會受到影響。移除標籤則會一併刪除與標籤相關聯的文字和使用者欄位。詳情請參閱「從檔案移除標籤」。
Java
LabelFieldModification fieldModification = new LabelFieldModification()
.setFieldId("FIELD_ID")
.setUnsetValues(true);
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',
'unsetValues': True
}
label_modification = {
'labelId': 'LABEL_ID',
'fieldModifications': [field_modification]
}
modified_labels = drive_service.files().modifyLabels(
fileId="FILE_ID",
body={'labelModifications': [label_modification]}
).execute()
Node.js
/**
* Unset a label with a field on a Drive file
* @return{obj} updated label data
**/
async function unsetLabelField() {
// 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',
'unsetValues': true,
};
const labelModification = {
'labelId': 'LABEL_ID',
'fieldModifications': [fieldModification],
};
const labelModificationRequest = {
'labelModifications': [labelModification],
};
try {
const updateResponse = await service.files.modifyLabels({
fileId: 'FILE_ID',
requestBody: labelModificationRequest,
});
return updateResponse;
} catch (err) {
// TODO (developer) - Handle error
throw err;
}
}
更改下列內容:
- FIELD_ID:要修改的欄位
fieldId。如要找出fieldId,請使用 Google 雲端硬碟 Labels API 擷取標籤。 - LABEL_ID:要修改的標籤
labelId。 - FILE_ID:要修改標籤的檔案
fileId。