टेक्स्ट में बदलाव करना और उसे आकर्षक बनाना

टेक्स्ट रेंज का इस्तेमाल करके, टेक्स्ट में बदलाव किया जा सकता है और उसकी स्टाइल बदली जा सकती है. इन्हें TextRange टाइप से दिखाया जाता है. TextRange, टेक्स्ट के किसी सेगमेंट को आकार या टेबल सेल में दिखाता है. किसी आकार या टेबल सेल पर getText() को कॉल करने से, ऐसी टेक्स्ट रेंज दिखती है जो पूरे टेक्स्ट को कवर करती है.

अगर आप ऐसे तरीकों का इस्तेमाल करते हैं जो आकार में टेक्स्ट के फ़िट होने के तरीके में बदलाव करते हैं, तो आकार पर लागू होने वाली अपने-आप फ़िट होने वाली सभी सेटिंग बंद हो जाती हैं.

टेक्स्ट रेंज का इस्तेमाल करना

किसी टेक्स्ट रेंज में दो इंडेक्स होते हैं, जो टेक्स्ट रेंज में शामिल टेक्स्ट के सेगमेंट को सीमित करते हैं: स्टार्ट इंडेक्स और एंड इंडेक्स. getStartIndex() और getEndIndex() फ़ंक्शन का इस्तेमाल करके, इन इंडेक्स का पता लगाया जा सकता है.

किसी टेक्स्ट रेंज का कॉन्टेंट पढ़ने के लिए, asString() या asRenderedString() फ़ंक्शन का इस्तेमाल करें.

टेक्स्ट रेंज में से किसी सब-रेंज को फिर से पाने के लिए, getRange() फ़ंक्शन का इस्तेमाल करें.

यह स्क्रिप्ट पहली स्लाइड पर एक टेक्स्ट बॉक्स बनाती है और अपनी टेक्स्ट कॉन्टेंट को "नमस्ते दुनिया!" पर सेट करती है. इसके बाद, यह एक सबरेंज हासिल करता है जो सिर्फ़ "हैलो" तक लिखी होती है.

slides/style/style.gs
try {
  // Get the first slide of active presentation
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  // Insert shape in the slide with dimensions
  const shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 100, 200, 300, 60);
  const textRange = shape.getText();
  // Set text in TEXT_BOX
  textRange.setText('Hello world!');
  console.log('Start: ' + textRange.getStartIndex() + '; End: ' +
    textRange.getEndIndex() + '; Content: ' + textRange.asString());
  const subRange = textRange.getRange(0, 5);
  console.log('Sub-range Start: ' + subRange.getStartIndex() + '; Sub-range End: ' +
    subRange.getEndIndex() + '; Sub-range Content: ' + subRange.asString());
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with an error %s ', err.message);
}

किसी आकार या टेबल सेल से मिलने वाली टेक्स्ट रेंज में हमेशा पूरा टेक्स्ट शामिल होगा. भले ही, टेक्स्ट को डाला गया हो और मिटाया गया हो. इसलिए, ऊपर दिया गया उदाहरण इन लॉग स्टेटमेंट बनाता है:

Start: 0; End: 13; Content: Hello world!
Start: 0; End: 5; Content: Hello

टेक्स्ट शामिल करना और मिटाना

टेक्स्ट रेंज का इस्तेमाल करके, टेक्स्ट के आकार और टेबल सेल को भी शामिल किया जा सकता है और मिटाया जा सकता है.

  • insertText() और appendText() में टेक्स्ट डाला जा सकता है.
  • setText() किसी टेक्स्ट रेंज के टेक्स्ट को, दिए गए टेक्स्ट से बदलता है.
  • clear(), टेक्स्ट रेंज में से टेक्स्ट मिटाता है.

नीचे दी गई स्क्रिप्ट इन फ़ंक्शन के इस्तेमाल के बारे में बताती है:

slides/style/style.gs
try {
  // Get the first slide of active presentation
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  // Insert shape in the slide with dimensions
  const shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 100, 200, 300, 60);
  const textRange = shape.getText();
  textRange.setText('Hello world!');
  textRange.clear(6, 11);
  // Insert text in TEXT_BOX
  textRange.insertText(6, 'galaxy');
  console.log('Start: ' + textRange.getStartIndex() + '; End: ' +
    textRange.getEndIndex() + '; Content: ' + textRange.asString());
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with an error %s ', err.message);
}

