פעולות עריכה

אפשר להשתמש באובייקטים של פעולה כדי להוסיף התנהגות אינטראקטיבית לתוספים של Google Workspace.

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

הוספת פעולה לווידג'ט

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

דוגמה: הצגת כרטיס חדש בלחיצה על לחצן

אם רוצים להוסיף לתוסף לחצן שיוצר ומציג כרטיס חדש בלחיצה, פועלים לפי השלבים הבאים:

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

בדוגמה הבאה מוצגת יצירה של לחצן ווידג'ט. הבקשה לפעולה היא עבור היקף ההרשאות drive.file של הקובץ הנוכחי בשם התוסף.

/**
 * Adds a section to the Card Builder that displays a "REQUEST PERMISSION" button.
 * When it's clicked, the callback triggers file scope permission flow. This is used in
 * the add-on when the home-page displays basic data.
 */
function addRequestFileScopeButtonToBuilder(cardBuilder) {
    var buttonSection = CardService.newCardSection();
    // 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.
    var buttonAction = CardService.newAction()
      .setFunctionName("onRequestFileScopeButtonClickedInEditor");

    var button = CardService.newTextButton()
      .setText("Request permission")
      .setBackgroundColor("#4285f4")
      .setTextButtonStyle(CardService.TextButtonStyle.FILLED)
      .setOnClickAction(buttonAction);

    buttonSection.addWidget(button);
    cardBuilder.addSection(buttonSection);
}

/**
 * Callback function for a button action. Instructs Docs to display a
 * permissions dialog to the user, requesting `drive.file` scope for the 
 * current file on behalf of this add-on.
 *
 * @param {Object} e The parameters object that contains the documents ID
 * @return {editorFileScopeActionResponse}
 */
function onRequestFileScopeButtonClickedInEditor(e) {
  return CardService.newEditorFileScopeActionResponseBuilder()
      .requestFileScopeForActiveDocument().build();

אינטראקציות של גישה לקבצים בממשקי API ל-REST

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

הפעולה שניסית לבצע פונקציית הקריאה החוזרת צריכה להחזיר
בקשת גישה לקובץ current_document EditorFileScopeActionResponse

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

  • התוסף משתמש בממשקי REST API.
  • התוסף מציג את תיבת הדו-שיח של היקף קובץ הבקשה באמצעות השיטה CardService.newEditorFileScopeActionResponseBuilder().requestFileScopeForActiveDocument().build();.
  • התוסף כולל את היקף העריכה https://www.googleapis.com/auth/drive.file ואת הטריגר onFileScopeGranted במניפסט שלו.

בקשת גישה לקובץ עבור המסמך הנוכחי

כדי לבקש גישה לקובץ של המסמך הנוכחי:

  1. יוצרים כרטיס בדף הבית שבודק אם לתוסף יש היקף drive.file.
  2. במקרים שבהם לא ניתנה לתוסף הרשאת drive.file, צריך ליצור דרך לבקש מהמשתמשים לתת הרשאת drive.file למסמך הנוכחי.

דוגמה: קבלת גישה למסמך הנוכחי ב-Google Docs

בדוגמה הבאה מוצג ממשק ל-Google Docs שמציג את הגודל של המסמך הנוכחי. אם לתוסף אין drive.fileהרשאה, מוצג לחצן להפעלת ההרשאה לגישה לקובץ.

/**
 * 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 information about the
 *   current document.
 * @return {Card}
 */
function onDocsHomepage(e) {
  return createAddOnView(e);
}

function onFileScopeGranted(e) {
  return createAddOnView(e);
}

/**
 * For the current document, display either its quota information or
 * a button that allows the user to provide permission to access that
 * file to retrieve its quota details.
 *
 * @param e The event containing information about the current document
 * @return {Card}
 */
function createAddOnView(e) {
  var docsEventObject = e['docs'];
  var builder =  CardService.newCardBuilder();

  var cardSection = CardService.newCardSection();
  if (docsEventObject['addonHasFileScopePermission']) {
    cardSection.setHeader(docsEventObject['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 the add-on has access permission, read and display its quota.
    cardSection.addWidget(
      CardService.newTextParagraph().setText(
          "This file takes up: " + getQuotaBytesUsed(docsEventObject['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");

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

    cardSection.addWidget(button);
  }
  return builder.addSection(cardSection).build();
}

/**
 * Callback function for a button action. Instructs Docs to display a
 * permissions dialog to the user, requesting `drive.file` scope for the
 * current file on behalf of this add-on.
 *
 * @param {Object} e The parameters object that contains the documents ID
 * @return {editorFileScopeActionResponse}
 */
function onRequestFileScopeButtonClicked(e) {
  return CardService.newEditorFileScopeActionResponseBuilder()
      .requestFileScopeForActiveDocument().build();
}