Class DocumentTab

DocumentTab

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

Truy xuất thẻ tài liệu bằng Document.getTabs()[tabIndex].asDocumentTab().

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

Phương thức

Phương thứcLoại dữ liệu trả vềMô tả ngắn
addBookmark(position)BookmarkThêm Bookmark tại Position đã cho.
addFooter()FooterSectionThêm phần chân trang thẻ, nếu không có.
addHeader()HeaderSectionThêm mục tiêu đề thẻ, nếu không có.
addNamedRange(name, range)NamedRangeThêm NamedRange, là Range có tên và mã nhận dạng để dùng cho truy xuất sau.
getBody()BodyTruy xuất Body của thẻ đó.
getBookmark(id)BookmarkLấy Bookmark với mã nhận dạng đã cho.
getBookmarks()Bookmark[]Lấy mọi đối tượng Bookmark trong thẻ.
getFooter()FooterSectionTruy xuất phần chân trang của thẻ, nếu có.
getFootnotes()Footnote[]Truy xuất tất cả phần tử Footnote trong phần nội dung của thẻ.
getHeader()HeaderSectionTruy xuất phần tiêu đề của thẻ, nếu có.
getNamedRangeById(id)NamedRangeLấy NamedRange với mã nhận dạng đã cho.
getNamedRanges()NamedRange[]Lấy mọi đối tượng NamedRange trong thẻ.
getNamedRanges(name)NamedRange[]Lấy mọi đối tượng NamedRange trong thẻ có tên cho sẵn.
newPosition(element, offset)PositionTạo một Position mới tham chiếu đến một vị trí trong thẻ, so với một phần tử cụ thể.
newRange()RangeBuilderTạo một trình tạo dùng để tạo đối tượng Range qua 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 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());

Tham 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

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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

addFooter()

Thêm phần chân trang thẻ, nếu không có.

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

Cầu thủ trả bóng

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

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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

addHeader()

Thêm mục tiêu đề thẻ, nếu không có.

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

Cầu thủ trả bóng

HeaderSection — Tiêu đề thẻ.

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

  • 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 truy xuất sau. Tên không nhất thiết phải là duy nhất, ngay cả trên các thẻ; một số phạm vi khác nhau trong cùng một tài liệu có thể có cùng tên, giống như một lớp trong HTML. Ngược lại, mã nhận dạng 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 nội dung đó, bạn chỉ có thể xoá nội dung đó.

Bất kỳ tập lệnh nào truy cập vào thẻ đó đều có thể truy cập vào NamedRange. Để tránh trường hợp ngoài ý muốn xung đột giữa các tập lệnh, hãy xem xét việc thêm tiền tố vào tên dải ô bằng một chuỗi duy nhất.

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

Tham số

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

Cầu thủ trả bóng

NamedRangeNamedRange.

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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

getBody()

Truy xuất Body của thẻ đó.

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

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

Cầu thủ trả bóng

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

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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

getBookmark(id)

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

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

Tham số

TênLoạiMô tả
idStringMã của Bookmark.

Cầu thủ trả bóng

BookmarkBookmark với mã nhận dạng đã cho hoặc null nếu không có Bookmark như vậy tồn tại trong thẻ.

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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

getBookmarks()

Lấy mọi đối tượng Bookmark trong thẻ.

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

Cầu thủ trả bóng

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

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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

Cầu thủ trả bóng

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

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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

getFootnotes()

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

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

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

Cầu thủ trả bóng

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

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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

Cầu thủ trả bóng

HeaderSection — Tiêu đề của thẻ.

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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

getNamedRangeById(id)

Lấy NamedRange với mã nhận dạng đã cho. Phương thức này trả về null nếu không Đã tồn tại NamedRange trong thẻ 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 ô trong cùng một tài liệu có thể có cùng 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.

Tham số

TênLoạiMô tả
idStringMã nhận dạng của dải ô là giá trị duy nhất trong thẻ.

Cầu thủ trả bóng

NamedRangeNamedRange với mã nhận dạng đã cho hoặc null nếu không có dải ô như vậy tồn tại trong thẻ.

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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

getNamedRanges()

Lấy mọi đối tượng NamedRange trong thẻ.

Bất kỳ tập lệnh nào truy cập vào thẻ đó đều có thể truy cập NamedRange. Cần 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ố vào tên dải ô bằng một chuỗi 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ó thể bao gồm nhiều đối tượng dải ô có cùng tên.

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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

getNamedRanges(name)

Lấy mọi đối tượng NamedRange trong thẻ có tên cho sẵn. Tên không nhất thiết độc đáo, thậm chí trên các thẻ; một số dải ô khác nhau trong cùng một tài liệu có thể có cùng rất giống 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.

Bất kỳ tập lệnh nào truy cập vào thẻ đó đều có thể truy cập NamedRange. Cần 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ố vào tên dải ô bằng một chuỗi duy nhất.

Tham 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 được đặt.

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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

newPosition(element, offset)

Tạo một Position mới tham chiếu đến một vị trí trong thẻ, so 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, cùng với các 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.
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);

Tham số

TênLoạiMô tả
elementElementPhần tử chứa Position mới tạo để; đâ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ố 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

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

  • 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 đối tượng Range qua các phần tử thẻ.

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

Cầu thủ trả bóng

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

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy quyền với một hoặc nhiều phạm vi sau:

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