यह स्क्रिप्ट पहली स्लाइड पर एक टेक्स्ट बॉक्स बनाती है और अपनी टेक्स्ट सामग्री को "नमस्ते दुनिया!" पर सेट कर देती है. इसके बाद, यह 6 से 11 वर्णों ("दुनिया") को मिटा देता है और इसके बजाय इंडेक्स 6 में "गैलेक्सी" टेक्स्ट डाल देता है. ऊपर दिए गए उदाहरण से यह लॉग स्टेटमेंट बनता है:

Start: 0; End: 14; Content: Hello galaxy!

खोजें और बदलें

पूरे प्रज़ेंटेशन या किसी खास पेज पर ग्लोबल खोज करने और उसे बदलने के लिए, प्रज़ेंटेशन या पेज पर replaceAllText() फ़ंक्शन का इस्तेमाल करें.

TextRange पर find() फ़ंक्शन रेंज के अंदर स्ट्रिंग के इंस्टेंस लौटाता है. इसे आकार या टेबल सेल में ढूंढें और बदलें जैसे काम करने के लिए, setText() के साथ इस्तेमाल किया जा सकता है.

पैराग्राफ़, आइटम की सूची बनाना, और रन

TextRange, टेक्स्ट इकाइयों के उपयोगी कलेक्शन दिखाने के लिए फ़ंक्शन देता है. इनमें से कुछ फ़ंक्शन में ये शामिल हैं:

  • getParagraphs(), में ऐसे सभी पैराग्राफ़ होते हैं जो टेक्स्ट की रेंज को ओवरलैप करते हैं. पैराग्राफ़, टेक्स्ट का एक क्रम होता है जो नई लाइन वर्ण, "\n" के साथ खत्म होता है.
  • getListParagraphs(),, जो मौजूदा टेक्स्ट रेंज में मौजूद सूची के आइटम दिखाता है.
  • getRuns(),, जिससे मौजूदा टेक्स्ट रेंज को ओवरलैप करने वाले टेक्स्ट रन मिलते हैं. टेक्स्ट रन, टेक्स्ट का ऐसा सेगमेंट होता है जिसमें सभी वर्णों की टेक्स्ट स्टाइल एक जैसी होती है.

टेक्स्ट स्टाइलिंग

टेक्स्ट स्टाइल, आपके प्रज़ेंटेशन में टेक्स्ट वर्णों की रेंडरिंग तय करती है. इसमें फ़ॉन्ट, रंग, और हाइपरलिंकिंग शामिल हैं.

टेक्स्ट रेंज का getTextStyle() फ़ंक्शन, TextStyle ऑब्जेक्ट देता है. इसका इस्तेमाल टेक्स्ट को स्टाइल करने के लिए किया जाता है. TextStyle ऑब्जेक्ट में वही टेक्स्ट है जो उसके पैरंट TextRange में है.

slides/style/style.gs
try {
  // Get the first slide of active presentation
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  // Insert shape in the slide with dimensions
  const shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 100, 200, 300, 60);
  const textRange = shape.getText();
  // Set text in TEXT_BOX
  textRange.setText('Hello ');
  // Append text in TEXT_BOX
  const insertedText = textRange.appendText('world!');
  // Style the text with url,bold
  insertedText.getTextStyle()
      .setBold(true)
      .setLinkUrl('www.example.com')
      .setForegroundColor('#ff0000');
  const helloRange = textRange.getRange(0, 5);
  console.log('Text: ' + helloRange.asString() + '; Bold: ' + helloRange.getTextStyle().isBold());
  console.log('Text: ' + insertedText.asString() + '; Bold: ' +
    insertedText.getTextStyle().isBold());
  console.log('Text: ' + textRange.asString() + '; Bold: ' + textRange.getTextStyle().isBold());
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with an error %s ', err.message);
}

ऊपर दिए गए उदाहरण में सबसे पहले पहली स्लाइड पर एक टेक्स्ट बॉक्स बनाया जाता है और उसके कॉन्टेंट को "नमस्ते " पर सेट किया जाता है. इसके बाद, यह "वर्ल्ड!" टेक्स्ट जोड़ देता है. जोड़े गए नए टेक्स्ट को बोल्ड किया गया है और वह www.example.com से जुड़ा है. साथ ही, इसका रंग लाल पर सेट है.

