प्रज़ेंटेशन में आइटम चुनना

चुनना वह चीज़ है जिसे अभी खुले हुए प्रज़ेंटेशन वाले पेज पर चुना गया है. जैसे, हाइलाइट किए गए टेक्स्ट या टेबल को अलग-अलग सेक्शन में रखना. इस गाइड में Apps Script का इस्तेमाल करके, किसी चालू प्रज़ेंटेशन में चुने गए विकल्प को पाने और उसे सेट करने का तरीका बताया गया है.

चुने गए विकल्प से, स्क्रिप्ट शुरू होने के समय की झलक का स्नैपशॉट मिलता है. अगर स्क्रिप्ट चलने के दौरान उपयोगकर्ता क्लिक करता है और चुने गए विकल्प बदल जाते हैं, तो वे बदलाव नहीं दिखेंगे.

चुने गए और चुने जाने का प्रकार

चुने गए विकल्पों को पढ़ने के लिए, सिलेक्शन क्लास का इस्तेमाल किया जा सकता है. चुने गए ऑब्जेक्ट के टाइप के आधार पर चुने गए ऑब्जेक्ट पाने के लिए क्लास में कई तरीके हैं.

SelectionType Enum चुने गए ऑब्जेक्ट के खास टाइप के बारे में बताता है. उदाहरण के लिए, अगर उपयोगकर्ता ने आकार में कुछ टेक्स्ट चुना है, तो चुनने का टाइप TEXT होगा. इस मामले में, selection.getTextRange() तरीके का इस्तेमाल करके, टेक्स्ट की चुनी गई रेंज को वापस लाया जा सकता है.

आपके पास उस ऑब्जेक्ट को वापस पाने का भी विकल्प है जिसमें चुना गया ऑब्जेक्ट मौजूद है. ऊपर दिए गए उदाहरण को जारी रखते हुए, selection.getPageElementRange().getPageElements()[0] का इस्तेमाल करके, चुने गए टेक्स्ट वाले आकार को वापस पाया जा सकता है. इसी तरह, जिस पेज में शामिल का आकार होता है वह मौजूदा ऐक्टिव पेज होता है; उस पेज को वापस पाने के लिए, selection.getCurrentPage() का इस्तेमाल करें.

चुने गए को पढ़ना

चुने गए टेक्स्ट को पढ़ने के लिए, Presentation.getSelection() तरीका इस्तेमाल करें, जैसा कि इस उदाहरण में दिखाया गया है:

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

मौजूदा पेज को पढ़ना

उपयोगकर्ता जिस मौजूदा पेज को देख रहा है उसे वापस पाने के लिए, getSelection() और getCurrentPage() तरीका इस्तेमाल करें. इसके लिए, ये तरीके अपनाएं:

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

ध्यान दें कि मौजूदा पेज इनमें से किसी भी तरह का हो सकता है:

मौजूदा पेज में एक या उससे ज़्यादा ऑब्जेक्ट चुने जा सकते हैं और SelectionType चुनने का टाइप तय करता है.

चुनने के टाइप के आधार पर चुने गए टेक्स्ट को पढ़ना

इस उदाहरण में दिखाया गया है कि मौजूदा चुने गए टेक्स्ट को पढ़ने के लिए, चुनने के टाइप का इस्तेमाल कैसे किया जा सकता है.

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

चुने गए टेक्स्ट को पढ़ना

Selection.getTextRange() तरीके का इस्तेमाल करके, चुने गए टेक्स्ट को पढ़ा जा सकता है. टेक्स्ट चुनने के दो टाइप होते हैं:

  • रेंज चुनना: अगर किसी आकार में "नमस्ते" टेक्स्ट है और "वह" चुना गया है, तो दिखाई गई रेंज में startIndex=0 और endIndex=2 होगा.
  • कर्सर चुनना: अगर किसी आकार में "हैलो" टेक्स्ट है और कर्सर "H" ("H|ello") के बाद है, तो नतीजे की रेंज खाली होती है. इसमें startIndex=1 और endIndex=1 शामिल होता है.

चुने गए में बदलाव किया जा रहा है

स्क्रिप्ट उपयोगकर्ता के चुने हुए विकल्प में बदलाव कर सकती है. स्क्रिप्ट की ओर से प्रज़ेंटेशन में किए जाने वाले बदलाव, स्क्रिप्ट के चलने की अवधि के दौरान चुनने की बाद की कार्रवाइयों में दिखते हैं.

स्क्रिप्ट का काम पूरा होने के बाद या Presentation.saveAndClose() को कॉल करने के बाद ही, चुनने की प्रक्रिया में किए गए बदलाव उपयोगकर्ता के ब्राउज़र पर दिखते हैं.

