การเลือกรายการภายในงานนำเสนอ

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

การเลือกคือสแนปชอตของสิ่งที่เกิดขึ้นเมื่อสคริปต์เริ่มทำงาน หากผู้ใช้คลิกและมีการเปลี่ยนแปลงการเลือกขณะที่สคริปต์ทำงาน การเปลี่ยนแปลงเหล่านั้นจะไม่แสดง

การเลือกและประเภทการเลือก

คุณอ่านข้อความที่เลือกได้โดยใช้คลาส Selection คลาสมีเมธอดต่างๆ เพื่อรับออบเจ็กต์ที่เลือกตาม ประเภทของออบเจ็กต์ที่เลือก

การแจงนับ SelectionType แสดงถึงประเภทที่เฉพาะเจาะจงของออบเจ็กต์ที่เลือก เช่น หากผู้ใช้เลือกข้อความในรูปร่าง ประเภทการเลือกจะเป็น TEXT ในกรณีนี้ คุณสามารถดึงข้อความที่เลือกได้โดยใช้เมธอด selection.getTextRange()

นอกจากนี้ คุณยังดึงข้อมูลออบเจ็กต์ที่มีข้อความที่เลือกได้ด้วย โดยหากใช้ตัวอย่างข้างต้น คุณจะดึงข้อมูลรูปร่างที่มีข้อความที่เลือกได้โดยใช้ selection.getPageElementRange().getPageElements()[0] ในทำนองเดียวกัน หน้าที่มีรูปร่างล้อมรอบคือหน้าปัจจุบันที่ใช้งานอยู่ หากต้องการดึงข้อมูลหน้าดังกล่าว ให้ใช้ selection.getCurrentPage()

การอ่านข้อความที่เลือก

หากต้องการอ่านข้อความที่เลือก ให้ใช้เมธอด Presentation.getSelection() ดังที่แสดงในตัวอย่างต่อไปนี้

slides/selection/selection.gs
const selection = SlidesApp.getActivePresentation().getSelection();

การอ่านหน้าปัจจุบัน

หากต้องการดึงข้อมูลหน้าปัจจุบันที่ผู้ใช้กำลังดู ให้ใช้วิธีการ getSelection() และ getCurrentPage() ดังนี้

slides/selection/selection.gs
const currentPage = SlidesApp.getActivePresentation()
  .getSelection()
  .getCurrentPage();

โปรดทราบว่าหน้าปัจจุบันอาจเป็นประเภทใดประเภทหนึ่งต่อไปนี้

หน้าปัจจุบันสามารถเลือกออบเจ็กต์ได้ตั้งแต่ 1 รายการขึ้นไป และ SelectionType จะกำหนดประเภทของการเลือก

การอ่านข้อความที่เลือกตามประเภทการเลือก

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

slides/selection/selection.gs
const selection = SlidesApp.getActivePresentation().getSelection();
const selectionType = selection.getSelectionType();
let currentPage;
switch (selectionType) {
  case SlidesApp.SelectionType.NONE:
    console.log("Nothing selected");
    break;
  case SlidesApp.SelectionType.CURRENT_PAGE:
    currentPage = selection.getCurrentPage();
    console.log(`Selection is a page with ID: ${currentPage.getObjectId()}`);
    break;
  case SlidesApp.SelectionType.PAGE_ELEMENT: {
    const pageElements = selection.getPageElementRange().getPageElements();
    console.log(`There are ${pageElements.length} page elements selected.`);
    break;
  }
  case SlidesApp.SelectionType.TEXT: {
    const tableCellRange = selection.getTableCellRange();
    if (tableCellRange !== null) {
      const tableCell = tableCellRange.getTableCells()[0];
      console.log(
        `Selected text is in a table at row ${tableCell.getRowIndex()}, column ${tableCell.getColumnIndex()}`,
      );
    }
    const textRange = selection.getTextRange();
    if (textRange.getStartIndex() === textRange.getEndIndex()) {
      console.log(`Text cursor position: ${textRange.getStartIndex()}`);
    } else {
      console.log(
        `Selection is a text range from: ${textRange.getStartIndex()} to: ${textRange.getEndIndex()} is selected`,
      );
    }
    break;
  }
  case SlidesApp.SelectionType.TABLE_CELL: {
    const tableCells = selection.getTableCellRange().getTableCells();
    const table = tableCells[0].getParentTable();
    console.log(`There are ${tableCells.length} table cells selected.`);
    break;
  }
  case SlidesApp.SelectionType.PAGE: {
    const pages = selection.getPageRange().getPages();
    console.log(`There are ${pages.length} pages selected.`);
    break;
  }
  default:
    break;
}

การอ่านข้อความที่เลือก

