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

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

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

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

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

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

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

อ่านรายการที่เลือก

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

สไลด์/selection/selection.gs
const selection = SlidesApp.getActivePresentation().getSelection();

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

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

สไลด์/selection/selection.gs
const currentPage = SlidesApp.getActivePresentation().getSelection().getCurrentPage();

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

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

อ่านรายการที่เลือกตามประเภทการเลือก

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

สไลด์/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 ประเภท ได้แก่

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

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

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

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

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

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

สไลด์/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() การดำเนินการนี้จะยกเลิกการเลือกองค์ประกอบของหน้าที่เลือกไว้ก่อนหน้านี้ด้วย

เช่น

สไลด์/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) องค์ประกอบของหน้าทั้งหมดต้องอยู่ในหน้าปัจจุบัน

สไลด์/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. สมมติว่าคุณได้เลือกรูปทรง A และ B สองรูป
  2. จากนั้น สคริปต์ของคุณจะนำรูปร่าง A ออก
  3. ดังนั้นระบบจะเปลี่ยนการเลือกตามการแก้ไขเพื่อให้เลือกเฉพาะรูปร่าง B เท่านั้น

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

สไลด์/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() หากข้อความอยู่ในรูปร่าง จะมีการเลือกรูปร่างนั้นด้วย หากข้อความอยู่ในเซลล์ตาราง จะมีการเลือกทั้งเซลล์ในตารางและตารางที่ล้อมรอบ

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

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

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

สไลด์/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
//

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

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

สไลด์/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
//

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

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

สไลด์/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

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

สไลด์/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
//

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

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

สไลด์/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()

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

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

สไลด์/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 รายการ ซึ่งจะนำรายการอื่นๆ ทั้งหมดออกจากการเลือก

สไลด์/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();