Class DocumentTab

แท็บเอกสาร

แท็บเอกสารที่มีข้อความที่จัดรูปแบบและองค์ประกอบต่างๆ เช่น ตารางและรายการ

ดึงแท็บเอกสารโดยใช้ Document.getTabs()[tabIndex].asDocumentTab()

// Get a specific document tab based on the tab ID.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();

เมธอด

วิธีการประเภทการแสดงผลรายละเอียดแบบย่อ
addBookmark(position)Bookmarkเพิ่ม Bookmark ที่ Position ที่ระบุ
addFooter()FooterSectionเพิ่มส่วนท้ายของแท็บ หากยังไม่มี
addHeader()HeaderSectionเพิ่มส่วนหัวของแท็บ หากไม่มี
addNamedRange(name, range)NamedRangeเพิ่ม NamedRange ซึ่งเป็น Range ที่มีชื่อและรหัสสำหรับใช้ ในการดึงข้อมูลในภายหลัง
getBody()Bodyเรียกข้อมูล Body ของแท็บ
getBookmark(id)Bookmark|nullรับ Bookmark ที่มีรหัสที่ระบุ
getBookmarks()Bookmark[]รับออบเจ็กต์ Bookmark ทั้งหมดในแท็บ
getFooter()FooterSection|nullดึงข้อมูลส่วนท้ายของแท็บ หากมี
getFootnotes()Footnote[]|nullดึงข้อมูลองค์ประกอบ Footnote ทั้งหมดในเนื้อหาของแท็บ
getHeader()HeaderSection|nullดึงข้อมูลส่วนหัวของแท็บ หากมี
getNamedRangeById(id)NamedRange|nullรับ NamedRange ที่มีรหัสที่ระบุ
getNamedRanges()NamedRange[]รับออบเจ็กต์ NamedRange ทั้งหมดในแท็บ
getNamedRanges(name)NamedRange[]รับออบเจ็กต์ NamedRange ทั้งหมดในแท็บที่มีชื่อที่ระบุ
newPosition(element, offset)Positionสร้าง Position ใหม่ ซึ่งเป็นการอ้างอิงถึงตำแหน่งในแท็บที่สัมพันธ์กับองค์ประกอบที่เฉพาะเจาะจง
newRange()RangeBuilderสร้างเครื่องมือสร้างที่ใช้ในการสร้างออบเจ็กต์ Range จากองค์ประกอบแท็บ

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

addBookmark(position)

เพิ่ม Bookmark ที่ Position ที่ระบุ

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();

// Gets the tab body and adds a paragraph.
const paragraph = documentTab.getBody().appendParagraph('My new paragraph.');

// Creates a position at the first character of the paragraph text.
const position = documentTab.newPosition(paragraph.getChild(0), 0);

// Adds a bookmark at the first character of the paragraph text.
const bookmark = documentTab.addBookmark(position);

// Logs the bookmark ID to the console.
console.log(bookmark.getId());

พารามิเตอร์

ชื่อประเภทคำอธิบาย
positionPositionตำแหน่งของบุ๊กมาร์กใหม่

รีเทิร์น

Bookmark — บุ๊กมาร์กใหม่

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

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

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

addFooter()

เพิ่มส่วนท้ายของแท็บ หากยังไม่มี

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();

// Adds a footer to the tab.
const footer = documentTab.addFooter();

// Sets the footer text to 'This is a footer.'
footer.setText('This is a footer');

รีเทิร์น

FooterSection — ส่วนท้ายของแท็บ

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

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

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

addHeader()

เพิ่มส่วนหัวของแท็บ หากไม่มี

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();

// Adds a header to the tab.
const header = documentTab.addHeader();

// Sets the header text to 'This is a header.'
header.setText('This is a header');

รีเทิร์น

HeaderSection — ส่วนหัวของแท็บ

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

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

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

addNamedRange(name, range)

เพิ่ม NamedRange ซึ่งเป็น Range ที่มีชื่อและรหัสสำหรับใช้ ในการดึงข้อมูลในภายหลัง ชื่อไม่จำเป็นต้องไม่ซ้ำกัน แม้ในแท็บต่างๆ ช่วงที่แตกต่างกันหลายช่วงในเอกสารเดียวกันสามารถใช้ชื่อเดียวกันได้ เช่นเดียวกับคลาสใน HTML ในทางตรงกันข้าม รหัสจะ ไม่ซ้ำกันภายในเอกสาร เช่น รหัสใน HTML หลังจากเพิ่ม NamedRange แล้ว คุณจะ แก้ไขไม่ได้ แต่จะนำออกได้เท่านั้น

