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 memiliki jenis yang kuat dengan setiap jenis 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
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. Menelusuri 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. Menelusuri berdasarkan kolom bernilai tunggal

Anda dapat menulis kueri penelusuran untuk mencocokkan nilai kolom yang diharapkan. Tabel berikut menunjukkan kueri kolom yang valid:

Yang ingin Anda kueri String kueri
Item tempat komentar ditetapkan ke "halo" labels/contract.comment = 'hello'
File tempat komentar dimulai dengan "halo" labels/contract.comment STARTS WITH 'hello'
File tempat statusnya dieksekusi labels/contract.status = 'executed'
File tempat statusnya tidak dieksekusi labels/contract.status != 'executed'
File tempat execution_date sebelum tanggal tertentu labels/contract.execution_date < '2020-06-22'
File tempat value_usd (bilangan bulat) 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. Menelusuri berdasarkan kolom dengan kolom bernilai banyak (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 mencantumkan semua file dengan label atau nilai kolom tertentu dari resource file Drive. Contoh 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: Yang pertama labelId dari label yang akan ditampilkan.
  • LABEL_2_ID: labelId kedua dari label yang akan ditampilkan.