Class Table

Bảng

Một phần tử đại diện cho một bảng. Table chỉ có thể chứa các phần tử TableRow. Để biết thêm thông tin về cấu trúc tài liệu, hãy xem hướng dẫn về cách mở rộng Google Tài liệu.

Khi tạo một Table có chứa một số lượng lớn hàng hoặc ô, hãy cân nhắc việc tạo Table đó từ một mảng chuỗi, như minh hoạ trong ví dụ sau.

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

// Create a two-dimensional array containing the cell contents.
const cells = [
  ['Row 1, Cell 1', 'Row 1, Cell 2'],
  ['Row 2, Cell 1', 'Row 2, Cell 2'],
];

// Build a table from the array.
body.appendTable(cells);

Phương thức

Phương thứcKiểu dữ liệu trả vềMô tả ngắn
appendTableRow()TableRowTạo và thêm một TableRow mới.
appendTableRow(tableRow)TableRowNối TableRow đã cho.
clear()TableXoá nội dung của phần tử.
copy()TableTrả về một bản sao tách biệt, sao chép sâu của phần tử hiện tại.
editAsText()TextLấy phiên bản Text của phần tử hiện tại để chỉnh sửa.
findElement(elementType)RangeElement|nullTìm kiếm nội dung của phần tử cho một phần tử con thuộc loại được chỉ định.
findElement(elementType, from)RangeElement|nullTìm kiếm nội dung của phần tử cho một phần tử con thuộc loại được chỉ định, bắt đầu từ RangeElement được chỉ định.
findText(searchPattern)RangeElement|nullTìm kiếm nội dung của phần tử theo mẫu văn bản đã chỉ định bằng cách sử dụng biểu thức chính quy.
findText(searchPattern, from)RangeElement|nullTìm kiếm nội dung của phần tử theo mẫu văn bản đã chỉ định, bắt đầu từ một kết quả tìm kiếm nhất định.
getAttributes()ObjectTruy xuất các thuộc tính của phần tử.
getBorderColor()String|nullTruy xuất màu đường viền.
getBorderWidth()Number|nullTruy xuất độ rộng đường viền, tính bằng điểm.
getCell(rowIndex, cellIndex)TableCell|nullTruy xuất TableCell tại chỉ mục hàng và ô được chỉ định.
getChild(childIndex)ElementTruy xuất phần tử con tại chỉ mục con được chỉ định.
getChildIndex(child)IntegerTruy xuất chỉ mục con cho phần tử con được chỉ định.
getColumnWidth(columnIndex)Number|nullTruy xuất chiều rộng của cột bảng được chỉ định, tính bằng điểm.
getLinkUrl()String|nullTruy xuất URL của đường liên kết.
getNextSibling()Element|nullTruy xuất phần tử anh chị em tiếp theo của phần tử.
getNumChildren()IntegerTruy xuất số lượng thành phần con.
getNumRows()IntegerTruy xuất số lượng TableRows.
getParent()ContainerElement|nullTruy xuất phần tử mẹ của phần tử.
getPreviousSibling()Element|nullTruy xuất phần tử anh chị em trước đó của phần tử.
getRow(rowIndex)TableRow|nullTruy xuất TableRow tại chỉ mục hàng được chỉ định.
getText()StringTruy xuất nội dung của phần tử dưới dạng một chuỗi văn bản.
getTextAlignment()TextAlignment|nullLấy chế độ căn chỉnh văn bản.
getType()ElementTypeTruy xuất ElementType của phần tử.
insertTableRow(childIndex)TableRowTạo và chèn một TableRow mới tại chỉ mục đã chỉ định.
insertTableRow(childIndex, tableRow)TableRowChèn TableRow đã cho vào chỉ mục được chỉ định.
isAtDocumentEnd()BooleanXác định xem phần tử có ở cuối Document hay không.
removeChild(child)TableXoá phần tử con đã chỉ định.
removeFromParent()Table|nullXoá phần tử khỏi phần tử mẹ.
removeRow(rowIndex)TableRowXoá TableRow tại chỉ mục hàng đã chỉ định.
replaceText(searchPattern, replacement)ElementThay thế tất cả các lần xuất hiện của một mẫu văn bản nhất định bằng một chuỗi thay thế nhất định, bằng cách sử dụng biểu thức chính quy.
setAttributes(attributes)TableĐặt các thuộc tính của phần tử.
setBorderColor(color)TableĐặt màu đường viền.
setBorderWidth(width)TableĐặt độ rộng đường viền (tính bằng điểm).
setColumnWidth(columnIndex, width)TableĐặt chiều rộng của cột đã chỉ định (tính bằng điểm).
setLinkUrl(url)TableĐặt URL của đường liên kết.
setTextAlignment(textAlignment)TableĐặt chế độ căn chỉnh văn bản.