สคริปต์ใดก็ตามที่เข้าถึงแท็บจะเข้าถึง NamedRange ได้ ลองนำหน้าชื่อช่วงด้วยสตริงที่ไม่ซ้ำกันเพื่อหลีกเลี่ยงข้อขัดแย้งระหว่างสคริปต์โดยไม่ตั้งใจ

// Creates a named range that includes every table in a tab by its ID.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();
const rangeBuilder = documentTab.newRange();
const tables = documentTab.getBody().getTables();
for (let i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
documentTab.addNamedRange('Tab t.0 tables', rangeBuilder.build());

พารามิเตอร์

ชื่อประเภทคำอธิบาย
nameStringชื่อช่วง ซึ่งไม่จำเป็นต้องไม่ซ้ำกัน ชื่อช่วงต้องมีความยาวระหว่าง 1-256 อักขระ
rangeRangeช่วงขององค์ประกอบที่จะเชื่อมโยงกับชื่อ โดยช่วงอาจเป็นผลการค้นหาหรือสร้างขึ้นด้วยตนเองโดยใช้ newRange()

รีเทิร์น

NamedRangeNamedRange

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

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

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

getBody()

เรียกข้อมูล Body ของแท็บ

แท็บอาจมีส่วนประเภทต่างๆ (เช่น HeaderSection, FooterSection) ส่วนที่ใช้งานอยู่ของแท็บคือBody

เมธอดขององค์ประกอบใน DocumentTab จะมอบสิทธิ์ให้กับ Body

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();

// Gets the tab body.
const body = documentTab.getBody();

// Gets the body text and logs it to the console.
console.log(body.getText());

รีเทิร์น

Body — ส่วนเนื้อหาของแท็บ

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

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

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

getBookmark(id)

รับ Bookmark ที่มีรหัสที่ระบุ เมธอดนี้จะแสดงผล null หากไม่มี Bookmark ดังกล่าวในแท็บนี้

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();

// Gets the bookmark by its ID.
const bookmark = documentTab.getBookmark('id.xyz654321');

// If the bookmark exists within the tab, logs the character offset of its
// position to the console. Otherwise, logs 'No bookmark exists with the given
// ID.' to the console.
if (bookmark) {
  console.log(bookmark.getPosition().getOffset());
} else {
  console.log('No bookmark exists with the given ID.');
}

พารามิเตอร์

ชื่อประเภทคำอธิบาย
idStringรหัสสำหรับ Bookmark

รีเทิร์น

Bookmark|nullBookmark ที่มีรหัสที่ระบุ หรือ null หากไม่มี Bookmark ดังกล่าวในแท็บ

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

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

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

getBookmarks()

รับออบเจ็กต์ Bookmark ทั้งหมดในแท็บ

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();

// Gets all of the bookmarks in the tab.
const bookmarks = documentTab.getBookmarks();

// Logs the number of bookmarks in the tab to the console.
console.log(bookmarks.length);

รีเทิร์น

Bookmark[] — อาร์เรย์ของออบเจ็กต์ Bookmark ในแท็บ

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

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

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

getFooter()

ดึงข้อมูลส่วนท้ายของแท็บ หากมี

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();

// Gets the text of the tab's footer and logs it to the console.
console.log(documentTab.getFooter().getText());

รีเทิร์น

FooterSection|null — ส่วนท้ายของแท็บ

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

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

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

getFootnotes()

ดึงข้อมูลองค์ประกอบ Footnote ทั้งหมดในเนื้อหาของแท็บ

การเรียกใช้ getFootnotes จะทำให้เกิดการวนซ้ำในองค์ประกอบของแท็บ สำหรับแท็บขนาดใหญ่ ให้หลีกเลี่ยงการเรียกใช้เมธอดนี้โดยไม่จำเป็น

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();

// Gets the first footnote.
const footnote = documentTab.getFootnotes()[0];

// Logs footnote contents to the console.
console.log(footnote.getFootnoteContents().getText());

รีเทิร์น

Footnote[]|null — เชิงอรรถของแท็บ

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

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

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

getHeader()

ดึงข้อมูลส่วนหัวของแท็บ หากมี

// Opens the Docs file and retrieves the tab by its IDs. If you created your
// script from within a Google Docs file, you can use
// DocumentApp.getActiveDocument().getActiveTab() instead.
// TODO(developer): Replace the IDs with your own.
const documentTab =
    DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();

// Gets the text of the tab's header and logs it to the console.
console.log(documentTab.getHeader().getText());

รีเทิร์น

HeaderSection|null — ส่วนหัวของแท็บ

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

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

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

getNamedRangeById(id)

รับ NamedRange ที่มีรหัสที่ระบุ เมธอดนี้จะแสดงผล null หากไม่มี NamedRangeดังกล่าวในแท็บ ชื่อไม่จำเป็นต้องไม่ซ้ำกัน แม้ในแท็บต่างๆ ช่วงที่แตกต่างกันหลายช่วงในเอกสารเดียวกันอาจใช้ชื่อเดียวกันได้ เช่นเดียวกับคลาสใน HTML ในทางตรงกันข้าม รหัสจะไม่ซ้ำกันภายในแท็บ เช่น รหัสใน HTML

พารามิเตอร์

ชื่อประเภทคำอธิบาย
idStringรหัสของช่วงซึ่งไม่ซ้ำกันภายในแท็บ

รีเทิร์น

NamedRange|nullNamedRange ที่มีรหัสที่ระบุ หรือ null หากไม่มีช่วงดังกล่าวใน แท็บ

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

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

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

getNamedRanges()

รับออบเจ็กต์ NamedRange ทั้งหมดในแท็บ

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

รีเทิร์น

NamedRange[] — อาร์เรย์ของออบเจ็กต์ NamedRange ในแท็บ ซึ่งอาจรวมถึงช่วงหลายช่วงที่มีชื่อเดียวกัน

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

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

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

getNamedRanges(name)

รับออบเจ็กต์ NamedRange ทั้งหมดในแท็บที่มีชื่อที่ระบุ ชื่อไม่จำเป็นต้อง ไม่ซ้ำกัน แม้ในแท็บต่างๆ ก็ตาม ช่วงที่แตกต่างกันหลายช่วงในเอกสารเดียวกันอาจใช้ชื่อเดียวกันได้ เช่นเดียวกับคลาสใน HTML ในทางตรงกันข้าม รหัสจะไม่ซ้ำกันภายในแท็บ เช่น รหัสใน HTML

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
nameStringชื่อช่วง ซึ่งไม่จำเป็นต้องไม่ซ้ำกัน

รีเทิร์น

NamedRange[] — อาร์เรย์ของออบเจ็กต์ NamedRange ในแท็บที่มีชื่อที่ระบุ

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

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

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

newPosition(element, offset)

สร้าง Position ใหม่ ซึ่งเป็นการอ้างอิงถึงตำแหน่งในแท็บที่สัมพันธ์กับองค์ประกอบที่เฉพาะเจาะจง เคอร์เซอร์ของผู้ใช้จะแสดงเป็น Position นอกเหนือจากการใช้งานอื่นๆ

// Append a paragraph, then place the user's cursor after the first word of the
// new paragraph.
// TODO(developer): Replace the IDs with your own.
const doc = DocumentApp.openById('123abc');
const documentTab = doc.getTab('123abc').asDocumentTab();
const paragraph = documentTab.getBody().appendParagraph('My new paragraph.');
const position = documentTab.newPosition(paragraph.getChild(0), 2);
doc.setCursor(position);

พารามิเตอร์

ชื่อประเภทคำอธิบาย
elementElementองค์ประกอบที่มี Position ที่สร้างขึ้นใหม่ ซึ่งต้องเป็นองค์ประกอบ Text หรือองค์ประกอบคอนเทนเนอร์ เช่น Paragraph
offsetIntegerสำหรับองค์ประกอบ Text จำนวนอักขระก่อน Position สำหรับองค์ประกอบอื่นๆ จำนวนองค์ประกอบย่อยก่อน Position ภายใน องค์ประกอบคอนเทนเนอร์เดียวกัน

รีเทิร์น

PositionPosition ใหม่

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

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

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

newRange()

สร้างเครื่องมือสร้างที่ใช้ในการสร้างออบเจ็กต์ Range จากองค์ประกอบแท็บ

// Change the user's selection to a range that includes every table in the tab.
// TODO(developer): Replace the IDs with your own.
const doc = DocumentApp.openById('123abc');
const documentTab = doc.getTab('123abc').asDocumentTab();
const rangeBuilder = documentTab.newRange();
const tables = documentTab.getBody().getTables();
for (let i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
doc.setSelection(rangeBuilder.build());

รีเทิร์น

RangeBuilder - เครื่องมือสร้างใหม่

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

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

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