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

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

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

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

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

टेक्स्ट रेंज का शुरुआती इंडेक्स शामिल होता है, जबकि इसका आखिरी इंडेक्स शामिल नहीं होता. दोनों इंडेक्स, शून्य पर आधारित होते हैं.

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

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

नीचे दी गई स्क्रिप्ट, पहली स्लाइड पर एक टेक्स्ट बॉक्स बनाती है और उसके टेक्स्ट कॉन्टेंट को "Hello world!" पर सेट करती है. इसके बाद, यह सिर्फ़ "Hello" वाले सब-रेंज को वापस लाता है.

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

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

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

ऊपर दिए गए उदाहरण में, सबसे पहले पहली स्लाइड पर एक टेक्स्ट बॉक्स बनाया गया है. इसके बाद, इसके कॉन्टेंट को "Hello " पर सेट किया गया है. इसके बाद, इसमें "world!" टेक्स्ट जोड़ा गया है. नया जोड़ा गया टेक्स्ट बोल्ड किया गया है. इसे 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