Class DocumentTab

DocumentTab

แท็บเอกสาร ซึ่งมี Rich Text และองค์ประกอบต่างๆ เช่น ตารางและรายการ

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

// Get a specific document tab based on the tab ID.
var documentTab = DocumentApp.openById('abc123456').getTab('t.0').asDocumentTab();

เมธอด

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

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

addBookmark(position)

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

// Opens the Docs file by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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 by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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 by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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 the tab with tab ID 't.0'.
var documentTab = DocumentApp.openById('abc123456').getTab('t.0').asDocumentTab();
var rangeBuilder = documentTab.newRange();
var tables = documentTab.getBody().getTables();
for (var 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 by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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 by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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

รีเทิร์น

BookmarkBookmark ที่มีรหัสที่ระบุ หรือ null หากไม่มี Bookmark ดังกล่าว อยู่ภายในแท็บนั้น

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

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

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

getBookmarks()

รับออบเจ็กต์ทั้ง Bookmark รายการในแท็บ

// Opens the Docs file by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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 by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').asDocumentTab();

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

รีเทิร์น

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

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

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

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

getFootnotes()

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

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

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

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

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

รีเทิร์น

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

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

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

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

getHeader()

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

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

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

รีเทิร์น

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

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

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

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

getNamedRangeById(id)

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

พารามิเตอร์

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

รีเทิร์น

NamedRangeNamedRange ที่มีรหัสที่ระบุ หรือ 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.
var doc = DocumentApp.openById('abc123456');
var documentTab = doc.getTab('t.0').asDocumentTab();
var paragraph = documentTab.getBody().appendParagraph('My new paragraph.');
var 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.
var doc = DocumentApp.openById('abc123456');
var documentTab = doc.getTab('t.0').asDocumentTab();
var rangeBuilder = documentTab.newRange();
var tables = documentTab.getBody().getTables();
for (var 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