קידום פעולות

אובייקטים של Action מאפשרים לכם ליצור התנהגות אינטראקטיבית בתוספים של Google Workspace. הם מגדירים מה קורה כשמשתמש יוצר אינטראקציה עם ווידג'ט (לדוגמה, לחצן) בממשק המשתמש של התוסף.

פעולה מצורפת לווידג'ט נתון באמצעות פונקציה של handler של ווידג'טים, שמגדירה גם את התנאי שמפעיל את הפעולה. כשהפעולה מופעלת, היא מבצעת פונקציית קריאה חוזרת ייעודית. לפונקציית הקריאה החוזרת מועבר אובייקט אירוע שנושא את המידע על האינטראקציות של המשתמש בצד הלקוח. צריך להטמיע את פונקציית הקריאה החוזרת ולהחזיר אובייקט ספציפי לתגובה.

לדוגמה, נניח שאתם רוצים ליצור לחצן שייבנה ומציג כרטיס חדש כשלוחצים עליו. לשם כך, צריך ליצור ווידג'ט חדש של לחצן ולהשתמש בפונקציה setOnClickAction(action) של handler של לחצנים כדי להגדיר Action בניית כרטיסים. ב-Action שהגדרתם מצוינת פונקציית קריאה חוזרת ל-Apps Script, שפועלת אחרי שלוחצים על הלחצן. במקרה כזה, צריך להטמיע את פונקציית הקריאה החוזרת כדי לבנות את הכרטיס הרצוי ולהחזיר אובייקט ActionResponse. אובייקט התשובה מורה לתוסף להציג את הכרטיס שיצרה פונקציית הקריאה החוזרת.

בדף הזה מתוארות פעולות בווידג'ט ספציפיות ל-Drive שאפשר לכלול בתוסף.

אינטראקציות ב-Drive

תוספים של Google Workspace שמרחיבים את Drive יכולים לכלול פעולת ווידג'ט נוספת שספציפית ל-Drive. לפעולה הזו נדרשת פונקציית קריאה חוזרת לפעולה המשויכת כדי להחזיר אובייקט תגובה מיוחד:

בוצע ניסיון לפעולה פונקציית הקריאה החוזרת אמורה לחזור
בקשת גישה לקבצים של הקבצים שנבחרו DriveItemsSelectedActionResponse

כדי להשתמש בפעולות הווידג'ט ובאובייקטים של התגובה האלה, כל התנאים הבאים צריכים להתקיים:

  • הפעולה הזו מופעלת כל עוד למשתמש נבחר לפחות פריט אחד ב-Drive.
  • במניפסט של התוסף מופיע https://www.googleapis.com/auth/drive.file ההיקף של Drive.

בקשת גישה לקבצים בקבצים שנבחרו

הדוגמה הבאה מראה איך לבנות ממשק לפי הקשר ל-Google Drive שמופעל כשהמשתמש בוחר פריט אחד או יותר ב-Drive. בדוגמה הזו אנחנו בודקים כל פריט כדי לראות אם התוסף קיבל הרשאת גישה. אחרת, הוא משתמש באובייקט DriveItemsSelectedActionResponse כדי לבקש את ההרשאה הזו מהמשתמש. כשמקבלים הרשאה לפריט, בתוסף מוצג שימוש במכסה ב-Drive לאותו פריט.

/**
 * Build a simple card that checks selected items' quota usage. Checking
 * quota usage requires user-permissions, so this add-on provides a button
 * to request `drive.file` scope for items the add-on doesn't yet have
 * permission to access.
 *
 * @param e The event object passed containing contextual information about
 *    the Drive items selected.
 * @return {Card}
 */
function onDriveItemsSelected(e) {
  var builder =  CardService.newCardBuilder();

  // For each item the user has selected in Drive, display either its
  // quota information or a button that allows the user to provide
  // permission to access that file to retrieve its quota details.
  e['drive']['selectedItems'].forEach(
    function(item){
      var cardSection = CardService.newCardSection()
          .setHeader(item['title']);

      // This add-on uses the recommended, limited-permission `drive.file`
      // scope to get granular per-file access permissions.
      // See: https://developers.google.com/drive/api/v2/about-auth
      if (item['addonHasFileScopePermission']) {
        // If the add-on has access permission, read and display its
        // quota.
        cardSection.addWidget(
          CardService.newTextParagraph().setText(
              "This file takes up: " + getQuotaBytesUsed(item['id'])));
      } else {
        // If the add-on does not have access permission, add a button
        // that allows the user to provide that permission on a per-file
        // basis.
        cardSection.addWidget(
          CardService.newTextParagraph().setText(
              "The add-on needs permission to access this file's quota."));

        var buttonAction = CardService.newAction()
          .setFunctionName("onRequestFileScopeButtonClicked")
          .setParameters({id: item.id});

        var button = CardService.newTextButton()
          .setText("Request permission")
          .setOnClickAction(buttonAction);

        cardSection.addWidget(button);
      }

      builder.addSection(cardSection);
    });

  return builder.build();
}

/**
 * Callback function for a button action. Instructs Drive to display a
 * permissions dialog to the user, requesting `drive.file` scope for a
 * specific item on behalf of this add-on.
 *
 * @param {Object} e The parameters object that contains the item's
 *   Drive ID.
 * @return {DriveItemsSelectedActionResponse}
 */
function onRequestFileScopeButtonClicked (e) {
  var idToRequest = e.parameters.id;
  return CardService.newDriveItemsSelectedActionResponseBuilder()
      .requestFileScope(idToRequest).build();
}

/**
 * Use the Advanced Drive Service
 * (See https://developers.google.com/apps-script/advanced/drive),
 * with `drive.file` scope permissions to request the quota usage of a
 * specific Drive item.
 *
 * @param {string} itemId The ID of the item to check.
 * @return {string} A description of the item's quota usage, in bytes.
 */
function getQuotaBytesUsed(itemId) {
  try {
    return Drive.Files.get(itemId,{fields: "quotaBytesUsed"})
        .quotaBytesUsed + " bytes";
  } catch (e) {
    return "Error fetching how much quota this item uses. Error: " + e;
  }
}