หน้านี้อธิบายวิธีค้นหาไฟล์ที่มีป้ายกำกับหรือค่าฟิลด์ที่เฉพาะเจาะจง
ประเภทฟิลด์ป้ายกำกับ
ฟิลด์ป้ายกำกับของ Google ไดรฟ์เป็นแบบกำหนดประเภทอย่างชัดเจน โดยแต่ละประเภทรองรับ การจัดทำดัชนีและการค้นหาเชิงความหมายที่แตกต่างกัน ตารางต่อไปนี้แสดงประเภทข้อมูลที่ใช้ได้
ประเภท | ตัวเลือกประเภทป้ายกำกับ | โอเปอเรเตอร์การค้นหาที่รองรับ |
---|---|---|
ข้อความ | TextOptions | is null, is not null, =, contains, starts with |
ข้อความยาว | LongTextOptions | is null, is not null, contains |
จำนวนเต็ม | 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 labels
not 'labels/contract' in labels
นอกจากนี้ คุณยังค้นหารายการที่มี (หรือไม่มี) การตั้งค่าฟิลด์ที่เฉพาะเจาะจงได้ด้วย
labels/contract.comment IS NOT NULL
labels/contract.comment IS NULL
2. ค้นหาตามฟิลด์ที่มีค่าเดียว
คุณเขียนคำค้นหาเพื่อให้ตรงกับค่าฟิลด์ที่คาดไว้ได้ ตารางต่อไปนี้ แสดงการค้นหาฟิลด์ที่ถูกต้อง
สิ่งที่คุณต้องการค้นหา | สตริงการค้นหา |
---|---|
รายการที่มีความคิดเห็นเป็น "hello" | labels/contract.comment = 'hello' |
ไฟล์ที่ความคิดเห็นขึ้นต้นด้วย "hello" | labels/contract.comment STARTS WITH 'hello' |
ไฟล์ที่ดำเนินการสถานะ | labels/contract.status = '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_leads
NOT '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 ของค่ายเพลงที่จะคืนเงิน