Tài liệu chi tiết

appendTableRow()

Tạo và thêm một TableRow mới.

Cầu thủ trả bóng

TableRow – phần tử hàng mới của bảng

Ủ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

appendTableRow(tableRow)

Nối TableRow đã cho.

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table in the tab and copies the second row.
const table = body.getTables()[0];
const row = table.getChild(1).copy();

// Adds the copied row to the bottom of the table.
const tableRow = table.appendTableRow(row);

Thông số

TênLoạiMô tả
tableRowTableRowHàng trong bảng cần thêm vào.

Cầu thủ trả bóng

TableRow – Phần tử hàng trong bảng được thêm vào.

Ủ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

clear()

Xoá nội dung của phần tử.

Cầu thủ trả bóng

Table – Phần tử hiện tại.


copy()

Trả về một bản sao tách biệt, sao chép sâu của phần tử hiện tại.

Mọi phần tử con có trong phần tử cũng sẽ được sao chép. Phần tử mới không có phần tử mẹ.

Cầu thủ trả bóng

Table – Bản sao 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

editAsText()

Lấy phiên bản Text của phần tử hiện tại để chỉnh sửa.

Sử dụng editAsText để thao tác nội dung của các phần tử dưới dạng văn bản đa dạng thức. Chế độ editAsText bỏ qua các phần tử không phải văn bản (chẳng hạn như InlineImageHorizontalRule).

Các phần tử con nằm hoàn toàn trong một dải văn bản đã bị xoá sẽ bị xoá khỏi phần tử.

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

Cầu thủ trả bóng

Text – phiên bản văn bản của phần tử hiện tại


findElement(elementType)

Tìm kiếm nội dung của phần tử cho một phần tử con thuộc loại được chỉ định.

Thông số

TênLoạiMô tả
elementTypeElementTypeLoại phần tử cần tìm kiếm.

Cầu thủ trả bóng

RangeElement|null – Kết quả tìm kiếm cho biết vị trí của phần tử tìm kiếm.

Ủ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

findElement(elementType, from)

Tìm kiếm nội dung của phần tử cho một phần tử con thuộc loại được chỉ định, bắt đầu từ RangeElement được chỉ định.

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;
  }
}

Thông số

TênLoạiMô tả
elementTypeElementTypeLoại phần tử cần tìm kiếm.
fromRangeElementKết quả tìm kiếm để tìm kiếm.

Cầu thủ trả bóng

RangeElement|null – Kết quả tìm kiếm cho biết vị trí tiếp theo của phần tử tìm kiếm.

Ủ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

findText(searchPattern)

Tìm kiếm nội dung của phần tử theo mẫu văn bản đã chỉ định bằng cách sử dụng biểu thức chính quy.

Một số tính năng biểu thức chính quy JavaScript không được hỗ trợ đầy đủ, chẳng hạn như nhóm chụp và bộ sửa đổi chế độ.

Mẫu biểu thức chính quy được cung cấp sẽ được so khớp độc lập với từng khối văn bản có trong phần tử hiện tại.

Thông số

TênLoạiMô tả
searchPatternStringmẫu cần tìm

Cầu thủ trả bóng

RangeElement|null – kết quả tìm kiếm cho biết vị trí của văn bản tìm kiếm hoặc giá trị rỗng nếu không có kết quả trùng khớp

Ủ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

findText(searchPattern, from)

Tìm kiếm nội dung của phần tử theo mẫu văn bản đã chỉ định, bắt đầu từ một kết quả tìm kiếm nhất định.

Một số tính năng biểu thức chính quy JavaScript không được hỗ trợ đầy đủ, chẳng hạn như nhóm chụp và bộ sửa đổi chế độ.

Mẫu biểu thức chính quy được cung cấp sẽ được so khớp độc lập với từng khối văn bản có trong phần tử hiện tại.

Thông số

TênLoạiMô tả
searchPatternStringmẫu cần tìm
fromRangeElementkết quả tìm kiếm để tìm

Cầu thủ trả bóng

RangeElement|null – kết quả tìm kiếm cho biết vị trí tiếp theo của văn bản tìm kiếm hoặc giá trị rỗng nếu không có kết quả khớp

Ủ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

getAttributes()

Truy xuất các thuộc tính của phần tử.