คุณอ่านข้อความที่เลือกได้โดยใช้เมธอด Selection.getTextRange() การเลือกข้อความมี 2 ประเภท ได้แก่

  • การเลือกช่วง: หากรูปร่างมีข้อความ "Hello" และมีการเลือก "He" ช่วงที่แสดงผลจะมี startIndex=0 และ endIndex=2
  • การเลือกด้วยเคอร์เซอร์: หากรูปร่างมีข้อความ "Hello" และเคอร์เซอร์อยู่ หลัง "H" ("H|ello") ช่วงที่แสดงผลจะเป็นช่วงว่างที่มี startIndex=1 และ endIndex=1

การแก้ไขการเลือก

สคริปต์สามารถแก้ไขตัวเลือกของผู้ใช้ได้ การเปลี่ยนแปลงการเลือกใดๆ ที่สคริปต์ทำกับงานนำเสนอจะแสดง ในการดำเนินการเลือกที่ตามมาตลอดระยะเวลาการดำเนินการของสคริปต์

การเปลี่ยนแปลงการเลือกจะแสดงในเบราว์เซอร์ของผู้ใช้หลังจากที่สคริปต์ ทํางานเสร็จสมบูรณ์ หรือเมื่อมีการเรียกใช้ Presentation.saveAndClose() เท่านั้น

การเลือกหน้าปัจจุบัน

คุณเลือกหน้าในงานนำเสนอที่ใช้งานอยู่เป็นหน้าปัจจุบันได้โดยเรียกใช้เมธอด selectAsCurrentPage() วิธีนี้จะนำองค์ประกอบหน้าเว็บ หน้าเว็บ หรือข้อความที่เลือกก่อนหน้าออก ดังนั้นการใช้วิธีนี้ในหน้าปัจจุบันจะช่วยให้คุณยกเลิกการเลือกใดๆ ในหน้าได้ เช่น

slides/selection/selection.gs
// Select the first slide as the current page selection and remove any previous selection.
const selection = SlidesApp.getActivePresentation().getSelection();
const slide = SlidesApp.getActivePresentation().getSlides()[0];
slide.selectAsCurrentPage();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.CURRENT_PAGE
// selection.getCurrentPage() = slide
//

การเลือกองค์ประกอบของหน้า

หากต้องการเลือกองค์ประกอบหน้าเว็บในหน้า ให้ใช้วิธี PageElement.select() การดำเนินการนี้จะยกเลิกการเลือกองค์ประกอบหน้าเว็บที่เลือกไว้ก่อนหน้านี้ด้วย

เช่น

slides/selection/selection.gs
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const pageElement = slide.getPageElements()[0];
// Only select this page element and remove any previous selection.
pageElement.select();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.PAGE_ELEMENT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = pageElement
//

การเลือกองค์ประกอบหลายหน้า

หากต้องการเพิ่มองค์ประกอบหน้าเว็บอื่นๆ ลงในการเลือก ให้ใช้วิธี PageElement.select(false) องค์ประกอบทั้งหมดของหน้าเว็บต้องอยู่ในหน้าปัจจุบัน

slides/selection/selection.gs
const slide = SlidesApp.getActivePresentation().getSlides()[0];
// First select the slide page, as the current page selection.
slide.selectAsCurrentPage();
// Then select all the page elements in the selected slide page.
const pageElements = slide.getPageElements();
for (let i = 0; i < pageElements.length; i++) {
  pageElements[i].select(false);
}
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.PAGE_ELEMENT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements() = pageElements
//

การเปลี่ยนรูปการเลือก

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

  1. สมมติว่าคุณเลือกรูปร่าง 2 รูปคือ A และ B
  2. จากนั้นสคริปต์จะนำรูปร่าง A ออก
  3. ด้วยเหตุนี้ ระบบจึงเปลี่ยนการเลือกให้สอดคล้องกับการแก้ไขเพื่อให้เลือกได้เฉพาะ รูปร่าง B

ตัวอย่างต่อไปนี้แสดงวิธีเปลี่ยนรูปแบบการเลือกโดยการแก้ไของค์ประกอบของหน้าที่เลือก

slides/selection/selection.gs
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const shape1 = slide.getPageElements()[0].asShape();
const shape2 = slide.getPageElements()[1].asShape();
// Select both the shapes.
shape1.select();
shape2.select(false);
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.PAGE_ELEMENT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements() = [shape1, shape2]
//
// Remove one shape.
shape2.remove();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.PAGE_ELEMENT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements() = [shape1]
//

การเลือกข้อความ

คุณเลือกข้อความที่อยู่ในรูปร่างหรือในเซลล์ตารางได้โดยใช้เมธอด TextRange.select() หากข้อความอยู่ในรูปร่าง ระบบจะเลือกรูปร่างนั้นด้วย หากข้อความอยู่ในเซลล์ตาราง ระบบจะเลือกทั้งเซลล์ตารางและตารางที่ครอบเซลล์นั้น

การดำเนินการนี้จะตั้งค่าหน้าหลักเป็นหน้าปัจจุบันด้วย