मौजूदा पेज को चुनना

selectAsCurrentPage() का इस्तेमाल करके, चालू प्रज़ेंटेशन में किसी पेज को मौजूदा पेज के तौर पर चुना जा सकता है. इस तरीके से, चुने गए पिछले पेज एलिमेंट, पेज या टेक्स्ट को हटा दिया जाता है. इसलिए, मौजूदा पेज पर इस तरीके का इस्तेमाल करने से, आपके पास पेज पर चुने गए किसी भी मौजूदा विकल्प से चुने हुए का निशान हटाने का विकल्प होता है. उदाहरण के लिए:

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

पेज एलिमेंट चुनना

किसी पेज में कोई पेज एलिमेंट चुनने के लिए, PageElement.select() का इस्तेमाल करें. ऐसा करने से, पहले चुने गए पेज के एलिमेंट से चुने हुए का निशान भी हट जाएगा.

उदाहरण के लिए:

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

पेज के एक से ज़्यादा एलिमेंट चुनना

चुने गए विकल्प में, पेज के अन्य एलिमेंट जोड़ने के लिए, PageElement.select(false) तरीके का इस्तेमाल करें. पेज के सभी एलिमेंट, मौजूदा पेज में ही होने चाहिए.

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

चुने गए हिस्से को बदला जा रहा है

आपकी स्क्रिप्ट में किए जाने वाले बदलाव, मौजूदा चुने गए विकल्प को पूरी तरह बदल सकते हैं. इससे, बदलाव करने की वजह से चुने गए बदलाव दिखेंगे. उदाहरण के लिए:

  1. मान लें कि आपने दो आकार A और B चुने हैं.
  2. इसके बाद, आपकी स्क्रिप्ट आकार A को हटा देती है.
  3. इसलिए, चुने गए हिस्से को बदलाव के हिसाब से बदल दिया जाता है, ताकि सिर्फ़ आकार B चुना जाए.

इस उदाहरण में दिखाया गया है कि चुने गए पेज एलिमेंट में बदलाव करके, चुने गए पेजों को कैसे बदला जा सकता है.

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

टेक्स्ट चुनना

किसी आकार या टेबल सेल में मौजूद टेक्स्ट को चुनने के लिए, TextRange.select() तरीका इस्तेमाल किया जा सकता है. अगर टेक्स्ट किसी आकार में है, तो वह आकार भी चुना जाता है. अगर टेक्स्ट किसी टेबल सेल में है, तो टेबल सेल और उसके शामिल होने की टेबल, दोनों को चुना जाता है.

इससे, पैरंट पेज भी मौजूदा पेज के रूप में सेट हो जाता है.

आकार में रेंज चुनने की सुविधा

नीचे दिए गए उदाहरण में, आकार में शामिल टेक्स्ट में रेंज चुनने का तरीका बताया गया है.

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

कर्सर को आकार में चुनना

इस उदाहरण में, आकार में शामिल टेक्स्ट में कर्सर चुनने का तरीका बताया गया है.

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

टेबल की किसी सेल में रेंज चुनने की सुविधा

नीचे दिए गए उदाहरण में बताया गया है कि टेबल सेल के टेक्स्ट में रेंज चुनने का तरीका क्या है.

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

TablesCell में कर्सर चुनना

इस उदाहरण में दिखाया गया है कि टेबल सेल में शामिल टेक्स्ट में कर्सर चुनने का तरीका कैसे बताया जाता है.

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

टेक्स्ट में बदलाव करने की सुविधा के साथ चुना गया बदलाव

इस उदाहरण में दिखाया गया है कि चुने गए टेक्स्ट में बदलाव करके, चुने गए टेक्स्ट को कैसे बदला जा सकता है.

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

चुना जा रहा है

टेक्स्ट या पेज एलिमेंट से चुने हुए का निशान हटाने का कोई साफ़ तरीका नहीं है. हालांकि, Page.selectAsCurrentPage() या pageElement.select() तरीकों का इस्तेमाल करके, यह नतीजा पाया जा सकता है.

मौजूदा पेज चुनें

इस उदाहरण में दिखाया गया है कि किसी पेज को मौजूदा पेज के तौर पर सेट करके, उस पेज पर चुने गए मौजूदा पेज से चुने हुए का निशान कैसे हटाया जा सकता है.

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

पेज एलिमेंट चुनें

इस उदाहरण में दिखाया गया है कि किसी पेज पर चुने गए मौजूदा आइटम से चुने हुए का निशान हटाने का क्या तरीका है. इसके लिए, पेज के किसी एक एलिमेंट को चुनें और फिर चुने गए आइटम से दूसरे सभी आइटम हटा दें.

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