หน้านี้อธิบายวิธีนำป้ายกำกับออกจากไฟล์ Google ไดรฟ์ไฟล์เดียว
หากต้องการนำข้อมูลเมตาของป้ายกำกับไฟล์ออกจากไฟล์ ให้ใช้วิธี
files.modifyLabels
เนื้อหาของคำขอ
มีอินสแตนซ์ของ
ModifyLabelsRequest
เพื่อแก้ไขชุดป้ายกำกับในไฟล์ คำขออาจมีการแก้ไขหลายรายการ
ซึ่งจะใช้พร้อมกัน กล่าวคือ หากการแก้ไขใดไม่ถูกต้อง การอัปเดตทั้งหมดจะไม่สำเร็จและระบบจะไม่ใช้การเปลี่ยนแปลงใดๆ (ซึ่งอาจขึ้นอยู่กับกัน)
ModifyLabelsRequest
มีอินสแตนซ์ของ
LabelModification
ซึ่งเป็นการแก้ไขป้ายกำกับในไฟล์ นอกจากนี้ยังอาจมีอินสแตนซ์ของ FieldModification
ซึ่งเป็นการแก้ไขฟิลด์ของป้ายกำกับ หากต้องการนำป้ายกำกับออกจากไฟล์ ให้ตั้งค่า FieldModification.removeLabel
เป็น True
หากทำสำเร็จ เนื้อหา
การตอบกลับจะมีป้ายกำกับที่คำขอเพิ่มหรืออัปเดต ซึ่งอยู่ในออบเจ็กต์ modifiedLabels
ประเภท Label
ตัวอย่าง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีใช้ labelId
เพื่อนำฟิลด์ทั้งหมด
ที่เชื่อมโยงกับป้ายกำกับออกโดยใช้ fileId
เช่น หากป้ายกำกับมีทั้งข้อความและฟิลด์ผู้ใช้ การนำป้ายกำกับออกจะเป็นการลบทั้งข้อความและฟิลด์ผู้ใช้ที่เชื่อมโยงกับป้ายกำกับทั้งคู่ ในขณะที่การยกเลิกการตั้งค่าช่องข้อความจะนำช่องข้อความออกจากป้ายกำกับ แต่จะไม่เปลี่ยนแปลงช่องผู้ใช้ ดูข้อมูลเพิ่มเติมได้ที่
ยกเลิกการตั้งค่าฟิลด์ป้ายกำกับในไฟล์
Java
ModifyLabelsRequest modifyLabelsRequest =
new ModifyLabelsRequest()
.setLabelModifications(
ImmutableList.of(
new LabelModification()
.setLabelId("LABEL_ID")
.setRemoveLabel(true)));
ModifyLabelsResponse modifyLabelsResponse = driveService.files().modifyLabels("FILE_ID", modifyLabelsRequest).execute();
Python
label_modification = {'labelId':'LABEL_ID', 'removeLabel': True]}
modified_labels = drive_service.files().modifyLabels(fileId="FILE_ID", body = {'labelModifications' : [label_modification]}).execute();
Node.js
/**
* Remove a label on a Drive file
* @return{obj} updated label data
**/
async function removeLabel() {
// 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 labelModification = {
'labelId': 'LABEL_ID',
'removeLabel': True,
};
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;
}
แทนที่ค่าต่อไปนี้
- LABEL_ID:
labelId
ของป้ายกำกับที่จะแก้ไข หากต้องการค้นหา ป้ายกำกับในไฟล์ ให้ใช้วิธีfiles.listLabels
- FILE_ID:
fileId
ของไฟล์ที่มีการแก้ไขป้ายกำกับ