การดำเนินการกับไดรฟ์

ออบเจ็กต์ Action ช่วยให้คุณสร้างแบบอินเทอร์แอกทีฟ การใช้งานส่วนเสริมของ Google Workspace คำจำกัดความ สิ่งที่จะเกิดขึ้นเมื่อผู้ใช้โต้ตอบกับวิดเจ็ต (เช่น ปุ่ม) ใน UI ของส่วนเสริม

มีการดำเนินการแนบอยู่กับวิดเจ็ตที่กำหนด โดยใช้ ฟังก์ชันเครื่องจัดการวิดเจ็ต ซึ่งกำหนดเงื่อนไขที่ทริกเกอร์การดำเนินการด้วย เมื่อทริกเกอร์ จะดำเนินการกำหนด ฟังก์ชัน Callback ฟังก์ชัน Callback จะส่งผ่าน ออบเจ็กต์เหตุการณ์ที่มี ข้อมูลเกี่ยวกับการโต้ตอบฝั่งไคลเอ็นต์ของผู้ใช้ คุณต้องใช้ Callback และทำให้แสดงผลออบเจ็กต์การตอบกลับที่เฉพาะเจาะจง

ตัวอย่างเช่น สมมติว่าคุณต้องการปุ่มที่สร้างและแสดงการ์ดใหม่เมื่อ คลิกแล้ว สำหรับกรณีนี้ คุณต้องสร้างวิดเจ็ตปุ่มใหม่และใช้วิดเจ็ตปุ่ม ฟังก์ชันตัวแฮนเดิล setOnClickAction(action) เพื่อตั้งค่าการสร้างการ์ด Action Action ที่คุณกำหนดจะระบุ Apps Script ฟังก์ชัน Callback ที่จะทำงานเมื่อมีการคลิกปุ่ม ในกรณีนี้ คุณ ใช้ฟังก์ชัน Callback เพื่อสร้างบัตรที่คุณต้องการและแสดงผล ActionResponse ออบเจ็กต์ ออบเจ็กต์การตอบกลับจะแจ้งให้ส่วนเสริมแสดงการ์ดที่ Callback สร้างฟังก์ชัน

หน้านี้จะอธิบายถึงการดำเนินการต่างๆ สำหรับวิดเจ็ตไดรฟ์โดยเฉพาะซึ่งคุณสามารถรวมไว้ในไฟล์ ส่วนเสริม

กระตุ้นการโต้ตอบ

ส่วนเสริมของ Google Workspace ที่ขยายไดรฟ์ได้มีดังนี้ การดำเนินการเพิ่มเติมของวิดเจ็ตเฉพาะไดรฟ์ การดำเนินการนี้ต้องมีการเชื่อมโยง การดำเนินการ ฟังก์ชัน Callback เพื่อแสดงออบเจ็กต์การตอบกลับแบบพิเศษ

พยายามดำเนินการแล้ว ฟังก์ชัน Callback ควรแสดงผล
ขอสิทธิ์เข้าถึงไฟล์สำหรับไฟล์ที่เลือก DriveItemsSelectedActionResponse

หากต้องการใช้การทำงานของวิดเจ็ตและออบเจ็กต์การตอบสนอง คุณจะทำสิ่งต่อไปนี้ได้ ต้องเป็นจริง

  • ระบบจะทริกเกอร์การดำเนินการขณะที่ผู้ใช้เลือกรายการในไดรฟ์ไว้อย่างน้อย 1 รายการ
  • ส่วนเสริมนี้ประกอบด้วย https://www.googleapis.com/auth/drive.file ขอบเขตของไดรฟ์ ไฟล์ Manifest

ขอสิทธิ์เข้าถึงไฟล์สำหรับไฟล์ที่เลือก

ตัวอย่างต่อไปนี้แสดงวิธีการสร้างอินเทอร์เฟซตามบริบทสําหรับ Google ไดรฟ์จะทำงานเมื่อผู้ใช้เลือกรายการในไดรฟ์อย่างน้อย 1 รายการ เช่น ทดสอบแต่ละรายการเพื่อดูว่าส่วนเสริมได้รับสิทธิ์ในการเข้าถึงหรือไม่ หากไม่ ก็จะใช้ DriveItemsSelectedActionResponse เพื่อขอสิทธิ์นั้นจากผู้ใช้ เมื่อให้สิทธิ์แล้ว รายการหนึ่ง ส่วนเสริมจะแสดงการใช้โควต้าไดรฟ์ของรายการนั้น

/**
 * 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;
  }
}