Class DocumentTab

ThẻTài liệu

Một thẻ tài liệu, chứa văn bản đa dạng thức và các phần tử như bảng và danh sách.

Truy xuất một thẻ tài liệu bằng cách sử dụng 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();

Phương thức

Phương thứcKiểu dữ liệu trả vềMô tả ngắn
addBookmark(position)BookmarkThêm Bookmark tại Position đã cho.
addFooter()FooterSectionThêm một phần chân trang cho thẻ nếu chưa có.
addHeader()HeaderSectionThêm một phần tiêu đề thẻ nếu chưa có.
addNamedRange(name, range)NamedRangeThêm NamedRange, là Range có tên và mã nhận dạng để dùng cho việc truy xuất sau này.
getBody()BodyTruy xuất Body của thẻ.
getBookmark(id)Bookmark|nullLấy Bookmark có mã nhận dạng đã cho.
getBookmarks()Bookmark[]Lấy tất cả các đối tượng Bookmark trong thẻ.
getFooter()FooterSection|nullTruy xuất phần chân trang của thẻ (nếu có).
getFootnotes()Footnote[]|nullTruy xuất tất cả các phần tử Footnote trong nội dung của thẻ.
getHeader()HeaderSection|nullTruy xuất phần tiêu đề của thẻ (nếu có).
getNamedRangeById(id)NamedRange|nullLấy NamedRange có mã nhận dạng đã cho.
getNamedRanges()NamedRange[]Lấy tất cả các đối tượng NamedRange trong thẻ.
getNamedRanges(name)NamedRange[]Lấy tất cả các đối tượng NamedRange trong thẻ có tên đã cho.
newPosition(element, offset)PositionTạo một Position mới, là một tham chiếu đến một vị trí trong thẻ, tương ứng với một phần tử cụ thể.
newRange()RangeBuilderTạo một trình tạo dùng để tạo các đối tượng Range từ các phần tử thẻ.

Tài liệu chi tiết

addBookmark(position)

Thêm Bookmark tại Position đã cho.

// 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());

Thông số

TênLoạiMô tả
positionPositionVị trí của dấu trang mới.

Cầu thủ trả bóng

Bookmark – Dấu trang mới.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

addFooter()

Thêm một phần chân trang cho thẻ nếu chưa có.

// 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');

Cầu thủ trả bóng

FooterSection – Chân trang của thẻ.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

addHeader()

Thêm một phần tiêu đề thẻ nếu chưa có.

// 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');

Cầu thủ trả bóng

HeaderSection – Tiêu đề thẻ.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

addNamedRange(name, range)

Thêm NamedRange, là Range có tên và mã nhận dạng để dùng cho việc truy xuất sau này. Tên không nhất thiết phải là duy nhất, ngay cả trên các thẻ; một số dải ô khác nhau trong cùng một tài liệu có thể dùng chung một tên, giống như một lớp trong HTML. Ngược lại, các mã nhận dạng là duy nhất trong tài liệu, chẳng hạn như mã nhận dạng trong HTML. Sau khi thêm NamedRange, bạn không thể sửa đổi mà chỉ có thể xoá NamedRange đó.

Mọi tập lệnh truy cập vào thẻ đều có thể truy cập vào một NamedRange. Để tránh xung đột không mong muốn giữa các tập lệnh, hãy cân nhắc việc thêm tiền tố cho tên dải ô bằng một chuỗi duy nhất.

// 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());

Thông số

TênLoạiMô tả
nameStringTên của dải ô (không cần phải là duy nhất); tên dải ô phải có từ 1 đến 256 ký tự.
rangeRangePhạm vi của các phần tử cần liên kết với tên; phạm vi này có thể là một kết quả tìm kiếm hoặc được tạo theo cách thủ công bằng newRange().

Cầu thủ trả bóng

NamedRangeNamedRange.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

getBody()

Truy xuất Body của thẻ.

Các thẻ có thể chứa nhiều loại phần (ví dụ: HeaderSection, FooterSection). Phần đang hoạt động của một thẻ là Body.

Các phương thức phần tử trong DocumentTab uỷ quyền cho 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());

Cầu thủ trả bóng

Body – Phần nội dung của thẻ.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

getBookmark(id)

Lấy Bookmark có mã nhận dạng đã cho. Phương thức này trả về null nếu không có Bookmark nào như vậy trong thẻ này.

// 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.');
}

Thông số

