انتخاب موارد در یک ارائه

انتخاب همان چیزی است که در حال حاضر در یک صفحه ارائه باز انتخاب شده است، مانند گستره ای از متن برجسته یا جدول. این راهنما به شما می گوید که چگونه می توانید انتخاب را در یک ارائه فعال با استفاده از Apps Script دریافت و تنظیم کنید.

انتخاب تصویری است از آنچه در زمان شروع فیلمنامه بود. اگر کاربر کلیک کند و انتخاب تغییر کند در حالی که اسکریپت در حال اجرا است، آن تغییرات منعکس نخواهند شد.

انتخاب ها و نوع انتخاب

با استفاده از کلاس Selection می توانید انتخاب را بخوانید. کلاس متدهای مختلفی برای بدست آوردن اشیاء انتخاب شده بر اساس نوع شی(های) انتخاب شده دارد.

شماره 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();

توجه داشته باشید که صفحه فعلی ممکن است یکی از انواع زیر باشد:

صفحه فعلی می تواند یک یا چند شی انتخاب شده داشته باشد و 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() بخوانید. دو نوع انتخاب متن وجود دارد:

  • انتخاب محدوده : اگر شکلی حاوی متن "Hello" باشد و "He" انتخاب شده باشد، محدوده بازگشتی startIndex=0 و endIndex=2 دارد.
  • انتخاب مکان نما : اگر شکلی حاوی متن "Hello" باشد و مکان نما بعد از "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();

یک عنصر صفحه را انتخاب کنید

مثال زیر نشان می دهد که چگونه می توان با انتخاب یک عنصر صفحه، انتخاب های فعلی را در یک صفحه لغو انتخاب کرد، بنابراین همه موارد دیگر را از انتخاب حذف کرد.

اسلاید/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();