การดําเนินการของเอดิเตอร์

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

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

เพิ่มการดำเนินการลงในวิดเจ็ต

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

ตัวอย่าง: แสดงบัตรใหม่เมื่อมีการคลิกปุ่ม

หากต้องการเพิ่มปุ่มที่สร้างและแสดงการ์ดใหม่เมื่อคลิกไปยังส่วนเสริม ให้ทำตามขั้นตอนด้านล่าง

  1. สร้างวิดเจ็ตปุ่ม
  2. หากต้องการตั้งค่าการดำเนินการสร้างการ์ด ให้เพิ่มฟังก์ชันเครื่องจัดการวิดเจ็ตปุ่ม 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 document’s ID
 * @return {editorFileScopeActionResponse}
 */
function onRequestFileScopeButtonClickedInEditor(e) {
  return CardService.newEditorFileScopeActionResponseBuilder()
      .requestFileScopeForActiveDocument().build();

การโต้ตอบการเข้าถึงไฟล์สำหรับ REST API

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

พยายามดำเนินการแล้ว ฟังก์ชันเรียกกลับควรจะแสดงผล
ส่งคำขอเข้าถึงไฟล์สำหรับ current_document EditorFileScopeActionResponse

หากต้องการใช้ประโยชน์จากการดำเนินการของวิดเจ็ตและออบเจ็กต์การตอบกลับนี้ เงื่อนไขทั้งหมดต่อไปนี้ต้องเป็นจริง

  • ส่วนเสริมใช้ API ของ REST
  • ส่วนเสริมจะแสดงกล่องโต้ตอบขอบเขตไฟล์คำขอโดยใช้เมธอด CardService.newEditorFileScopeActionResponseBuilder().requestFileScopeForActiveDocument().build();
  • ส่วนเสริมรวมขอบเขตผู้แก้ไข https://www.googleapis.com/auth/drive.file และทริกเกอร์ onFileScopeGranted ไว้ในไฟล์ Manifest

ขอสิทธิ์เข้าถึงไฟล์สำหรับเอกสารปัจจุบัน

หากต้องการขอสิทธิ์เข้าถึงไฟล์สำหรับเอกสารปัจจุบัน ให้ทำตามขั้นตอนต่อไปนี้

  1. สร้างการ์ดหน้าแรกที่จะตรวจสอบว่าส่วนเสริมมีขอบเขต drive.file หรือไม่
  2. สำหรับกรณีที่ส่วนเสริมยังไม่ได้รับขอบเขต drive.file ให้สร้างวิธีขอให้ผู้ใช้มอบขอบเขต drive.file สำหรับเอกสารปัจจุบัน

ตัวอย่าง: เข้าถึงเอกสารปัจจุบันใน Google เอกสาร

ตัวอย่างต่อไปนี้สร้างอินเทอร์เฟซสำหรับ Google เอกสารที่แสดงขนาดของเอกสารปัจจุบัน หากส่วนเสริมไม่ได้รับการให้สิทธิ์ 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 document’s ID
 * @return {editorFileScopeActionResponse}
 */
function onRequestFileScopeButtonClicked(e) {
  return CardService.newEditorFileScopeActionResponseBuilder()
      .requestFileScopeForActiveDocument().build();
}