Class TableOfContents

สารบัญ

องค์ประกอบที่มีสารบัญ TableOfContents อาจมีองค์ประกอบ ListItem, Paragraph และ Table แม้ว่าโดยปกติแล้วเนื้อหาของ TableOfContents จะสร้างขึ้นโดยอัตโนมัติใน Google เอกสาร ดูข้อมูลเพิ่มเติมเกี่ยวกับ โครงสร้างเอกสารได้ในคำแนะนำในการ ขยาย Google เอกสาร

เมธอด

วิธีการประเภทการแสดงผลรายละเอียดแบบย่อ
clear()TableOfContentsล้างเนื้อหาขององค์ประกอบ
copy()TableOfContentsแสดงผลสำเนาแบบลึกที่แยกออกมาขององค์ประกอบปัจจุบัน
editAsText()Textรับองค์ประกอบปัจจุบันเวอร์ชัน Text เพื่อแก้ไข
findElement(elementType)RangeElement|nullค้นหาเนื้อหาขององค์ประกอบสำหรับองค์ประกอบย่อยของประเภทที่ระบุ
findElement(elementType, from)RangeElement|nullค้นหาเนื้อหาขององค์ประกอบสำหรับองค์ประกอบย่อยของประเภทที่ระบุ โดยเริ่มจาก RangeElement ที่ระบุ
findText(searchPattern)RangeElement|nullค้นหารูปแบบข้อความที่ระบุในเนื้อหาขององค์ประกอบโดยใช้นิพจน์ทั่วไป
findText(searchPattern, from)RangeElement|nullค้นหารูปแบบข้อความที่ระบุในเนื้อหาขององค์ประกอบ โดยเริ่มจากผลการค้นหาที่ระบุ
getAttributes()Objectดึงแอตทริบิวต์ขององค์ประกอบ
getChild(childIndex)Elementดึงข้อมูลองค์ประกอบย่อยที่ดัชนีขององค์ประกอบย่อยที่ระบุ
getChildIndex(child)Integerดึงข้อมูลดัชนีขององค์ประกอบย่อยสำหรับองค์ประกอบย่อยที่ระบุ
getLinkUrl()String|nullเรียก URL ของลิงก์
getNextSibling()Element|nullดึงองค์ประกอบพี่น้องถัดไปขององค์ประกอบ
getNumChildren()Integerดึงข้อมูลจำนวนเด็ก
getParent()ContainerElement|nullดึงองค์ประกอบหลักขององค์ประกอบ
getPreviousSibling()Element|nullดึงองค์ประกอบพี่น้องก่อนหน้าขององค์ประกอบ
getText()Stringดึงเนื้อหาขององค์ประกอบเป็นสตริงข้อความ
getTextAlignment()TextAlignment|nullรับการจัดข้อความ
getType()ElementTypeดึง ElementType ขององค์ประกอบ
isAtDocumentEnd()Booleanกำหนดว่าองค์ประกอบอยู่ท้าย Document หรือไม่
removeFromParent()TableOfContents|nullนำองค์ประกอบออกจากองค์ประกอบหลัก
replaceText(searchPattern, replacement)Elementแทนที่รูปแบบข้อความที่กำหนดทั้งหมดด้วยสตริงทดแทนที่กำหนดโดยใช้นิพจน์ทั่วไป
setAttributes(attributes)TableOfContentsตั้งค่าแอตทริบิวต์ขององค์ประกอบ
setLinkUrl(url)TableOfContentsตั้งค่า URL ของลิงก์
setTextAlignment(textAlignment)TableOfContentsตั้งค่าการจัดข้อความ

เอกสารโดยละเอียด

clear()

ล้างเนื้อหาขององค์ประกอบ

รีเทิร์น

TableOfContents — องค์ประกอบปัจจุบัน


copy()

แสดงผลสำเนาแบบลึกที่แยกออกมาขององค์ประกอบปัจจุบัน

ระบบจะคัดลอกองค์ประกอบย่อยที่อยู่ในองค์ประกอบด้วย องค์ประกอบใหม่ไม่มี องค์ประกอบหลัก

รีเทิร์น

TableOfContents — สำเนาใหม่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

editAsText()