Kết quả là một đối tượng chứa một thuộc tính cho mỗi thuộc tính phần tử hợp lệ, trong đó mỗi tên thuộc tính tương ứng với một mục trong phép liệt kê 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]}`);
}

Cầu thủ trả bóng

Object – Thuộc tính của phần tử.

Ủ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

getBorderColor()

Truy xuất màu đường viền.

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Sets the border color of the first table.
table.setBorderColor('#00FF00');

// Logs the border color of the first table to the console.
console.log(table.getBorderColor());

Cầu thủ trả bóng

String|null – Màu đường viền, được định dạng theo ký hiệu CSS (chẳng hạn như '#ffffff').

Ủ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

getBorderWidth()

Truy xuất độ rộng đường viền, tính bằng điểm.

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Sets the border width of the first table.
table.setBorderWidth(20);

// Logs the border width of the first table.
console.log(table.getBorderWidth());

Cầu thủ trả bóng

Number|null – Độ rộng đường viền, tính bằng điểm.

Ủ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

getCell(rowIndex, cellIndex)

Truy xuất TableCell tại chỉ mục hàng và ô được chỉ định.

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Gets the cell of the table's third row and second column.
const cell = table.getCell(2, 1);

// Logs the cell text to the console.
console.log(cell.getText());

Thông số

TênLoạiMô tả
rowIndexIntegerChỉ mục của hàng chứa ô cần truy xuất.
cellIndexIntegerChỉ mục của ô cần truy xuất.

Cầu thủ trả bóng

TableCell|null – Ô trong bảng.

Ủ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

getChild(childIndex)

Truy xuất phần tử con tại chỉ mục con được chỉ định.

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

Thông số

TênLoạiMô tả
childIndexIntegerChỉ mục của phần tử con cần truy xuất.

Cầu thủ trả bóng

Element – Phần tử con tại chỉ mục được chỉ định.

Ủ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

getChildIndex(child)

Truy xuất chỉ mục con cho phần tử con được chỉ định.

Thông số

TênLoạiMô tả
childElementPhần tử con mà bạn muốn truy xuất chỉ mục.

Cầu thủ trả bóng

Integer – Chỉ mục con.

Ủ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

getColumnWidth(columnIndex)

Truy xuất chiều rộng của cột bảng được chỉ định, tính bằng điểm.

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Sets the width of the second column to 100 points.
const columnWidth = table.setColumnWidth(1, 100);

// Gets the width of the second column and logs it to the console.
console.log(columnWidth.getColumnWidth(1));

Thông số

TênLoạiMô tả
columnIndexIntegerChỉ mục cột.

Cầu thủ trả bóng

Number|null – Chiều rộng của cột, tính bằng điểm.

Ủ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

getLinkUrl()

Truy xuất URL của đường liên kết.

Cầu thủ trả bóng

String|null – URL của đường liên kết hoặc giá trị rỗng nếu phần tử chứa nhiều giá trị cho thuộc tính này

Ủ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

getNextSibling()

Truy xuất phần tử anh chị em tiếp theo của phần tử.

Phần tử anh chị em tiếp theo có cùng phần tử mẹ và nằm sau phần tử hiện tại.

Cầu thủ trả bóng

Element|null – Phần tử anh chị em tiếp theo.

Ủ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

getNumChildren()

Truy xuất số lượng thành phần con.

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.`);

Cầu thủ trả bóng

Integer – Số trẻ em.

Ủ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

getNumRows()

Truy xuất số lượng TableRows.

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Logs the number of rows of the first table to the console.
console.log(table.getNumRows());

Cầu thủ trả bóng

Integer — Số hàng trong bảng.

Ủ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

getParent()

Truy xuất phần tử mẹ của phần tử.

Phần tử mẹ chứa phần tử hiện tại.

Cầu thủ trả bóng

ContainerElement|null – Phần tử mẹ.

Ủ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

getPreviousSibling()

Truy xuất phần tử anh chị em trước đó của phần tử.

Phần tử anh chị em trước có cùng phần tử mẹ và đứng trước phần tử hiện tại.

Cầu thủ trả bóng

Element|null – Phần tử anh chị em trước đó.

Ủ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

getRow(rowIndex)

Truy xuất TableRow tại chỉ mục hàng được chỉ định.

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table and logs the text of first row to the console.
const table = body.getTables()[0];
console.log(table.getRow(0).getText());

Thông số

TênLoạiMô tả
rowIndexIntegerChỉ mục của hàng cần truy xuất.

Cầu thủ trả bóng

TableRow|null – Hàng trong bảng.

Ủ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

getText()

Truy xuất nội dung của phần tử dưới dạng một chuỗi văn bản.

Cầu thủ trả bóng

String – nội dung của phần tử dưới dạng chuỗi văn bả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

getTextAlignment()

Lấy chế độ căn chỉnh văn bản. Các loại căn chỉnh có sẵn là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT.

Cầu thủ trả bóng

TextAlignment|null – loại căn chỉnh văn bản hoặc null nếu văn bản có nhiều loại căn chỉnh văn bản hoặc nếu bạn chưa bao giờ đặt chế độ căn chỉnh văn bả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

getType()

Truy xuất ElementType của phần tử.

Dùng getType() để xác định chính xác loại của một phần tử nhất định.

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

Cầu thủ trả bóng

ElementType – Loại phần tử.