การเลือกช่วงในรูปร่าง

ตัวอย่างต่อไปนี้แสดงวิธีเลือกช่วงภายในข้อความที่อยู่ในรูปร่าง

slides/selection/selection.gs
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const shape = slide.getPageElements()[0].asShape();
shape.getText().setText("Hello");
// Range selection: Select the text range 'He'.
shape.getText().getRange(0, 2).select();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.TEXT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = shape
// selection.getTextRange().getStartIndex() = 0
// selection.getTextRange().getEndIndex() = 2
//

การเลือกเคอร์เซอร์ในรูปร่าง

ตัวอย่างต่อไปนี้แสดงวิธีเลือกเคอร์เซอร์ภายในข้อความที่อยู่ในรูปร่าง

slides/selection/selection.gs
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const shape = slide.getPageElements()[0].asShape();
shape.getText().setText("Hello");
// Cursor selection: Place the cursor after 'H' like 'H|ello'.
shape.getText().getRange(1, 1).select();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.TEXT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = shape
// selection.getTextRange().getStartIndex() = 1
// selection.getTextRange().getEndIndex() = 1
//

การเลือกช่วงในเซลล์ของตาราง

ตัวอย่างต่อไปนี้แสดงวิธีเลือกช่วงภายในข้อความที่อยู่ในเซลล์ตาราง

slides/selection/selection.gs
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const table = slide.getPageElements()[0].asTable();
const tableCell = table.getCell(0, 1);
tableCell.getText().setText("Hello");
// Range selection: Select the text range 'He'.
tableCell.getText().getRange(0, 2).select();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.TEXT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = table
// selection.getTableCellRange().getTableCells()[0] = tableCell
// selection.getTextRange().getStartIndex() = 0
// selection.getTextRange().getEndIndex() = 2
//

การเลือกเคอร์เซอร์ใน TableCell

ตัวอย่างต่อไปนี้แสดงวิธีเลือกเคอร์เซอร์ภายในข้อความที่มีอยู่ในเซลล์ตาราง

slides/selection/selection.gs
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const table = slide.getPageElements()[0].asTable();
const tableCell = table.getCell(0, 1);
tableCell.getText().setText("Hello");
// Cursor selection: Place the cursor after 'H' like 'H|ello'.
tableCell.getText().getRange(1, 1).select();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.TEXT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = table
// selection.getTableCellRange().getTableCells()[0] = tableCell
// selection.getTextRange().getStartIndex() = 1
// selection.getTextRange().getEndIndex() = 1
//

การเปลี่ยนรูปแบบการเลือกด้วยการแก้ไขข้อความ

ตัวอย่างต่อไปนี้แสดงวิธีเปลี่ยนรูปแบบข้อความที่เลือกโดยการแก้ไข ข้อความที่เลือก

slides/selection/selection.gs
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const shape = slide.getPageElements()[0].asShape();
const textRange = shape.getText();
textRange.setText("World");
// Select all the text 'World'.
textRange.select();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.TEXT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = shape
// selection.getTextRange().getStartIndex() = 0
// selection.getTextRange().getEndIndex() = 6
//
// Add some text to the shape, and the selection will be transformed.
textRange.insertText(0, "Hello ");

// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.TEXT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = shape
// selection.getTextRange().getStartIndex() = 0
// selection.getTextRange().getEndIndex() = 12
//

ยกเลิกการเลือก

ไม่มีวิธีที่ชัดเจนในการยกเลิกการเลือกข้อความหรือองค์ประกอบของหน้า อย่างไรก็ตาม คุณสามารถทำได้โดยใช้วิธี Page.selectAsCurrentPage() หรือ pageElement.select()

เลือกหน้าปัจจุบัน

ตัวอย่างต่อไปนี้แสดงวิธียกเลิกการเลือกปัจจุบันในหน้าเว็บ โดยการตั้งค่าหน้าเว็บนั้นเป็นหน้าปัจจุบัน

slides/selection/selection.gs
// Unselect one or more page elements already selected.
//
// In case one or more page elements in the first slide are selected, setting the
// same (or any other) slide page as the current page would do the unselect.
//
const slide = SlidesApp.getActivePresentation().getSlides()[0];
slide.selectAsCurrentPage();

เลือกองค์ประกอบหน้าเว็บ

ตัวอย่างต่อไปนี้แสดงวิธียกเลิกการเลือกปัจจุบันในหน้า โดยการเลือกองค์ประกอบของหน้า 1 รายการ ซึ่งจะนำรายการอื่นๆ ทั้งหมดออกจากส่วนที่เลือก

slides/selection/selection.gs
// Unselect one or more page elements already selected.
//
// In case one or more page elements in the first slide are selected,
// selecting any pageElement in the first slide (or any other pageElement) would
// do the unselect and select that pageElement.
//
const slide = SlidesApp.getActivePresentation().getSlides()[0];
slide.getPageElements()[0].select();