การแก้ไขและการจัดรูปแบบข้อความ

คุณแก้ไขและจัดรูปแบบข้อความได้โดยใช้ช่วงข้อความ ซึ่งแสดงด้วยTextRange ประเภท TextRange แสดงถึงส่วนของข้อความภายในรูปร่างหรือ ภายในเซลล์ตาราง การเรียก getText() ในรูปร่างหรือเซลล์ตารางจะแสดงผลช่วงข้อความที่ครอบคลุมข้อความทั้งหมด

หากคุณใช้วิธีที่แก้ไขวิธีที่ข้อความพอดีกับรูปร่าง ระบบจะปิดใช้งานการตั้งค่าการปรับให้พอดีอัตโนมัติ ที่ใช้กับรูปร่าง

การใช้ช่วงข้อความ

ช่วงข้อความมีดัชนี 2 รายการที่กำหนดขอบเขตของส่วนข้อความ ที่ครอบคลุมโดยช่วงข้อความ ได้แก่ ดัชนีเริ่มต้นและดัชนีสิ้นสุด คุณสามารถกำหนดดัชนีเหล่านี้ได้โดยใช้ฟังก์ชัน 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") และ แทรกข้อความ "galaxy" ที่ดัชนี 6 แทน ตัวอย่างข้างต้นจะสร้างข้อความบันทึกต่อไปนี้

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

ค้นหาและแทนที่

ใช้ฟังก์ชัน replaceAllText() ในงานนำเสนอหรือหน้าเว็บเพื่อค้นหาและแทนที่ทั่วทั้งงานนำเสนอหรือหน้าเว็บที่เฉพาะเจาะจง

find() ฟังก์ชันใน TextRange จะแสดงอินสแตนซ์ของสตริงภายใน ช่วง ใช้ร่วมกับ 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 และตั้งค่าสี เป็นสีแดง

เมื่ออ่านรูปแบบ ฟังก์ชันจะแสดงผลเป็น null หากช่วงมีค่าหลายค่า สำหรับรูปแบบ ดังนั้นตัวอย่างข้างต้นจะสร้างคำสั่งบันทึกต่อไปนี้

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

นอกจากนี้ยังมีสไตล์อื่นๆ อีกมากมายที่ใช้กับข้อความได้ ดูรายละเอียดเพิ่มเติมได้ในTextStyleเอกสารอ้างอิง

การจัดรูปแบบย่อหน้า

รูปแบบย่อหน้าจะมีผลกับทั้งย่อหน้า และรวมถึงสิ่งต่างๆ เช่น การจัดแนวข้อความและ ระยะห่างบรรทัด ฟังก์ชัน getParagraphStyle() ใน TextRange จะให้ParagraphStyle ออบเจ็กต์สำหรับการจัดรูปแบบย่อหน้าทั้งหมดที่ทับซ้อนกับช่วงข้อความหลัก

ตัวอย่างต่อไปนี้สร้างกล่องข้อความในสไลด์แรกที่มี 4 ย่อหน้า จากนั้นจัดกึ่งกลาง 3 ย่อหน้าแรก

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

การจัดรูปแบบรายการ

ListStyle ใช้จัดรูปแบบย่อหน้าทั้งหมดที่ทับซ้อนกับช่วงข้อความหลักได้เช่นเดียวกับ 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);
  // 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);
}

ตัวอย่างข้างต้นสร้างกล่องข้อความในสไลด์แรกซึ่งมี 4 ย่อหน้า ย่อหน้าที่ 2 เยื้อง 1 ครั้ง และย่อหน้าที่ 3 เยื้อง 2 ครั้ง จากนั้นจะใช้ค่ากำหนดล่วงหน้าของรายการกับย่อหน้าทั้งหมด สุดท้าย ระบบจะบันทึกระดับการซ้อนของแต่ละ ย่อหน้า (ระดับการซ้อนของย่อหน้ามาจาก จำนวนแท็บก่อนข้อความของย่อหน้า) ดังนั้น สคริปต์ด้านบนจึงสร้างข้อความบันทึกต่อไปนี้

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