TênLoạiMô tả
idStringMã nhận dạng của Bookmark.

Cầu thủ trả bóng

Bookmark|nullBookmark có mã nhận dạng đã cho hoặc null nếu không có Bookmark nào như vậy trong thẻ.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

getBookmarks()

Lấy tất cả các đối tượng Bookmark trong thẻ.

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

Cầu thủ trả bóng

Bookmark[] – Một mảng gồm các đối tượng Bookmark trong thẻ.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

getFooter()

Truy xuất phần chân trang của thẻ (nếu có).

// 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());

Cầu thủ trả bóng

FooterSection|null – Chân trang của thẻ.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

getFootnotes()

Truy xuất tất cả các phần tử Footnote trong nội dung của thẻ.

Các lệnh gọi đến getFootnotes sẽ gây ra một lần lặp lại trên các phần tử của thẻ. Đối với các thẻ lớn, hãy tránh gọi phương thức này một cách không cần thiết.

// 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());

Cầu thủ trả bóng

Footnote[]|null – Chú thích cuối trang của thẻ.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

getHeader()

Truy xuất phần tiêu đề của thẻ (nếu có).

// 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());

Cầu thủ trả bóng

HeaderSection|null – Tiêu đề của thẻ.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

getNamedRangeById(id)

Lấy NamedRange có mã nhận dạng đã cho. Phương thức này trả về null nếu không có NamedRange nào như vậy trong thẻ. Tên không nhất thiết phải là duy nhất, ngay cả trên các thẻ; một số dải ô khác nhau trong cùng một tài liệu có thể dùng chung một tên, giống như một lớp trong HTML. Ngược lại, các mã nhận dạng là duy nhất trong thẻ, chẳng hạn như mã nhận dạng trong HTML.

Thông số

TênLoạiMô tả
idStringMã nhận dạng của dải ô, là mã nhận dạng riêng biệt trong thẻ.

Cầu thủ trả bóng

NamedRange|nullNamedRange có mã nhận dạng đã cho hoặc null nếu không có phạm vi nào như vậy trong thẻ.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

getNamedRanges()

Lấy tất cả các đối tượng NamedRange trong thẻ.

Mọi tập lệnh truy cập vào thẻ đều có thể truy cập vào NamedRange. Để tránh xung đột không mong muốn giữa các tập lệnh, hãy cân nhắc thêm tiền tố là một chuỗi duy nhất vào tên dải ô.

Cầu thủ trả bóng

NamedRange[] – Một mảng gồm các đối tượng NamedRange trong thẻ, có thể bao gồm nhiều dải ô có cùng tên.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

getNamedRanges(name)

Lấy tất cả các đối tượng NamedRange trong thẻ có tên đã cho. Tên không nhất thiết phải là duy nhất, ngay cả trên các thẻ; một số dải ô khác nhau trong cùng một tài liệu có thể dùng chung một tên, giống như một lớp trong HTML. Ngược lại, mã nhận dạng là duy nhất trong thẻ, chẳng hạn như mã nhận dạng trong HTML.

Mọi tập lệnh truy cập vào thẻ đều có thể truy cập vào NamedRange. Để tránh xung đột không mong muốn giữa các tập lệnh, hãy cân nhắc thêm tiền tố là một chuỗi duy nhất vào tên dải ô.

Thông số

TênLoạiMô tả
nameStringTên của dải ô (không nhất thiết phải là duy nhất).

Cầu thủ trả bóng

NamedRange[] – Một mảng gồm các đối tượng NamedRange trong thẻ có tên đã cho.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

newPosition(element, offset)

Tạo một Position mới, là một tham chiếu đến một vị trí trong thẻ, tương ứng với một phần tử cụ thể. Con trỏ của người dùng được biểu thị dưới dạng Position, trong số những mục đích sử dụng khác.

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

Thông số

TênLoạiMô tả
elementElementPhần tử chứa Position mới tạo; phần tử này phải là phần tử Text hoặc phần tử vùng chứa như Paragraph.
offsetIntegerĐối với các phần tử Text, số lượng ký tự trước Position; đối với các phần tử khác, số lượng phần tử con trước Position trong cùng một phần tử vùng chứa.

Cầu thủ trả bóng

PositionPosition mới.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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

newRange()

Tạo một trình tạo dùng để tạo các đối tượng Range từ các phần tử thẻ.

// 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());

Cầu thủ trả bóng

RangeBuilder – Trình tạo mới.

Ủy quyền

Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:

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