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

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

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

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

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

ตัวอย่างข้างต้นจะสร้างกล่องข้อความในสไลด์แรกก่อน และตั้งค่าเนื้อหาเป็น "สวัสดี " จากนั้นจึงต่อท้ายข้อความ "โลก!" ข้อความที่ต่อท้ายใหม่จะเป็นตัวหนา ลิงก์กับ 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