سرویس برچسب‌های درایو پیشرفته

با سرویس پیشرفته Google Drive Labels، برچسب‌ها را برای فایل‌ها و پوشه‌های Drive خود ایجاد و مدیریت کنید. با استفاده از این سرویس پیشرفته، می توانید از تمام ویژگی های Drive Labels API در Apps Script استفاده کنید.

برای اعمال یا حذف برچسب‌های Drive، از Advanced Drive Service استفاده کنید.

ارجاع

برای اطلاعات بیشتر درباره این سرویس، به مستندات Google Drive Labels API مراجعه کنید. مانند همه سرویس‌های پیشرفته در Apps Script، سرویس Drive Labels API از اشیاء، روش‌ها و پارامترهای مشابه API عمومی استفاده می‌کند.

برای گزارش مشکلات و یافتن پشتیبانی دیگر، راهنمای پشتیبانی Google Drive Labels API را ببینید.

کد نمونه

کد نمونه زیر از نسخه 2 API استفاده می کند.

لیست برچسب ها

نمونه کد زیر نشان می دهد که چگونه می توان لیستی از برچسب ها را در اختیار کاربر درخواست کننده قرار داد.

advanced/driveLabels.gs
/**
 * List labels available to the user.
 */
function listLabels() {
  let pageToken = null;
  let labels = [];
  do {
    try {
      const response = DriveLabels.Labels.list({
        publishedOnly: true,
        pageToken: pageToken
      });
      pageToken = response.nextPageToken;
      labels = labels.concat(response.labels);
    } catch (err) {
      // TODO (developer) - Handle exception
      console.log('Failed to list labels with error %s', err.message);
    }
  } while (pageToken != null);

  console.log('Found %d labels', labels.length);
}

یک برچسب دریافت کنید

نمونه کد زیر نشان می دهد که چگونه می توان یک برچسب را با نام منبع آن (که مقدار رشته برچسب است) بدست آورد. برای پیدا کردن نام برچسب، فهرست برچسب‌ها را از طریق API دریافت کنید یا از مدیریت برچسب‌های Drive استفاده کنید. برای اطلاعات بیشتر درباره مدیریت برچسب‌ها، به مدیریت برچسب‌های Drive بروید.

advanced/driveLabels.gs
/**
 * Get a label by name.
 * @param {string} labelName The label name.
 */
function getLabel(labelName) {
  try {
    const label = DriveLabels.Labels.get(labelName, {view: 'LABEL_VIEW_FULL'});
    const title = label.properties.title;
    const fieldsLength = label.fields.length;
    console.log(`Fetched label with title: '${title}' and ${fieldsLength} fields.`);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to get label with error %s', err.message);
  }
}

برچسب‌ها را برای یک مورد Drive فهرست کنید

نمونه کد زیر نحوه دریافت یک مورد Drive را نشان می‌دهد و همه برچسب‌های اعمال شده روی آن مورد را فهرست می‌کند.

advanced/driveLabels.gs
/**
 * List Labels on a Drive Item
 * Fetches a Drive Item and prints all applied values along with their to their
 * human-readable names.
 *
 * @param {string} fileId The Drive File ID
 */
function listLabelsOnDriveItem(fileId) {
  try {
    const appliedLabels = Drive.Files.listLabels(fileId);

    console.log('%d label(s) are applied to this file', appliedLabels.items.length);

    appliedLabels.items.forEach((appliedLabel) => {
      // Resource name of the label at the applied revision.
      const labelName = 'labels/' + appliedLabel.id + '@' + appliedLabel.revisionId;

      console.log('Fetching Label: %s', labelName);
      const label = DriveLabels.Labels.get(labelName, {view: 'LABEL_VIEW_FULL'});

      console.log('Label Title: %s', label.properties.title);

      Object.keys(appliedLabel.fields).forEach((fieldId) => {
        const fieldValue = appliedLabel.fields[fieldId];
        const field = label.fields.find((f) => f.id == fieldId);

        console.log(`Field ID: ${field.id}, Display Name: ${field.properties.displayName}`);
        switch (fieldValue.valueType) {
          case 'text':
            console.log('Text: %s', fieldValue.text[0]);
            break;
          case 'integer':
            console.log('Integer: %d', fieldValue.integer[0]);
            break;
          case 'dateString':
            console.log('Date: %s', fieldValue.dateString[0]);
            break;
          case 'user':
            const user = fieldValue.user.map((user) => {
              return `${user.emailAddress}: ${user.displayName}`;
            }).join(', ');
            console.log(`User: ${user}`);
            break;
          case 'selection':
            const choices = fieldValue.selection.map((choiceId) => {
              return field.selectionOptions.choices.find((choice) => choice.id === choiceId);
            });
            const selection = choices.map((choice) => {
              return `${choice.id}: ${choice.properties.displayName}`;
            }).join(', ');
            console.log(`Selection: ${selection}`);
            break;
          default:
            console.log('Unknown: %s', fieldValue.valueType);
            console.log(fieldValue.value);
        }
      });
    });
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', err.message);
  }
}