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

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

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

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

Type ตัวเลือกประเภทป้ายกำกับ โอเปอเรเตอร์การค้นหาที่รองรับ
ข้อความ 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, =, !=
รายการตัวเลือก ตัวเลือก (มี 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'
ไฟล์ที่ actions_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 ของป้ายกำกับที่จะแสดงผล