Ủ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

insertTableRow(childIndex)

Tạo và chèn một TableRow mới tại chỉ mục đã chỉ định.

Thông số

TênLoạiMô tả
childIndexIntegerchỉ mục mà bạn muốn chèn phần tử

Cầu thủ trả bóng

TableRow – phần tử hiện tạ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

insertTableRow(childIndex, tableRow)

Chèn TableRow đã cho vào chỉ mục được chỉ định.

Thông số

TênLoạiMô tả
childIndexIntegerchỉ mục mà bạn muốn chèn phần tử
tableRowTableRowhàng trong bảng cần chèn

Cầu thủ trả bóng

TableRow – phần tử hàng của bảng được chè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

isAtDocumentEnd()

Xác định xem phần tử có ở cuối Document hay không.

Cầu thủ trả bóng

Boolean – Cho biết phần tử có nằm ở cuối thẻ hay không.

Ủ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

removeChild(child)

Xoá phần tử con đã chỉ định.

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Finds the first table row and removes it.
const element = table.findElement(DocumentApp.ElementType.TABLE_ROW);
table.removeChild(element.getElement());

Thông số

TênLoạiMô tả
childElementPhần tử con cần xoá.

Cầu thủ trả bóng

Table – Phần tử hiện tạ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

removeFromParent()

Xoá phần tử khỏi phần tử mẹ.

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

Cầu thủ trả bóng

Table|null – Phần tử đã bị xoá.

Ủ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

removeRow(rowIndex)

Xoá TableRow tại chỉ mục hàng đã chỉ định.

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table and removes its second row.
const table = body.getTables()[0];
table.removeRow(1);

Thông số

TênLoạiMô tả
rowIndexIntegerChỉ mục của hàng cần xoá.

Cầu thủ trả bóng

TableRow – Hàng đã bị xoá.

Ủ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

replaceText(searchPattern, replacement)

Thay thế tất cả các lần xuất hiện của một mẫu văn bản nhất định bằng một chuỗi thay thế nhất định, bằng cách sử dụng biểu thức chính quy.

Mẫu tìm kiếm được truyền dưới dạng một chuỗi, không phải là đối tượng biểu thức chính quy JavaScript. Do đó, bạn cần thoát mọi dấu gạch chéo ngược trong mẫu.

Phương thức này sử dụng thư viện biểu thức chính quy RE2 của Google, giúp giới hạn cú pháp được hỗ trợ.

Mẫu biểu thức chính quy được cung cấp sẽ được so khớp độc lập với từng khối văn bản có trong phần tử hiện tại.

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

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

Thông số

TênLoạiMô tả
searchPatternStringmẫu biểu thức chính quy để tìm kiếm
replacementStringvăn bản dùng để thay thế

Cầu thủ trả bóng

Element – phần tử hiện tạ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

setAttributes(attributes)

Đặt các thuộc tính của phần tử.

Tham số thuộc tính được chỉ định phải là một đối tượng trong đó mỗi tên thuộc tính là một mục trong phép liệt kê DocumentApp.Attribute và mỗi giá trị thuộc tính là giá trị mới sẽ được áp dụng.

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

Thông số

TênLoạiMô tả
attributesObjectThuộc tính của phần tử.

Cầu thủ trả bóng

Table – Phần tử hiện tạ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

setBorderColor(color)

Đặt màu đường viền.

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Sets the border color of the table to green.
table.setBorderColor('#00FF00');

Thông số

TênLoạiMô tả
colorStringMàu đường viền, được định dạng theo ký hiệu CSS (chẳng hạn như '#ffffff').

Cầu thủ trả bóng

Table – Phần tử hiện tạ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

setBorderWidth(width)

Đặt độ rộng đường viền (tính bằng điểm).

Thông số

TênLoạiMô tả
widthNumberđộ rộng đường viền, tính bằng điểm

Cầu thủ trả bóng

Table – phần tử hiện tạ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

setColumnWidth(columnIndex, width)

Đặt chiều rộng của cột đã chỉ định (tính bằng điểm).

Thông số

TênLoạiMô tả
columnIndexIntegerchỉ mục cột
widthNumberđộ rộng đường viền, tính bằng điểm

Cầu thủ trả bóng

Table – phần tử hiện tạ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

setLinkUrl(url)

Đặt URL của đường liên kết.

Thông số

TênLoạiMô tả
urlStringURL của đường liên kết

Cầu thủ trả bóng

Table – phần tử hiện tạ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

setTextAlignment(textAlignment)

Đặt chế độ căn chỉnh văn bản. Các loại căn chỉnh có sẵn là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPTDocumentApp.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);

Thông số

TênLoạiMô tả
textAlignmentTextAlignmentloại căn chỉnh văn bản cần áp dụng

Cầu thủ trả bóng

Table – phần tử hiện tạ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