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

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

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

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

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

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

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

  1. สร้างวิดเจ็ตปุ่ม
  2. หากต้องการตั้งค่าการดำเนินการสร้างการ์ด ให้เพิ่มฟังก์ชันตัวแฮนเดิลของวิดเจ็ตปุ่ม setOnClickAction
  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
    // lets the user 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 สามารถรวมการดำเนินการของวิดเจ็ตเพิ่มเติมเพื่อขอสิทธิ์เข้าถึงไฟล์ได้ การดำเนินการนี้กำหนดให้ฟังก์ชัน Callback ของการดำเนินการที่เชื่อมโยงแสดงผลออบเจ็กต์การตอบกลับเฉพาะ

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

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

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

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

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

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

ตัวอย่าง: รับสิทธิ์เข้าถึงเอกสารปัจจุบันใน Docs

ตัวอย่างต่อไปนี้สร้างอินเทอร์เฟซสำหรับ Docs ที่แสดงขนาดของเอกสารปัจจุบัน หากส่วนเสริมไม่มีการให้สิทธิ์ drive.file ส่วนเสริมจะแสดงปุ่มเพื่อเริ่มการให้สิทธิ์ขอบเขตไฟล์

/**
 * Build a 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 lets the user 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
    // lets the user 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();
}