หน้านี้อธิบายวิธีค้นหาไฟล์ที่มีป้ายกำกับหรือค่าในช่องที่เฉพาะเจาะจง
ประเภทช่องป้ายกำกับ
ช่องป้ายกำกับของ Google ไดรฟ์เป็นประเภทข้อมูลที่กำหนดไว้อย่างชัดเจน โดยแต่ละประเภทจะรองรับการจัดทำดัชนีและความหมายของการค้นหาที่แตกต่างกัน ตารางต่อไปนี้แสดงประเภทข้อมูลที่ใช้ได้
| ประเภท | ตัวเลือกประเภทป้ายกำกับ | โอเปอเรเตอร์การค้นหาที่รองรับ |
|---|---|---|
| ข้อความ | TextOptions | is null, is not null, =, contains, starts with |
| จำนวนเต็ม | IntegerOptions | is null, is not null, =, !=, <, >, <=, >= |
| วันที่ | DateOptions | is null, is not null, =, !=, <, >, <=, >= |
| การเลือก | SelectionOptions | is null, is not null, =, != |
| ผู้ใช้ | UserOptions | is null, is not null, =, != |
| รายการที่เลือก | SelectionOptions (ที่มี max_entries > 1) | is null, is not null, in, not in |
| รายการผู้ใช้ | UserOptions (ที่มี max_entries > 1) | is null, is not null, in, not in |
ตัวอย่างการค้นหา
1. ค้นหาตามการมีอยู่ของป้ายกำกับหรือช่อง
คุณสามารถค้นหารายการที่มี (หรือไม่มี) การใช้ป้ายกำกับที่เฉพาะเจาะจงได้โดยใช้คำค้นหาต่อไปนี้
'labels/contract' in labelsnot 'labels/contract' in labels
นอกจากนี้ คุณยังค้นหารายการที่มี (หรือไม่มี) การตั้งค่าช่องที่เฉพาะเจาะจงได้โดยใช้คำค้นหาต่อไปนี้
labels/contract.comment IS NOT NULLlabels/contract.comment IS NULL
2. ค้นหาตามช่องที่มีค่าเดียว
คุณสามารถเขียนคำค้นหาเพื่อให้ตรงกับค่าในช่องที่คาดไว้ได้ ตารางต่อไปนี้แสดงคำค้นหาในช่องที่ถูกต้อง
| สิ่งที่คุณต้องการค้นหา | สตริงการค้นหา |
|---|---|
| รายการที่มีความคิดเห็นตั้งค่าเป็น "hello" | labels/contract.comment = 'hello' |
| ไฟล์ที่มีความคิดเห็นขึ้นต้นด้วย "hello" | labels/contract.comment STARTS WITH 'hello' |
| ไฟล์ที่มีสถานะเป็น "executed" | labels/contract.status = 'executed' |
| ไฟล์ที่มีสถานะไม่ใช่ "executed" | labels/contract.status != 'executed' |
| ไฟล์ที่มี execution_date ก่อนวันที่ที่เฉพาะเจาะจง | labels/contract.execution_date < '2020-06-22' |
| ไฟล์ที่มี value_usd (จำนวนเต็ม) น้อยกว่าค่าที่เฉพาะเจาะจง | labels/contract.value_usd < 2000 |
| ไฟล์ที่มี client_contact ตั้งค่าเป็นอีเมลที่เฉพาะเจาะจง | labels/contract.client_contact = 'alex@altostrat.com' |
3. ค้นหาตามช่องที่มีช่องหลายค่า (เช่น ListOptions.max_entries > 1)
คุณจะค้นหาช่องที่รองรับหลายค่าได้โดยใช้โอเปอเรเตอร์ IN เท่านั้น
'EMAIL_ADDRESS' IN labels/project.project_leadsNOT 'EMAIL_ADDRESS' IN labels/project.project_leads
ตัวอย่าง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีใช้ labelId อย่างน้อย 1 รายการเพื่อแสดงไฟล์ทั้งหมดที่มีป้ายกำกับหรือค่าในช่องที่เฉพาะเจาะจงจากทรัพยากรไฟล์ในไดรฟ์ นอกจากนี้ยังใช้
files.listเมธอดด้วย เนื้อหาของคำขอต้องว่างเปล่า
หากต้องการรวม labelInfo ไว้ในการตอบกลับ คุณต้องระบุสิ่งต่อไปนี้ด้วย
includeLabelsเป็นรายการรหัสที่คั่นด้วยคอมมาlabelInfoในพารามิเตอร์fieldsเพื่อระบุว่าคุณต้องการให้แสดงผลlabelInfoภายในincludeLabels
หากทำสำเร็จ เนื้อหาการตอบกลับจะมีรายการไฟล์
Java
List<File> fileList = driveService.files().list().setIncludeLabels("LABEL_1_ID,LABEL_2_ID").setFields("items(labelInfo, id)").setQ("'labels/LABEL_1_ID' in labels and 'labels/LABEL_2_ID' in labels").execute().getItems();
Python
file_list = drive_service.files().list(includeLabels="LABEL_1_ID,LABEL_2_ID", q="'labels/LABEL_1_ID' in labels and 'labels/LABEL_2_ID' in labels", fields="items(labelInfo, id)").execute();
Node.js
/**
* Search for Drive files with specific labels
* @return{obj} file list with labelInfo
**/
async function searchForFileWithLabels() {
// 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});
try {
const fileList = await service.files.list({
includeLabels: 'LABEL_1_ID,LABEL_2_ID',
q: '\'labels/LABEL_1_ID\' in labels and \'labels/LABEL_2_ID\' in labels',
fields:'files(labelInfo, id)',
});
return file;
} catch (err) {
// TODO (developer) - Handle error
throw err;
}
แทนที่ค่าต่อไปนี้
- LABEL_1_ID:
labelIdแรกของป้ายกำกับที่จะแสดงผล - LABEL_2_ID:
labelIdที่ 2 ของป้ายกำกับที่จะแสดงผล