องค์กรของคุณสามารถมีป้ายกำกับได้หลายป้ายกำกับ โดยป้ายกำกับจะมีหลายช่อง
Google Drive Labels API มีทรัพยากร
labels เพื่อให้สามารถอ่านป้ายกำกับได้
เอกสารนี้อธิบายวิธีค้นหาและดึงข้อมูลป้ายกำกับโดยใช้ Drive Labels API
เมธอด
ทรัพยากร labels มีเมธอดต่อไปนี้สำหรับการอ่านค่าป้ายกำกับ โดยแต่ละเมธอดมีหน้าที่เฉพาะดังนี้
| ช่วง | วิธีการ |
|---|---|
| ป้ายกำกับเดียวตามชื่อทรัพยากร | labels.get |
| ป้ายกำกับทั้งหมด | labels.list |
รับป้ายกำกับตามชื่อทรัพยากร
หากต้องการรับป้ายกำกับเดียวตามชื่อทรัพยากร ให้ใช้
getเมธอดใน
labelsทรัพยากร
คุณต้องระบุ name ของทรัพยากรป้ายกำกับ ซึ่งมีโครงสร้างได้ดังนี้
labels/{id}หรือlabels/{id}@latest: รับป้ายกำกับเวอร์ชันล่าสุดlabels/{id}@published: รับป้ายกำกับเวอร์ชันที่เผยแพร่ในปัจจุบันlabels/{id}@{revisionId}: รับป้ายกำกับที่รหัสเวอร์ชันที่ระบุ
คุณต้องระบุออบเจ็กต์
LabelView เป็น
LABEL_VIEW_FULL เพื่อตั้งค่ามุมมองทรัพยากรที่จะใช้กับคำตอบของป้ายกำกับ
LABEL_VIEW_FULL จะแสดงผลช่องทั้งหมดที่เป็นไปได้
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีใช้ name ของป้ายกำกับเพื่อรับป้ายกำกับเดียวตามชื่อทรัพยากร
Python
# Label name, with or without revision:
#
# Revision specified:
# labels/LABEL_ID@published
# labels/LABEL_ID@latest
# labels/LABEL_ID@1
#
# No revision specified, returns latest revision:
# labels/LABEL_ID
name = 'labels/NAME@published'
# Label view controls level of data in response
view = 'LABEL_VIEW_FULL'
label = service.labels().get(
name=name,
view=view
).execute()
Node.js
// Label name, with or without revision:
//
// Revision specified:
// labels/LABEL_ID@published
// labels/LABEL_ID@latest
// labels/LABEL_ID@1
//
// No revision specified, returns latest revision:
// labels/LABEL_ID
const name = 'labels/NAME@published';
// Label view controls level of data in response
const view = 'LABEL_VIEW_FULL';
service.labels.get({
name: name,
view: view
}, (err, res) => {
if (err) return console.error('The API returned an error: ' + err);
console.log(res);
});
แทนที่ NAME ด้วยชื่อป้ายกำกับ
แสดงป้ายกำกับทั้งหมด
หากต้องการรับรายการป้ายกำกับ ให้ใช้เมธอด
list ในทรัพยากร
labels
นอกจากนี้ คุณยังต้องระบุข้อมูลต่อไปนี้ด้วย
พารามิเตอร์การค้นหา
customerเพื่อกำหนดขอบเขตคำขอรายการนี้ หากไม่ได้ตั้งค่าcustomerระบบจะแสดงผลป้ายกำกับทั้งหมดภายในลูกค้าปัจจุบันออบเจ็กต์
LabelViewเป็นLABEL_VIEW_FULLเพื่อตั้งค่ามุมมองทรัพยากรที่จะใช้กับคำตอบของป้ายกำกับLABEL_VIEW_FULLจะแสดงผลช่องทั้งหมดที่เป็นไปได้
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีใช้พารามิเตอร์การค้นหา customer เพื่อรับรายการป้ายกำกับ
Python
response = service.labels().list(
customer='customers/CUSTOMER',
view='LABEL_VIEW_FULL'
).execute()
Node.js
const params = {
customer: 'customers/CUSTOMER',
view: 'LABEL_VIEW_FULL'
};
service.labels.list(params, (err, res) => {
if (err) return console.error('The API returned an error: ' + err);
const labels = res.data.labels;
if (labels) {
labels.forEach((label) => {
const name = label.name;
const title = label.properties.title;
console.log(`${name}\t${title}`);
});
} else {
console.log('No Labels');
}
});
แทนที่ CUSTOMER ด้วยชื่อลูกค้า