अगर स्टाइल के लिए रेंज में एक से ज़्यादा वैल्यू हैं, तो स्टाइल को पढ़ते समय, फ़ंक्शन शून्य दिखाता है. इसलिए, ऊपर दिया गया सैंपल, ये लॉग स्टेटमेंट बनाता है:

Text: Hello; Bold: false
Text: world!; Bold: true
Text: Hello world!; Bold: null

टेक्स्ट पर कई अन्य स्टाइल लागू की जा सकती हैं. ज़्यादा जानकारी के लिए, TextStyle रेफ़रंस दस्तावेज़ देखें.

पैराग्राफ़ स्टाइलिंग

पैराग्राफ़ स्टाइल पूरे पैराग्राफ़ पर लागू होती हैं. इसमें टेक्स्ट अलाइनमेंट और लाइन के बीच की दूरी जैसी चीज़ें शामिल होती हैं. TextRange में getParagraphStyle() फ़ंक्शन, उन सभी पैराग्राफ़ को स्टाइल देने के लिए ParagraphStyle ऑब्जेक्ट देता है जो पैरंट टेक्स्ट रेंज को ओवरलैप करते हैं.

इस उदाहरण में पहली स्लाइड पर एक टेक्स्ट बॉक्स बनाया गया है, जिसमें चार पैराग्राफ़ हैं. इसके बाद, पहले तीन पैराग्राफ़ को बीच में अलाइन किया गया है.

slides/style/style.gs
try {
  // Get the first slide of active presentation
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  // Insert shape in the slide with dimensions
  const shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 50, 50, 300, 300);
  const textRange = shape.getText();
  // Set the text in the shape/TEXT_BOX
  textRange.setText('Paragraph 1\nParagraph2\nParagraph 3\nParagraph 4');
  const paragraphs = textRange.getParagraphs();
  // Style the paragraph alignment center.
  for (let i = 0; i <= 3; i++) {
    const paragraphStyle = paragraphs[i].getRange().getParagraphStyle();
    paragraphStyle.setParagraphAlignment(SlidesApp.ParagraphAlignment.CENTER);
  }
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with an error %s ', err.message);
}

सूची की स्टाइलिंग

ParagraphStyle की तरह ही, ListStyle का इस्तेमाल उन सभी पैराग्राफ़ को स्टाइल देने के लिए किया जा सकता है जो पैरंट टेक्स्ट रेंज को ओवरलैप करते हैं.

slides/style/style.gs
try {
  // Get the first slide of active presentation
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  // Insert shape in the slide with dimensions
  const shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 50, 50, 300, 300);
  // Add and style the list
  const textRange = shape.getText();
  textRange.appendText('Item 1\n')
      .appendText('\tItem 2\n')
      .appendText('\t\tItem 3\n')
      .appendText('Item 4');
  // Preset patterns of glyphs for lists in text.
  textRange.getListStyle().applyListPreset(SlidesApp.ListPreset.DIGIT_ALPHA_ROMAN);
  const paragraphs = textRange.getParagraphs();
  for (let i = 0; i < paragraphs.length; i++) {
    const listStyle = paragraphs[i].getRange().getListStyle();
    console.log('Paragraph ' + (i + 1) + '\'s nesting level: ' + listStyle.getNestingLevel());
  }
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with an error %s ', err.message);
}

ऊपर दिए गए उदाहरण में पहली स्लाइड पर एक टेक्स्ट बॉक्स बनाया गया है. इसमें चार पैराग्राफ़ हैं: दूसरे पैराग्राफ़ को एक बार और तीसरा पैराग्राफ़ दो बार इंडेंट किया जाता है. इसके बाद, यह सभी पैराग्राफ़ पर सूची प्रीसेट लागू करता है. आखिर में, हर पैराग्राफ़ के नेस्टिंग लेवल को लॉग किया जाता है. (पैराग्राफ़ का नेस्टिंग स्तर, पैराग्राफ़ के टेक्स्ट से पहले टैब की संख्या से आता है.) इसलिए, ऊपर दी गई स्क्रिप्ट इन लॉग स्टेटमेंट बनाती है:

Paragraph 1's nesting level: 0
Paragraph 2's nesting level: 1
Paragraph 3's nesting level: 2
Paragraph 4's nesting level: 0