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

Lựa chọn là bất kỳ nội dung nào hiện đang được chọn trong một trang trình bày đang mở, chẳng hạn như một đoạn văn bản được đánh dấu hoặc một bảng. Hướng dẫn này cho bạn biết cách nhận 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 là ảnh chụp nhanh của lựa chọn khi tập lệnh bắt đầu. Nếu người dùng nhấp 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.

Lựa chọn và loại lựa chọn

Bạn có thể đọc lựa chọn bằng cách sử dụng lớp Selection. 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(các) đối tượng đã chọn.

Liệt kê SelectionType đại diện cho loại đối tượng được chọn cụ thể. 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. Trong trường hợp này, bạn có thể truy xuất phạm vi văn bản đã chọn bằng phương thức selection.getTextRange().

Bạn cũng có thể truy xuất đối tượng chứa vùng 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 hiện đang hoạt động; để truy xuất trang đó, hãy dùng selection.getCurrentPage().

Đọc phần văn bản được chọn

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

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

Đọc trang hiện tại

Để truy xuất Page 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:

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

Xin lưu ý rằng trang hiện tại có thể là một trong các loại sau:

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

Đọc phần văn bản được chọn dựa trên loại lựa chọn

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

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;
}

Đọc phần văn bản được chọn

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

  • Chọn dải ô: Nếu một hình dạng có chứa văn bản "Hello" và "He" đượ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" và con trỏ nằm sau "H" ("H|ello"), thì dải ô được trả về là dải ô trống có startIndex=1 và endIndex=1.

Sửa đổi vùng 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 về 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 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 văn bản đã chọ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ụ:

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
//

Chọn một phần tử trên 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ụ:

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
//

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

Để thêm các phần tử khác trên trang 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 nằm trong trang hiện tại.

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
//

Biến đổi vùng chọn

Các thao tác chỉnh sửa mà tập lệnh của bạn thực hiện có thể biến đổi vùng chọn hiện tại, sao cho vùng chọn thay đổi do thao tác chỉnh sửa. Ví dụ:

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

Ví dụ sau đây cho thấy cách biến đổi vùng chọn bằng cách thao tác với các phần tử trang đã chọn.

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]
//

Chọn văn bản

Bạn có thể chọn văn bản 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 sẽ được chọn. Nếu văn bản nằm trong một ô của bảng, thì cả ô đó và bảng chứa ô đó đều được chọn.

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

Chọn dải ô trong một hình dạng

Ví dụ sau đây cho thấy cách chọn một dải ô trong văn bản có trong một hình dạng.

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
//

Chọn bằng con trỏ trong một hình dạng

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

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
//

Chọn dải ô trong một ô của bảng

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

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
//

Lựa chọn con trỏ trong TableCell

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

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
//

Biến đổi vùng chọn bằng cách chỉnh sửa văn bản

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

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
//

Đang bỏ chọn

Không có phương thức rõ ràng nào để bỏ chọn văn bản hoặc các phần tử trên 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 đây cho thấy cách bỏ chọn mọi lựa chọn hiện tại trên một trang bằng cách đặt trang đó làm trang hiện tại.

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();

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

Ví dụ sau đây cho biết cách bỏ chọn mọi lựa chọn hiện tại trên một trang bằng cách chọn một phần tử trang, nhờ đó xoá tất cả các mục khác khỏi lựa chọn.

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();