Drive के लेबल से जुड़ी बेहतर सेवा

Google Apps Script में, Drive के लेबल की बेहतर सेवा का इस्तेमाल करना.

Google Drive के लेबल की बेहतर सेवा की मदद से, Drive में मौजूद फ़ाइलों और फ़ोल्डर के लिए लेबल बनाएं और उन्हें मैनेज करें. इस बेहतर सेवा की मदद से, Google Apps Script में Drive के लेबल के एपीआई की सभी सुविधाओं का इस्तेमाल किया जा सकता है.

Drive के लेबल लागू करने या हटाने के लिए, Drive की बेहतर सेवा का इस्तेमाल करें.

यह एक बेहतर सेवा है. इसका इस्तेमाल करने से पहले, इसे चालू करना ज़रूरी है.

संदर्भ

इस सेवा के बारे में ज़्यादा जानकारी पाने के लिए, Google Drive के लेबल के एपीआई से जुड़ा दस्तावेज़ देखें. Apps Script में मौजूद सभी बेहतर सेवाओं की तरह, Drive के लेबल की एपीआई सेवा में भी सार्वजनिक एपीआई वाले ऑब्जेक्ट, तरीके, और पैरामीटर इस्तेमाल किए जाते हैं.

समस्याओं की शिकायत करने और अन्य सहायता पाने के लिए, Google Drive के लेबल के एपीआई के लिए सहायता गाइड देखें.

नमूना कोड

नीचे दिए गए सैंपल कोड में, एपीआई के वर्शन 2 का इस्तेमाल किया गया है.

लेबल की सूची देखना

नीचे दिए गए कोड सैंपल में, अनुरोध करने वाले उपयोगकर्ता के लिए उपलब्ध लेबल की सूची पाने का तरीका बताया गया है.

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

कोई लेबल पाना

नीचे दिए गए कोड सैंपल में, संसाधन के नाम (जो लेबल की स्ट्रिंग वैल्यू होती है) की मदद से, कोई एक लेबल पाने का तरीका बताया गया है. लेबल का नाम जानने के लिए, एपीआई की मदद से लेबल की सूची पाएं या 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.labels.length,
    );

    for (const appliedLabel of appliedLabels.labels) {
      // 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);

      for (const fieldId of Object.keys(appliedLabel.fields)) {
        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);
  }
}