ค้นหาไฟล์ที่มีป้ายกํากับหรือค่าในช่องที่ต้องการ

จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ

หน้านี้จะอธิบายวิธีค้นหาไฟล์ที่มีป้ายกํากับหรือค่าในช่องที่เจาะจง

ประเภทของช่องป้ายกํากับ

ช่องป้ายกํากับของ Google ไดรฟ์มีการพิมพ์แต่ละประเภทที่รองรับการจัดทําดัชนีและความหมายในการค้นหาที่แตกต่างกัน ตารางต่อไปนี้แสดงประเภทข้อมูล ที่พร้อมใช้งาน

ประเภท ตัวเลือกประเภทป้ายกํากับ โอเปอเรเตอร์การค้นหาที่รองรับ
ข้อความ ตัวเลือกข้อความ is null, is not null, =, contains, starts with
ข้อความแบบยาว ตัวเลือกข้อความแบบยาว is null, is not null, contains
จำนวนเต็ม ตัวเลือกจํานวนเต็ม is null, is not null, =, !=, <, >, <=, >=
วันที่ ตัวเลือกวันที่ is null, is not null, =, !=, <, >, <=, >=
การเลือก ตัวเลือกการเลือก is null, is not null, =, !=
ผู้ใช้ ตัวเลือกสําหรับผู้ใช้ is null, is not null, =, !=
รายการตัวเลือก SelectOptions (ที่มี 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. ค้นหาตามช่องเดียว

คุณสามารถเขียนคําค้นหาให้ตรงกับค่าในช่องที่คาดไว้ ตารางต่อไปนี้แสดงการค้นหาในช่องที่ถูกต้อง

สิ่งที่คุณต้องการค้นหา สตริงการค้นหา
รายการที่ความคิดเห็นตั้งค่าเป็น "สวัสดี" labels/contract.comment = '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

ตัวอย่าง

ตัวอย่างนี้อธิบายวิธีแสดงรายการไฟล์ทั้งหมดที่มีป้ายกํากับหรือค่าในช่องที่เฉพาะเจาะจงจากทรัพยากรไฟล์ของ Google ไดรฟ์

หากต้องการรวม labelInfo ไว้ในการตอบกลับ คุณต้องระบุสิ่งต่อไปนี้ด้วย

  • includeLabels เป็นรายการ labelId ที่คั่นด้วยคอมมา

  • labelInfo ในพารามิเตอร์ fields เพื่อระบุว่าคุณต้องการให้ labelInfo แสดงผลในการตอบกลับไฟล์

ตัวอย่างนี้ใช้ labelId อย่างน้อย 1 รายการเพื่อค้นหาและส่งคืนไฟล์ที่ตรงกับข้อมูลป้ายกํากับที่ขอ และยังใช้เมธอด files.list ด้วย

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)");

Node.js

/**
* Search for Drive files with specific labels
* @return{obj} file list with labelInfo
**/
async function getFileWithSpecificLabels() {
  // 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;
  }
}