Chọn các mục trong bản trình bày

Lựa chọn là bất kỳ lựa chọn nào hiện được chọn trong một trang trình bày đang mở, chẳng hạn như một khoảng văn bản được làm nổi bật hoặc một bảng. Hướng dẫn này cho bạn biết cách lấy và đặt lựa chọn trong một bản trình bày đang hoạt động bằng Apps Script.

Lựa chọn này cung cấp thông tin tổng quan nhanh về thời điểm tập lệnh bắt đầu. Nếu người dùng nhấp vào và lựa chọn thay đổi trong khi tập lệnh đang chạy, thì những thay đổi đó sẽ không được phản ánh.

Các lựa chọn và kiểu lựa chọn

Bạn có thể đọc lựa chọn bằng cách sử dụng lớp Lựa chọn. Lớp này có nhiều phương thức để lấy các đối tượng đã chọn dựa trên loại đối tượng đã chọn.

Enum SelectionType đại diện cho kiểu đối tượng cụ thể được chọn. Ví dụ: nếu người dùng đã chọn một số văn bản trong một hình dạng, thì loại lựa chọn sẽ là TEXT (VĂN BẢN). Trong trường hợp này, bạn có thể truy xuất dải văn bản đã chọn bằng cách sử dụng phương thức selection.getTextRange().

Bạn cũng có thể truy xuất đối tượng chứa văn bản đã chọn; tiếp tục ví dụ trên, bạn có thể truy xuất hình dạng chứa văn bản đã chọn bằng cách sử dụng selection.getPageElementRange().getPageElements()[0]. Tương tự, trang chứa hình dạng bao quanh là trang đang hoạt động; để truy xuất trang đó, hãy sử dụng selection.getCurrentPage().

Đọc lựa chọn

Để đọc lựa chọn, hãy sử dụng phương thức Presentation.getSelection() như trong ví dụ sau:

trang trình bày/selection/selection.gs
const selection = SlidesApp.getActivePresentation().getSelection();

Đọc trang hiện tại

Để truy xuất Trang hiện tại mà người dùng đang xem, hãy sử dụng các phương thức getSelection()getCurrentPage() như sau:

trang trình bày/selection/selection.gs
const currentPage = SlidesApp.getActivePresentation().getSelection().getCurrentPage();

Xin lưu ý rằng trang hiện tại có thể thuộc một trong những loại sau đây:

Trang hiện tại có thể chọn một hoặc nhiều đối tượng và SelectionType sẽ xác định loại lựa chọn.

Đọc lựa chọn dựa trên loại lựa chọn

Ví dụ sau cho thấy cách bạn có thể sử dụng loại đã chọn để đọc lựa chọn hiện tại theo cách phù hợp với loại.

trang trình bày/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;
}

Đọc lựa chọn văn bản

Bạn có thể đọc phần lựa chọn văn bản bằng cách sử dụng phương thức Selection.getTextRange(). Có hai loại lựa chọn văn bản:

  • Lựa chọn dải ô: Nếu một hình dạng chứa văn bản "Xin chào" và "Ông" được chọn, thì dải ô được trả về sẽ có startIndex=0 và endIndex=2.
  • Lựa chọn con trỏ: Nếu một hình dạng chứa văn bản "Hello" (Xin chào) và con trỏ đứng sau "H" ("H|ello"), thì dải ô được trả về sẽ là dải ô trống với startIndex=1 và endIndex=1.

Sửa đổi lựa chọn

Tập lệnh có thể sửa đổi lựa chọn của người dùng. Mọi thay đổi lựa chọn mà tập lệnh thực hiện đối với bản trình bày đều được phản ánh trong các thao tác lựa chọn tiếp theo trong suốt thời gian thực thi tập lệnh.

Các thay đổi về lựa chọn chỉ được phản ánh trên trình duyệt của người dùng sau khi quá trình thực thi tập lệnh hoàn tất hoặc khi Presentation.saveAndClose() được gọi.