รับองค์ประกอบปัจจุบันเวอร์ชัน Text เพื่อแก้ไข

ใช้ editAsText เพื่อจัดการเนื้อหาขององค์ประกอบเป็น Rich Text editAsText จะไม่สนใจองค์ประกอบที่ไม่ใช่ข้อความ (เช่น InlineImage และ HorizontalRule)

ระบบจะนำองค์ประกอบย่อยที่อยู่ในช่วงข้อความที่ถูกลบออกอย่างสมบูรณ์ออกจากองค์ประกอบ

const body =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Insert two paragraphs separated by a paragraph containing an
// horizontal rule.
body.insertParagraph(0, 'An editAsText sample.');
body.insertHorizontalRule(0);
body.insertParagraph(0, 'An example.');

// Delete " sample.\n\n An" removing the horizontal rule in the process.
body.editAsText().deleteText(14, 25);

รีเทิร์น

Text - ข้อความขององค์ประกอบปัจจุบัน


findElement(elementType)

ค้นหาเนื้อหาขององค์ประกอบสำหรับองค์ประกอบย่อยของประเภทที่ระบุ

พารามิเตอร์

ชื่อประเภทคำอธิบาย
elementTypeElementTypeประเภทขององค์ประกอบที่จะค้นหา

รีเทิร์น

RangeElement|null — ผลการค้นหาที่ระบุตำแหน่งขององค์ประกอบการค้นหา

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

findElement(elementType, from)

ค้นหาเนื้อหาขององค์ประกอบสำหรับองค์ประกอบย่อยของประเภทที่ระบุ โดยเริ่มจาก RangeElement ที่ระบุ

const body =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Define the search parameters.

let searchResult = null;

// Search until the paragraph is found.
while (
    (searchResult = body.findElement(
         DocumentApp.ElementType.PARAGRAPH,
         searchResult,
         ))) {
  const par = searchResult.getElement().asParagraph();
  if (par.getHeading() === DocumentApp.ParagraphHeading.HEADING1) {
    // Found one, update and stop.
    par.setText('This is the first header.');
    break;
  }
}

พารามิเตอร์

ชื่อประเภทคำอธิบาย
elementTypeElementTypeประเภทขององค์ประกอบที่จะค้นหา
fromRangeElementผลการค้นหาที่จะค้นหา

รีเทิร์น

RangeElement|null — ผลการค้นหาที่ระบุตำแหน่งถัดไปขององค์ประกอบการค้นหา

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

findText(searchPattern)

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

ระบบไม่รองรับฟีเจอร์นิพจน์ทั่วไปของ JavaScript บางส่วนอย่างเต็มที่ เช่น กลุ่มจับภาพและตัวแก้ไขโหมด

รูปแบบนิพจน์ทั่วไปที่ระบุจะได้รับการจับคู่กับบล็อกข้อความแต่ละบล็อก ที่อยู่ในองค์ประกอบปัจจุบันโดยอิสระ

พารามิเตอร์

ชื่อประเภทคำอธิบาย
searchPatternStringรูปแบบที่จะค้นหา

รีเทิร์น

RangeElement|null — ผลการค้นหาที่ระบุตำแหน่งของข้อความค้นหา หรือ null หากไม่มี รายการที่ตรงกัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

findText(searchPattern, from)

ค้นหารูปแบบข้อความที่ระบุในเนื้อหาขององค์ประกอบ โดยเริ่มจากผลการค้นหาที่ระบุ

ระบบไม่รองรับฟีเจอร์นิพจน์ทั่วไปของ JavaScript บางส่วนอย่างเต็มที่ เช่น กลุ่มจับภาพและตัวแก้ไขโหมด

รูปแบบนิพจน์ทั่วไปที่ระบุจะได้รับการจับคู่กับบล็อกข้อความแต่ละบล็อก ที่อยู่ในองค์ประกอบปัจจุบันโดยอิสระ

พารามิเตอร์

ชื่อประเภทคำอธิบาย
searchPatternStringรูปแบบที่จะค้นหา
fromRangeElementผลการค้นหาที่จะค้นหา

รีเทิร์น

RangeElement|null — ผลการค้นหาที่ระบุตำแหน่งถัดไปของข้อความค้นหา หรือ null หากไม่มี รายการที่ตรงกัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getAttributes()

