Menelusuri file dengan label atau nilai kolom tertentu

Halaman ini menjelaskan cara menelusuri file dengan label atau nilai kolom tertentu yang diterapkan.

Jenis kolom label

Kolom label Google Drive diketik dengan ketat dan setiap jenisnya mendukung semantik pengindeksan dan penelusuran yang berbeda. Tabel berikut menunjukkan jenis data yang tersedia.

Jenis Opsi jenis label Operator penelusuran yang didukung
Teks TextOptions is null, is not null, =, contains, starts with
Teks Panjang LongTextOptions is null, is not null, contains
Bilangan bulat IntegerOptions is null, is not null, =, !=, <, >, <=, >=
Tanggal DateOptions is null, is not null, =, !=, <, >, <=, >=
Pilihan SelectionOptions is null, is not null, =, !=
Pengguna UserOptions is null, is not null, =, !=
Daftar Pilihan SelectionOptions (dengan max_entries > 1) is null, is not null, in, not in
Daftar Pengguna UserOptions (dengan max_entries > 1) is null, is not null, in, not in

1. Melakukan penelusuran berdasarkan keberadaan label atau kolom

Anda dapat menelusuri item tempat label tertentu telah (atau belum) diterapkan:

  • 'labels/contract' in labels
  • not 'labels/contract' in labels

Anda juga dapat menelusuri item tempat kolom tertentu telah (atau belum) ditetapkan:

  • labels/contract.comment IS NOT NULL
  • labels/contract.comment IS NULL

2. Melakukan penelusuran berdasarkan kolom bernilai tunggal

Anda dapat menulis kueri penelusuran agar cocok dengan nilai kolom yang diharapkan. Tabel berikut menunjukkan kueri kolom yang valid:

Hal yang ingin Anda kueri String kueri
Item dengan komentar yang ditetapkan ke "halo" labels/contract.comment = 'hello'
File yang komentarnya diawali dengan "halo" labels/contract.comment STARTS WITH 'hello'
File dengan status dijalankan labels/contract.status = 'executed'
File dengan status tidak dijalankan labels/contract.status != 'executed'
File dengan execution_date jatuh sebelum tanggal tertentu labels/contract.execution_date < '2020-06-22'
File dengan value_usd (integer) kurang dari nilai tertentu labels/contract.value_usd < 2000
File tempat client_contact ditetapkan ke alamat email tertentu labels/contract.client_contact = 'alex@altostrat.com'

3. Telusuri berdasarkan kolom dengan kolom multinilai (seperti ListOptions.max_entries > 1)

Kolom yang mendukung beberapa nilai hanya dapat dikueri menggunakan operator IN:

  • 'EMAIL_ADDRESS' IN labels/project.project_leads
  • NOT 'EMAIL_ADDRESS' IN labels/project.project_leads

Contoh

Contoh kode berikut menunjukkan cara menggunakan satu atau beberapa labelId untuk menampilkan daftar semua file dengan label atau nilai kolom tertentu dari resource file Drive. Class ini juga menggunakan metode files.list. Isi permintaan harus kosong.

Jika ingin menyertakan labelInfo dalam respons, Anda juga harus menentukan:

  • includeLabels sebagai daftar ID yang dipisahkan koma.

  • labelInfo dalam parameter fields untuk menunjukkan bahwa Anda ingin labelInfo ditampilkan dalam includeLabels.

Jika berhasil, isi respons akan berisi daftar file.

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;
  }

Ganti kode berikut:

  • LABEL_1_ID: labelId pertama dari label yang akan ditampilkan.
  • LABEL_2_ID: labelId kedua dari label yang akan ditampilkan.