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

ใช้ออบเจ็กต์ Action เพื่อสร้างอินเทอร์แอกทีฟ การใช้งานส่วนเสริมของ Google Workspace

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

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

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

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

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

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

ตัวอย่างต่อไปนี้แสดงการสร้างวิดเจ็ตปุ่ม คำขอการดำเนินการ ขอบเขต 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 อาจมี การดำเนินการเพิ่มเติมของวิดเจ็ตเพื่อขอสิทธิ์เข้าถึงไฟล์ การดำเนินการนี้ต้องใช้เมธอด ฟังก์ชัน Callback ของการดำเนินการที่เชื่อมโยงเพื่อแสดงออบเจ็กต์การตอบกลับแบบพิเศษ

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

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

  • ส่วนเสริมใช้ REST API
  • ส่วนเสริมแสดงกล่องโต้ตอบขอบเขตไฟล์คำขอ โดยใช้เมธอด 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();
}