ดึงแอตทริบิวต์ขององค์ประกอบ

ผลลัพธ์คือออบเจ็กต์ที่มีพร็อพเพอร์ตี้สำหรับแอตทริบิวต์ขององค์ประกอบที่ถูกต้องแต่ละรายการ โดยที่ชื่อพร็อพเพอร์ตี้แต่ละชื่อจะสอดคล้องกับรายการในDocumentApp.Attributeการแจงนับ

const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Append a styled paragraph.
const par = body.appendParagraph('A bold, italicized paragraph.');
par.setBold(true);
par.setItalic(true);

// Retrieve the paragraph's attributes.
const atts = par.getAttributes();

// Log the paragraph attributes.
for (const att in atts) {
  Logger.log(`${att}:${atts[att]}`);
}

รีเทิร์น

Object — แอตทริบิวต์ขององค์ประกอบ

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getChild(childIndex)

ดึงข้อมูลองค์ประกอบย่อยที่ดัชนีขององค์ประกอบย่อยที่ระบุ

const body =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Obtain the first element in the tab.
const firstChild = body.getChild(0);

// If it's a paragraph, set its contents.
if (firstChild.getType() === DocumentApp.ElementType.PARAGRAPH) {
  firstChild.asParagraph().setText('This is the first paragraph.');
}

พารามิเตอร์

ชื่อประเภทคำอธิบาย
childIndexIntegerดัชนีขององค์ประกอบย่อยที่จะดึงข้อมูล

รีเทิร์น

Element - องค์ประกอบย่อยที่ดัชนีที่ระบุ

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getChildIndex(child)

ดึงข้อมูลดัชนีขององค์ประกอบย่อยสำหรับองค์ประกอบย่อยที่ระบุ

พารามิเตอร์

ชื่อประเภทคำอธิบาย
childElementองค์ประกอบย่อยที่จะดึงดัชนี

รีเทิร์น

Integer — ดัชนีขององค์ประกอบย่อย

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getLinkUrl()

เรียก URL ของลิงก์

รีเทิร์น

String|null — URL ของลิงก์ หรือ null หากองค์ประกอบมีค่าหลายค่าสำหรับแอตทริบิวต์นี้

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getNextSibling()

ดึงองค์ประกอบพี่น้องถัดไปขององค์ประกอบ

องค์ประกอบระดับข้างเคียงถัดไปมีองค์ประกอบหลักเดียวกันและอยู่ต่อจากองค์ประกอบปัจจุบัน

รีเทิร์น

Element|null — องค์ประกอบร่วมถัดไป

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getNumChildren()

ดึงข้อมูลจำนวนเด็ก

const body =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Log the number of elements in the tab.
Logger.log(`There are ${body.getNumChildren()} elements in the tab's body.`);

รีเทิร์น

Integer — จำนวนเด็ก

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getParent()

ดึงองค์ประกอบหลักขององค์ประกอบ

องค์ประกอบหลักมีองค์ประกอบปัจจุบัน

รีเทิร์น

ContainerElement|null — องค์ประกอบระดับบนสุด

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getPreviousSibling()

ดึงองค์ประกอบพี่น้องก่อนหน้าขององค์ประกอบ

องค์ประกอบระดับข้างเคียงก่อนหน้ามีองค์ประกอบระดับบนสุดเดียวกันและอยู่ก่อนองค์ประกอบปัจจุบัน

รีเทิร์น

Element|null — องค์ประกอบระดับข้างเคียงก่อนหน้า

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getText()

ดึงเนื้อหาขององค์ประกอบเป็นสตริงข้อความ

รีเทิร์น

String — เนื้อหาขององค์ประกอบเป็นสตริงข้อความ

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getTextAlignment()

รับการจัดข้อความ ประเภทการจัดแนวที่ใช้ได้คือ DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPT และ DocumentApp.TextAlignment.SUPERSCRIPT

รีเทิร์น

TextAlignment|null — ประเภทการจัดแนวข้อความ หรือ null หากข้อความมีการจัดแนวข้อความหลายประเภท หรือหากไม่เคยตั้งค่าการจัดแนวข้อความ

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getType()

ดึง ElementType ขององค์ประกอบ