Chọn trang hiện tại

Bạn có thể chọn một trang trong bản trình bày đang hoạt động làm trang hiện tại bằng cách gọi phương thức selectAsCurrentPage(). Phương thức này sẽ xoá mọi phần tử trang, trang hoặc lựa chọn văn bản trước đó. Vì vậy, việc sử dụng phương thức này trên trang hiện tại cho phép bạn bỏ chọn mọi lựa chọn hiện tại trên trang. Ví dụ:

trang trình bày/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
//

Chọn một phần tử trang

Để chọn một phần tử trang trong một trang, hãy sử dụng phương thức PageElement.select(). Thao tác này cũng sẽ bỏ chọn mọi phần tử trang đã chọn trước đó.

Ví dụ:

trang trình bày/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
//

Chọn nhiều phần tử trang

Để thêm các phần tử trang bổ sung vào lựa chọn, hãy sử dụng phương thức PageElement.select(false). Tất cả các phần tử trang phải có trên trang hiện tại.

trang trình bày/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
//

Thay đổi lựa chọn

Các nội dung chỉnh sửa mà tập lệnh của bạn thực hiện có thể biến đổi lựa chọn hiện tại để nội dung được chọn sẽ thay đổi do chỉnh sửa đó. Ví dụ:

  1. Giả sử bạn có hai hình dạng A và B được chọn.
  2. Tiếp theo, tập lệnh của bạn sẽ xoá hình dạng A.
  3. Do đó, lựa chọn này được chuyển đổi dựa trên bản chỉnh sửa để chỉ chọn hình dạng B.

Ví dụ sau cho thấy cách biến đổi lựa chọn bằng cách chỉnh sửa các phần tử trang đã chọn.

trang trình bày/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]
//

Chọn văn bản

Bạn có thể chọn văn bản chứa trong một hình dạng hoặc trong một ô của bảng bằng phương thức TextRange.select(). Nếu văn bản nằm trong một hình dạng, thì hình dạng đó cũng được chọn. Nếu văn bản nằm trong một ô của bảng, thì ô bảng đó và bảng bao quanh đều được chọn.

Thao tác này cũng đặt trang gốc làm trang hiện tại.

Lựa chọn dải ô theo hình dạng

Ví dụ sau cho thấy cách lựa chọn dải ô trong văn bản được chứa trong một hình dạng.

trang trình bày/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
//

Lựa chọn con trỏ theo hình dạng

Ví dụ sau cho thấy cách lựa chọn con trỏ trong văn bản nằm trong một hình dạng.

trang trình bày/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
//

Lựa chọn dải ô trong một ô của bảng

Ví dụ sau cho thấy cách lựa chọn dải ô trong văn bản nằm trong một ô của bảng.

trang trình bày/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
//

Lựa chọn con trỏ trong TableCell

Ví dụ sau cho thấy cách lựa chọn con trỏ trong văn bản nằm trong một ô của bảng.

trang trình bày/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
//

Biến đổi lựa chọn bằng chỉnh sửa văn bản

Ví dụ sau cho thấy cách biến đổi văn bản đã chọn bằng cách chỉnh sửa văn bản đã chọn.

trang trình bày/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
//

Đang bỏ chọn

Không có phương pháp rõ ràng nào để bỏ chọn văn bản hoặc phần tử trang. Tuy nhiên, bạn có thể đạt được kết quả này bằng cách sử dụng phương thức Page.selectAsCurrentPage() hoặc pageElement.select().

Chọn một trang hiện tại

Ví dụ sau cho biết cách bỏ chọn bất kỳ lựa chọn hiện tại nào trên một trang bằng cách đặt trang đó làm trang hiện tại.

trang trình bày/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();

Chọn một phần tử trang

Ví dụ sau cho thấy cách bỏ chọn bất kỳ lựa chọn hiện tại nào trên một trang bằng cách chọn một phần tử trang, do đó sẽ xoá tất cả các mục khác khỏi lựa chọn.

trang trình bày/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();