Elemente in einer Präsentation auswählen

Die Auswahl ist der Inhalt, der auf einer geöffneten Präsentationsseite im Fokus steht, z. B. ein Bereich mit markiertem Text oder eine Tabelle. In diesem Leitfaden wird beschrieben, wie Sie die Auswahl in einer aktiven Präsentation mit Apps Script abrufen und festlegen.

Ein Skript kann nur auf die Auswahl des Nutzers zugreifen, der das Skript ausführt.

Die Auswahl ist eine Momentaufnahme des Zustands zu Beginn der Ausführung des Skripts. Wenn der Nutzer klickt und sich die Auswahl ändert, während das Script ausgeführt wird, werden diese Änderungen nicht berücksichtigt.

Auswahlen und Auswahltyp

Lesen Sie die Auswahl mit der Klasse Selection. Die Klasse bietet verschiedene Methoden zum Abrufen der ausgewählten Objekte basierend auf dem Typ der ausgewählten Objekte.

Das Enum SelectionType stellt den spezifischen Typ der ausgewählten Objekte dar. Wenn der Nutzer beispielsweise Text in einer Form ausgewählt hat, ist der Auswahltyp TEXT. In diesem Fall können Sie den ausgewählten Textbereich mit der Methode selection.getTextRange() abrufen.

Sie können auch das Objekt abrufen, das die Auswahl enthält. Sie können beispielsweise die Form mit dem ausgewählten Text mit selection.getPageElementRange().getPageElements()[0] abrufen. Die Seite, die die umschließende Form enthält, ist die aktuelle aktive Seite. Verwenden Sie selection.getCurrentPage(), um diese Seite abzurufen.

Auswahl vorlesen

Verwenden Sie zum Lesen der Auswahl die Methode Presentation.getSelection(), wie im folgenden Beispiel gezeigt:

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

Aktuelle Seite lesen

Verwenden Sie die Methoden getSelection() und getCurrentPage(), um die aktuelle Seite abzurufen, die der Nutzer aufruft:

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

Die aktuelle Seite kann einen der folgenden Typen haben:

Auf der aktuellen Seite können ein oder mehrere Objekte ausgewählt sein. Der Typ der Auswahl wird durch SelectionType bestimmt.

Auswahl basierend auf dem Auswahltyp lesen

Das folgende Beispiel zeigt, wie Sie den Auswahltyp verwenden können, um die aktuelle Auswahl typgerecht zu lesen.

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

Ausgewählten Text vorlesen lassen

Lesen Sie die Textauswahl mit der Methode Selection.getTextRange(). Es gibt zwei Arten der Textauswahl:

  • Bereichsauswahl: Wenn eine Form den Text „Hallo“ enthält und „Ha“ ausgewählt ist, hat der zurückgegebene Bereich „startIndex=0“ und „endIndex=2“.
  • Cursorauswahl: Wenn eine Form den Text „Hallo“ enthält und der Cursor sich hinter „H“ befindet („H|allo“), ist der zurückgegebene Bereich ein leerer Bereich mit startIndex=1 und endIndex=1.

Auswahl ändern

Das Skript kann die Auswahl des Nutzers ändern. Alle Auswahländerungen, die das Skript an der Präsentation vornimmt, werden für die Dauer der Skriptausführung in nachfolgenden Auswahlvorgängen berücksichtigt.

Die Änderungen an der Auswahl werden erst im Browser des Nutzers übernommen, wenn die Ausführung des Skripts abgeschlossen ist oder Presentation.saveAndClose() aufgerufen wird.

Aktuelle Seite auswählen

Eine Seite in der aktiven Präsentation kann als aktuelle Seite ausgewählt werden, indem die Methode selectAsCurrentPage() aufgerufen wird. Mit dieser Methode werden alle vorherigen Seitenelemente, Seiten oder Textauswahlen entfernt. Wenn Sie diese Methode auf der aktuellen Seite verwenden, können Sie alle aktuellen Auswahlen auf der Seite aufheben. Beispiel:

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

Seitenelement auswählen

Verwenden Sie die Methode PageElement.select(), um ein Seitenelement auf einer Seite auszuwählen. Dadurch wird auch die Auswahl aller zuvor ausgewählten Seitenelemente aufgehoben.

Die Methoden select() und select(true) sind gleichwertig.

Beispiel:

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

Mehrere Seitenelemente auswählen

Wenn Sie der Auswahl weitere Seitenelemente hinzufügen möchten, verwenden Sie die Methode PageElement.select(false). Alle Seitenelemente müssen auf der aktuellen Seite vorhanden sein.

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

Auswahl transformieren

Durch Bearbeitungen, die von Ihrem Skript ausgeführt werden, kann die aktuelle Auswahl transformiert werden. Das bedeutet, dass sich die Auswahl durch die Bearbeitung ändert. Beispiel:

  1. Angenommen, Sie haben zwei Formen (A und B) ausgewählt.
  2. Als Nächstes wird Form A entfernt.
  3. Die Auswahl wird entsprechend der Bearbeitung transformiert, sodass nur Form B ausgewählt ist.

Im folgenden Beispiel sehen Sie, wie die Auswahl durch Bearbeiten ausgewählter Seitenelemente transformiert werden kann.

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

Text auswählen

Text in einer Form oder Tabellenzelle kann mit der Methode TextRange.select() ausgewählt werden. Wenn der Text in einer Form enthalten ist, wird auch diese Form ausgewählt. Wenn sich der Text in einer Tabellenzelle befindet, werden sowohl die Tabellenzelle als auch die zugehörige Tabelle ausgewählt.

Dadurch wird auch die übergeordnete Seite als aktuelle Seite festgelegt.

Bereichsauswahl in einer Form

Im folgenden Beispiel wird gezeigt, wie Sie einen Bereich in Text auswählen, der in einer Form enthalten ist.

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

Cursorauswahl in einer Form

Im folgenden Beispiel wird gezeigt, wie Sie eine Cursorauswahl in Text vornehmen, der in einer Form enthalten ist.

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

Bereichsauswahl in einer Tabellenzelle

Im folgenden Beispiel wird gezeigt, wie Sie einen Bereich in Text auswählen, der in einer Tabellenzelle enthalten ist.

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

Cursorauswahl in TableCell

Das folgende Beispiel zeigt, wie Sie eine Cursorauswahl in Text vornehmen, der in einer Tabellenzelle enthalten ist.

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

Auswahltransformation mit Textbearbeitungen

Das folgende Beispiel zeigt, wie die Auswahl durch Bearbeiten des ausgewählten Texts transformiert werden kann.

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

Auswahl aufheben

Es gibt keine expliziten Methoden zum Aufheben der Auswahl von Text oder Seitenelementen. Dieses Ergebnis kann jedoch mit den Methoden Page.selectAsCurrentPage() oder pageElement.select() erzielt werden.

Aktuelle Seite auswählen

Im folgenden Beispiel wird gezeigt, wie Sie die aktuelle Auswahl auf einer Seite aufheben, indem Sie diese Seite als aktuelle Seite festlegen.

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

Seitenelement auswählen

Im folgenden Beispiel wird gezeigt, wie Sie die aktuelle Auswahl auf einer Seite aufheben, indem Sie ein Seitenelement auswählen. Dadurch werden alle anderen Elemente aus der Auswahl entfernt.

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