ใช้ getType() เพื่อระบุประเภทที่แน่นอนขององค์ประกอบที่กำหนด

const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Obtain the first element in the active tab's body.

const firstChild = body.getChild(0);

// Use getType() to determine the element's type.
if (firstChild.getType() === DocumentApp.ElementType.PARAGRAPH) {
  Logger.log('The first element is a paragraph.');
} else {
  Logger.log('The first element is not a paragraph.');
}

รีเทิร์น

ElementType — ประเภทองค์ประกอบ

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

isAtDocumentEnd()

กำหนดว่าองค์ประกอบอยู่ท้าย Document หรือไม่

รีเทิร์น

Boolean — องค์ประกอบอยู่ที่ส่วนท้ายของแท็บหรือไม่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

removeFromParent()

นำองค์ประกอบออกจากองค์ประกอบหลัก

const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Remove all images in the active tab's body.
const imgs = body.getImages();
for (let i = 0; i < imgs.length; i++) {
  imgs[i].removeFromParent();
}

รีเทิร์น

TableOfContents|null — องค์ประกอบที่นำออก

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

replaceText(searchPattern, replacement)

แทนที่รูปแบบข้อความที่กำหนดทั้งหมดด้วยสตริงทดแทนที่กำหนดโดยใช้นิพจน์ทั่วไป

ระบบจะส่งรูปแบบการค้นหาเป็นสตริง ไม่ใช่ออบเจ็กต์นิพจน์ทั่วไปของ JavaScript ด้วยเหตุนี้ คุณจึงต้องหลีกเลี่ยงการใช้แบ็กสแลชในรูปแบบ

วิธีการนี้ใช้ไลบรารีนิพจน์ทั่วไป RE2 ของ Google ซึ่งจำกัดไวยากรณ์ที่รองรับ

รูปแบบนิพจน์ทั่วไปที่ระบุจะได้รับการจับคู่กับบล็อกข้อความแต่ละบล็อก ที่อยู่ในองค์ประกอบปัจจุบันโดยอิสระ

const body =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Clear the text surrounding "Apps Script", with or without text.
body.replaceText('^.*Apps ?Script.*$', 'Apps Script');

พารามิเตอร์

ชื่อประเภทคำอธิบาย
searchPatternStringรูปแบบนิพจน์ทั่วไปที่จะค้นหา
replacementStringข้อความที่จะใช้แทน

รีเทิร์น

Element — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setAttributes(attributes)

ตั้งค่าแอตทริบิวต์ขององค์ประกอบ

พารามิเตอร์แอตทริบิวต์ที่ระบุต้องเป็นออบเจ็กต์ที่ชื่อพร็อพเพอร์ตี้แต่ละรายการเป็นรายการใน การแจงนับ DocumentApp.Attribute และค่าพร็อพเพอร์ตี้แต่ละรายการเป็นค่าใหม่ที่จะ ใช้

const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Define a custom paragraph style.
const style = {};
style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
    DocumentApp.HorizontalAlignment.RIGHT;
style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style[DocumentApp.Attribute.FONT_SIZE] = 18;
style[DocumentApp.Attribute.BOLD] = true;

// Append a plain paragraph.
const par = body.appendParagraph('A paragraph with custom style.');

// Apply the custom style.
par.setAttributes(style);

พารามิเตอร์

ชื่อประเภทคำอธิบาย
attributesObjectแอตทริบิวต์ขององค์ประกอบ

รีเทิร์น

TableOfContents — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setLinkUrl(url)

ตั้งค่า URL ของลิงก์

พารามิเตอร์

ชื่อประเภทคำอธิบาย
urlStringURL ของลิงก์

รีเทิร์น

TableOfContents — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setTextAlignment(textAlignment)

ตั้งค่าการจัดข้อความ ประเภทการจัดแนวที่ใช้ได้คือ DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPT และ DocumentApp.TextAlignment.SUPERSCRIPT

// Make the entire first paragraph in the active tab be superscript.
const documentTab =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab();
const text = documentTab.getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);

พารามิเตอร์

ชื่อประเภทคำอธิบาย
textAlignmentTextAlignmentประเภทการจัดแนวข้อความที่จะใช้

รีเทิร์น

TableOfContents — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents