Class Text

Văn bản

Một phần tử đại diện cho một vùng văn bản đa dạng thức. Mọi văn bản trong Document đều nằm trong phần tử Text. Phần tử Text có thể nằm trong Equation, EquationFunction ListItem hoặc Paragraph, nhưng không được chứa phần tử nào khác. Để biết thêm thông tin về cấu trúc tài liệu, hãy xem hướng dẫn mở rộng Google Tài liệu.

// Gets the body contents of the active tab.
var body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Use editAsText to obtain a single text element containing
// all the characters in the tab.
var text = body.editAsText();

// Insert text at the beginning of the tab.
text.insertText(0, 'Inserted text.\n');

// Insert text at the end of the tab.
text.appendText('\nAppended text.');

// Make the first half of the tab blue.
text.setForegroundColor(0, text.getText().length / 2, '#00FFFF');

Phương thức

Phương thứcLoại dữ liệu trả vềMô tả ngắn
appendText(text)TextThêm văn bản được chỉ định vào cuối vùng văn bản này.
copy()TextTrả về một bản sao sâu, tách biệt của phần tử hiện tại.
deleteText(startOffset, endOffsetInclusive)TextXoá một dải văn bản.
editAsText()TextLấy phiên bản Text của phần tử hiện tại để chỉnh sửa.
findText(searchPattern)RangeElementTìm kiếm nội dung của phần tử cho mẫu văn bản được chỉ định bằng cách sử dụng biểu thức chính quy.
findText(searchPattern, from)RangeElementTìm kiếm nội dung của phần tử cho mẫu văn bản được chỉ định, bắt đầu từ một mẫu văn bản cho trước kết quả tìm kiếm.
getAttributes()ObjectTruy xuất các thuộc tính của phần tử.
getAttributes(offset)ObjectTruy xuất các thuộc tính tại độ lệch ký tự đã chỉ định.
getBackgroundColor()StringTruy xuất chế độ cài đặt màu nền.
getBackgroundColor(offset)StringTruy xuất màu nền tại độ lệch ký tự được chỉ định.
getFontFamily()StringTruy xuất chế độ cài đặt bộ phông chữ.
getFontFamily(offset)StringTruy xuất bộ phông chữ ở độ lệch ký tự được chỉ định.
getFontSize()NumberTruy xuất chế độ cài đặt cỡ chữ.
getFontSize(offset)NumberTruy xuất kích thước phông chữ ở độ lệch ký tự được chỉ định.
getForegroundColor()StringTruy xuất chế độ cài đặt màu nền trước.
getForegroundColor(offset)StringTruy xuất màu nền trước tại độ lệch ký tự được chỉ định.
getLinkUrl()StringTruy xuất URL của đường liên kết.
getLinkUrl(offset)StringTruy xuất URL liên kết tại độ lệch ký tự được chỉ định.
getNextSibling()ElementTruy xuất phần tử đồng cấp tiếp theo của phần tử đó.
getParent()ContainerElementTruy xuất phần tử mẹ của phần tử đó.
getPreviousSibling()ElementTruy xuất phần tử đồng cấp trước đó của phần tử.
getText()StringTruy xuất nội dung của phần tử dưới dạng chuỗi văn bản.
getTextAlignment()TextAlignmentXem cách căn chỉnh văn bản.
getTextAlignment(offset)TextAlignmentXem cách căn chỉnh văn bản cho một ký tự.
getTextAttributeIndices()Integer[]Truy xuất tập hợp chỉ mục văn bản tương ứng với điểm bắt đầu định dạng văn bản riêng biệt chạy.
getType()ElementTypeTruy xuất ElementType của phần tử.
insertText(offset, text)TextChèn văn bản đã chỉ định tại độ lệch ký tự đã cho.
isAtDocumentEnd()BooleanXác định xem phần tử có nằm ở cuối Document hay không.
isBold()BooleanTruy xuất chế độ cài đặt in đậm.
isBold(offset)BooleanTruy xuất chế độ cài đặt in đậm tại độ lệch ký tự đã chỉ định.
isItalic()BooleanTruy xuất chế độ cài đặt in nghiêng.
isItalic(offset)BooleanTruy xuất chế độ cài đặt in nghiêng tại độ lệch ký tự được chỉ định.
isStrikethrough()BooleanTruy xuất chế độ cài đặt gạch ngang chữ.
isStrikethrough(offset)BooleanTruy xuất chế độ cài đặt gạch ngang chữ tại độ lệch ký tự được chỉ định.
isUnderline()BooleanTruy xuất chế độ cài đặt gạch chân.
isUnderline(offset)BooleanTruy xuất chế độ cài đặt gạch chân tại độ lệch ký tự đã chỉ định.
merge()TextHợp nhất phần tử với phần tử đồng cấp trước đó thuộc cùng loại.
removeFromParent()TextXoá phần tử khỏi phần tử mẹ.
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 cho trước bằng một chuỗi thay thế cho trước, sử dụng thông thường biểu thức.
setAttributes(startOffset, endOffsetInclusive, attributes)TextÁp dụng các thuộc tính đã chỉ định cho dải ký tự đã cho.
setAttributes(attributes)TextThiết lập các thuộc tính của phần tử.
setBackgroundColor(startOffset, endOffsetInclusive, color)TextĐặt màu nền cho dải ký tự được chỉ định.
setBackgroundColor(color)TextĐặt màu nền.
setBold(bold)TextĐặt chế độ cài đặt đậm.
setBold(startOffset, endOffsetInclusive, bold)TextĐặt chế độ cài đặt in đậm cho phạm vi ký tự được chỉ định.
setFontFamily(startOffset, endOffsetInclusive, fontFamilyName)TextĐặt bộ phông chữ cho dải ký tự được chỉ định.
setFontFamily(fontFamilyName)TextĐặt bộ phông chữ.
setFontSize(startOffset, endOffsetInclusive, size)TextĐặt kích thước phông chữ cho dải ký tự được chỉ định.
setFontSize(size)TextĐặt kích thước phông chữ.
setForegroundColor(startOffset, endOffsetInclusive, color)TextĐặt màu nền trước cho dải ký tự được chỉ định.
setForegroundColor(color)TextĐặt màu cho nền trước.
setItalic(italic)TextĐặt chế độ cài đặt in nghiêng.
setItalic(startOffset, endOffsetInclusive, italic)TextĐặt chế độ cài đặt in nghiêng cho phạm vi ký tự được chỉ định.
setLinkUrl(startOffset, endOffsetInclusive, url)TextĐặt URL liên kết cho dải ký tự được chỉ định.
setLinkUrl(url)TextĐặt URL của đường liên kết.
setStrikethrough(strikethrough)TextĐặt chế độ cài đặt gạch ngang chữ.
setStrikethrough(startOffset, endOffsetInclusive, strikethrough)TextĐặt chế độ cài đặt gạch ngang chữ cho dải ký tự được chỉ định.
setText(text)TextThiết lập nội dung văn bản.
setTextAlignment(startOffset, endOffsetInclusive, textAlignment)TextĐặt căn chỉnh văn bản cho một dải ký tự nhất định.
setTextAlignment(textAlignment)TextĐặt căn chỉnh văn bản.
setUnderline(underline)TextĐặt chế độ cài đặt gạch chân.
setUnderline(startOffset, endOffsetInclusive, underline)TextĐặt chế độ cài đặt gạch chân cho dải ký tự được chỉ định.

Tài liệu chi tiết

appendText(text)

Thêm văn bản được chỉ định vào cuối vùng văn bản này.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Adds the text, 'Sample body text,' to the end of the tab body.
const text = body.editAsText().appendText('Sample body text');

Tham số

TênLoạiMô tả
textStringVăn bản cần bổ sung.

Cầu thủ trả bóng

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

copy()

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

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

Cầu thủ trả bóng

Text — Bản sao 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

deleteText(startOffset, endOffsetInclusive)

Xoá một dải văn bản.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Deletes the first 10 characters in the body.
 const text = body.editAsText().deleteText(0, 9);

Tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch ký tự của ký tự đầu tiên cần xoá.
endOffsetInclusiveIntegerĐộ lệch ký tự của ký tự cuối cùng cần xoá.

Cầu thủ trả bóng

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

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 để chỉnh sửa nội dung các phần tử ở dạng văn bản đa dạng thức. Chế độ editAsText sẽ 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ử đó.

var 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


findText(searchPattern)

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

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

Mẫu biểu thức chính quy đã cho đượ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.

Tham số

TênLoạiMô tả
searchPatternStringmẫu tìm kiếm

Cầu thủ trả bóng

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

Ủ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

findText(searchPattern, from)

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

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

Mẫu biểu thức chính quy đã cho đượ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.

Tham số

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

Cầu thủ trả bóng

RangeElement – một 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 rỗng nếu không có khớp

Ủ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

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 từng thuộc tính phần tử hợp lệ, trong đó mỗi thuộc tính tên thuộc tính tương ứng với một mục trong bản liệt kê DocumentApp.Attribute.

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.getBody();

// Append a styled paragraph.
var par = body.appendParagraph('A bold, italicized paragraph.');
par.setBold(true);
par.setItalic(true);

// Retrieve the paragraph's attributes.
var atts = par.getAttributes();

// Log the paragraph attributes.
for (var att in atts) {
  Logger.log(att + ":" + atts[att]);
}

Cầu thủ trả bóng

Object – Các thuộc tính của phần 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

getAttributes(offset)

Truy xuất các thuộc tính tại độ lệch ký tự đã chỉ định.

Kết quả là một đối tượng chứa một thuộc tính cho từng thuộc tính văn bản hợp lệ, trong đó mỗi thuộc tính tên thuộc tính tương ứng với một mục trong bản liệt kê DocumentApp.Attribute.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Declares style attributes.
const style = {}
style[DocumentApp.Attribute.BOLD] = true;
style[DocumentApp.Attribute.ITALIC] = true;
style[DocumentApp.Attribute.FONT_SIZE] = 29;

// Sets the style attributes to the tab's body.
const text = body.editAsText();
text.setAttributes(style);

// Gets the style attributes applied to the eleventh character in the
// body and logs them to the console.
const attributes = text.getAttributes(10);
console.log(attributes);

Tham số

TênLoạiMô tả
offsetIntegerĐộ lệch ký tự.

Cầu thủ trả bóng

Object – Các thuộc tính của phần 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

getBackgroundColor()

Truy xuất chế độ cài đặt màu nền.

Cầu thủ trả bóng

String – màu nền, được định dạng trong ký hiệu CSS (như '#ffffff') 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

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

getBackgroundColor(offset)

Truy xuất màu nền tại độ lệch ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456');

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

// Sets the background color of the first 3 characters in the body.
const text = body.editAsText().setBackgroundColor(0, 2, '#FFC0CB');

// Gets the background color of the first character in the body.
const backgroundColor = text.getBackgroundColor(0);

// Logs the background color to the console.
console.log(backgroundColor);

Tham số

TênLoạiMô tả
offsetIntegerĐộ lệch ký tự.

Cầu thủ trả bóng

String – Màu nền, được định dạng trong ký hiệu CSS (như '#ffffff').

Ủ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

getFontFamily()

Truy xuất chế độ cài đặt bộ phông chữ. Tên này có thể là phông chữ bất kỳ trong trình đơn Phông chữ trong Tài liệu hoặc Google Fonts, đồng thời có phân biệt chữ hoa chữ thường. Các phương thức getFontFamily()setFontFamily(fontFamilyName) hiện sử dụng tên chuỗi cho phông chữ thay vì enum FontFamily. Mặc dù enum này là không dùng nữa, nhưng mã vẫn sẽ dùng được để tương thích với các tập lệnh cũ.

Cầu thủ trả bóng

String – bộ phông chữ 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

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

getFontFamily(offset)

Truy xuất bộ phông chữ ở độ lệch ký tự được chỉ định. Tên có thể là bất kỳ phông chữ nào từ Trình đơn phông chữ trong Tài liệu hoặc Google Fonts và phân biệt chữ hoa chữ thường. Các phương thức getFontFamily()setFontFamily(fontFamilyName) hiện hãy sử dụng tên chuỗi cho phông chữ thay vì enum FontFamily. Mặc dù enum này là không dùng nữa, nhưng mã vẫn sẽ dùng được để tương thích với các tập lệnh cũ.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the font of the first 16 characters to Impact.
const text = body.editAsText().setFontFamily(0, 15, 'Impact');

// Gets the font family of the 16th character in the tab body.
const fontFamily = text.getFontFamily(15);

// Logs the font family to the console.
console.log(fontFamily);

Tham số

TênLoạiMô tả
offsetIntegerĐộ lệch ký tự.

Cầu thủ trả bóng

String — Bộ phông chữ.

Ủ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

getFontSize()

Truy xuất chế độ cài đặt cỡ chữ.

Cầu thủ trả bóng

Number – cỡ chữ 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

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

getFontSize(offset)

Truy xuất kích thước phông chữ ở độ lệch ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the font size of the first 13 characters to 15.
const text = body.editAsText().setFontSize(0, 12, 15);

// Gets the font size of the first character.
const fontSize = text.getFontSize(0);

// Logs the font size to the console.
console.log(fontSize);

Tham số

TênLoạiMô tả
offsetIntegerĐộ lệch ký tự.

Cầu thủ trả bóng

Number – Kích thước phông chữ.

Ủ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

getForegroundColor()

Truy xuất chế độ cài đặt màu nền trước.

Cầu thủ trả bóng

String – màu nền trước, được định dạng trong ký hiệu CSS (như '#ffffff') 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

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

getForegroundColor(offset)

Truy xuất màu nền trước tại độ lệch ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the foreground color of the first 3 characters in the tab body.
const text = body.editAsText().setForegroundColor(0, 2, '#0000FF');

// Gets the foreground color of the first character in the tab body.
const foregroundColor = text.getForegroundColor(0);

// Logs the foreground color to the console.
console.log(foregroundcolor);

Tham số

TênLoạiMô tả
offsetIntegerĐộ lệch ký tự.

Cầu thủ trả bóng

String – Màu nền trước, được định dạng bằng ký hiệu CSS (như '#ffffff').

Ủ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

getLinkUrl()

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

Cầu thủ trả bóng

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

Ủ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

getLinkUrl(offset)

Truy xuất URL liên kết tại độ lệch ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Applies a link to the first 10 characters in the body.
const text = body.editAsText().setLinkUrl(0, 9, 'https://www.example.com/');

// Gets the URL of the link from the first character.
const link = text.getLinkUrl(0);

// Logs the link URL to the console.
console.log(link);

Tham số

TênLoạiMô tả
offsetIntegerĐộ lệch ký tự.

Cầu thủ trả bóng

String – URL liên kế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

getNextSibling()

Truy xuất phần tử đồng cấp tiếp theo của phần tử đó.

Phần tử đồng cấp tiếp theo có cùng phần tử mẹ và theo sau phần tử hiện tại.

Cầu thủ trả bóng

Element – Phần tử đồng cấp tiếp theo.

Ủ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

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 – Phần tử mẹ.

Ủ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

getPreviousSibling()

Truy xuất phần tử đồng cấp trước đó của phần tử.

Phần tử đồng cấp 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 – Phần tử đồng cấp trước đó.

Ủ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

getText()

Truy xuất nội dung của phần tử dưới dạng 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

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

getTextAlignment()

Xem cách căn chỉnh văn bản. Các kiểu căn chỉnh có thể dùng là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT.

Cầu thủ trả bóng

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

getTextAlignment(offset)

Xem cách căn chỉnh văn bản cho một ký tự. Các kiểu căn chỉnh có thể dùng là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the text alignment of the tab's body to NORMAL.
const text = body.editAsText().setTextAlignment(DocumentApp.TextAlignment.NORMAL);

// Gets the text alignment of the ninth character.
const alignment = text.getTextAlignment(8);

// Logs the text alignment to the console.
console.log(alignment.toString());

Tham số

TênLoạiMô tả
offsetIntegerĐộ lệch của ký tự.

Cầu thủ trả bóng

TextAlignment – Loại căn chỉnh văn bản hoặc null nếu cách căn chỉnh văn bản chưa từng đượ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

getTextAttributeIndices()

Truy xuất tập hợp chỉ mục văn bản tương ứng với điểm bắt đầu định dạng văn bản riêng biệt chạy.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Gets the text indices at which text formatting changes.
const indices = body.editAsText().getTextAttributeIndices();

// Logs the indices to the console.
console.log(indices.toString());

Cầu thủ trả bóng

Integer[] – Tập hợp các chỉ mục văn bản mà tại đó định dạng văn bản thay đổ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

getType()

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

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

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.getBody();

// Obtain the first element in the active tab's body.

var 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

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

insertText(offset, text)

Chèn văn bản đã chỉ định tại vị trí chênh lệch ký tự đã cho.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Inserts the text, 'Sample inserted text', at the start of the body content.
const text = body.editAsText().insertText(0, 'Sample inserted text');

Tham số

TênLoạiMô tả
offsetIntegerĐộ lệch ký tự cần chèn văn bản.
textStringVăn bản cần chèn.

Cầu thủ trả bóng

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

isAtDocumentEnd()

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

Cầu thủ trả bóng

Boolean – Liệu phần tử này có nằm ở cuối thẻ hay không.

Ủ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

isBold()

Truy xuất chế độ cài đặt in đậm.

Cầu thủ trả bóng

Boolean – liệu văn bản được in đậm hay rỗng nếu phần tử chứa nhiều giá trị cho đoạn mã này phân bổ

Ủ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

isBold(offset)

Truy xuất chế độ cài đặt in đậm tại độ lệch ký tự đã chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Bolds the first 4 characters in the tab body.
const text = body.editAsText().setBold(0, 3, true);

// Gets whether or not the text is bold.
const bold = text.editAsText().isBold(0);

// Logs the text's bold setting to the console
console.log(bold);

Tham số

TênLoạiMô tả
offsetIntegerĐộ lệch ký tự.

Cầu thủ trả bóng

Boolean — Chế độ cài đặt táo bạo.

Ủ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

isItalic()

Truy xuất chế độ cài đặt in nghiêng.

Cầu thủ trả bóng

Boolean – liệu văn bản in nghiêng hay rỗng nếu phần tử chứa nhiều giá trị cho cụm từ này phân bổ

Ủ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

isItalic(offset)

Truy xuất chế độ cài đặt in nghiêng tại độ lệch ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the first 13 characters of the tab body to italic.
const text = body.editAsText().setItalic(0, 12, true);

// Gets whether the fifth character in the tab body is set to
// italic and logs it to the console.
const italic = text.isItalic(4);
console.log(italic);

Tham số

TênLoạiMô tả
offsetIntegerĐộ lệch ký tự.

Cầu thủ trả bóng

Boolean — Chế độ cài đặt in nghiêng.

Ủ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

isStrikethrough()

Truy xuất chế độ cài đặt gạch ngang chữ.

Cầu thủ trả bóng

Boolean – liệu văn bản bị gạch ngang hay rỗng nếu phần tử chứa nhiều giá trị cho thuộc tính này

Ủ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

isStrikethrough(offset)

Truy xuất chế độ cài đặt gạch ngang chữ tại độ lệch ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the first 17 characters of the tab body to strikethrough.
const text = body.editAsText().setStrikethrough(0, 16, true);

// Gets whether the first character in the tab body is set to
// strikethrough and logs it to the console.
const strikethrough = text.isStrikethrough(0);
console.log(strikethrough);

Tham số

TênLoạiMô tả
offsetIntegerĐộ lệch ký tự.

Cầu thủ trả bóng

Boolean — Chế độ cài đặt gạch ngang chữ.

Ủ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

isUnderline()

Truy xuất chế độ cài đặt gạch chân.

Cầu thủ trả bóng

Boolean – liệu văn bản có được gạch chân hay không nếu phần tử chứa nhiều giá trị đối với thuộc tính này

Ủ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

isUnderline(offset)

Truy xuất chế độ cài đặt gạch chân tại độ lệch ký tự đã chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the first 13 characters of the tab body to underline.
const text = body.editAsText().setUnderline(0, 12, false);

// Gets whether the first character in the tab body is set to
// underline and logs it to the console
const underline = text.editAsText().isUnderline(0);
console.log(underline);

Tham số

TênLoạiMô tả
offsetIntegerĐộ lệch ký tự.

Cầu thủ trả bóng

Boolean — Chế độ cài đặt gạch châ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

merge()

Hợp nhất phần tử với phần tử đồng cấp trước đó thuộc cùng loại.

Chỉ có thể hợp nhất các phần tử của cùng một ElementType. Mọi phần tử con có trong phần tử hiện tại sẽ được di chuyển đến phần tử cùng cấp trước đó.

Phần tử hiện tại sẽ bị xoá khỏi tài liệu.

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.getBody();

// Example 1: Merge paragraphs
// Append two paragraphs to the document's active tab.
var par1 = body.appendParagraph('Paragraph 1.');
var par2 = body.appendParagraph('Paragraph 2.');
// Merge the newly added paragraphs into a single paragraph.
par2.merge();

// Example 2: Merge table cells
// Create a two-dimensional array containing the table's cell contents.
var cells = [
['Row 1, Cell 1', 'Row 1, Cell 2'],
['Row 2, Cell 1', 'Row 2, Cell 2']
];
// Build a table from the array.
var table = body.appendTable(cells);
// Get the first row in the table.
 var row = table.getRow(0);
// Get the two cells in this row.
var cell1 = row.getCell(0);
var cell2 = row.getCell(1);
// Merge the current cell into its preceding sibling element.
var merged = cell2.merge();

Cầu thủ trả bóng

Text – Phần tử đã hợp nhấ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

removeFromParent()

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

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab()
var body = documentTab.getBody();

// Remove all images in the active tab's body.
var imgs = body.getImages();
for (var i = 0; i < imgs.length; i++) {
  imgs[i].removeFromParent();
}

Cầu thủ trả bóng

Text – Phần tử đã bị xoá.

Ủ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

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 cho trước bằng một chuỗi thay thế cho trước, sử dụng thông thường biểu thức.

Mẫu tìm kiếm được chuyển dưới dạng 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 khỏi mọi dấu gạch chéo ngược trong mẫu.

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

Mẫu biểu thức chính quy đã cho đượ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.

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

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

Tham số

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

Cầu thủ trả bóng

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

setAttributes(startOffset, endOffsetInclusive, attributes)

Áp dụng các thuộc tính đã chỉ định cho dải ký tự đã cho.

Thông 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 bản liệt kê DocumentApp.Attribute và mỗi giá trị thuộc tính là một giá trị mới áp dụng.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Declares style attributes for font size and font family.
const style = {}
style[DocumentApp.Attribute.FONT_SIZE] = 20 ;
style[DocumentApp.Attribute.FONT_FAMILY] = 'Impact';

// Sets the style attributes to the first 9 characters in the tab's body.
const text = body.setAttributes(0, 8, style);

Tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch đầu của dải ô văn bản.
endOffsetInclusiveIntegerĐộ lệch cuối của dải ô văn bản.
attributesObjectCác thuộc tính của phần tử.

Cầu thủ trả bóng

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

setAttributes(attributes)

Thiết lập các thuộc tính của phần tử.

Thông 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 bản liệt kê DocumentApp.Attribute và mỗi giá trị thuộc tính là một giá trị mới áp dụng.

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.getBody();

// Define a custom paragraph style.
var 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.
var par = body.appendParagraph('A paragraph with custom style.');

// Apply the custom style.
par.setAttributes(style);

Tham số

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

Cầu thủ trả bóng

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

setBackgroundColor(startOffset, endOffsetInclusive, color)

Đặt màu nền cho dải ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the background color of the first 3 characters in the
// tab body to hex color #0000FF.
const text = body.editAsText().setBackgroundColor(0, 2, '#0000FF');

Tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch đầu của dải ô văn bản.
endOffsetInclusiveIntegerĐộ lệch cuối của dải ô văn bản.
colorStringMàu nền, được định dạng bằng ký hiệu CSS (như '#ffffff').

Cầu thủ trả bóng

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

setBackgroundColor(color)

Đặt màu nền.

Tham số

TênLoạiMô tả
colorStringmàu nền, được định dạng bằng ký hiệu CSS (như '#ffffff')

Cầu thủ trả bóng

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

setBold(bold)

Đặt chế độ cài đặt in đậm.

Tham số

TênLoạiMô tả
boldBooleanchế độ cài đặt đậm

Cầu thủ trả bóng

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

setBold(startOffset, endOffsetInclusive, bold)

Đặt chế độ cài đặt in đậm cho phạm vi ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the first 11 characters in the tab's body to bold.
const text = body.editAsText().setBold(0, 10, true);

Tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch đầu của dải ô văn bản.
endOffsetInclusiveIntegerĐộ lệch cuối của dải ô văn bản.
boldBooleanChế độ cài đặt táo bạo.

Cầu thủ trả bóng

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

setFontFamily(startOffset, endOffsetInclusive, fontFamilyName)

Đặt bộ phông chữ cho dải ký tự được chỉ định. Tên có thể là bất kỳ phông chữ nào trong Phông chữ trong Tài liệu hoặc Google Fonts, đồng thời phân biệt chữ hoa chữ thường. Tên phông chữ không được nhận dạng sẽ hiển thị dưới dạng phông chữ tục. Các phương thức getFontFamily(offset)setFontFamily(fontFamilyName) hiện sử dụng tên chuỗi cho phông chữ thay vì enum FontFamily. Mặc dù enum này là không dùng nữa, nhưng mã vẫn sẽ dùng được để tương thích với các tập lệnh cũ.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the font of the first 4 characters in the tab's body to Roboto.
const text = body.editAsText().setFontFamily(0, 3, 'Roboto');

Tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch đầu của dải ô văn bản.
endOffsetInclusiveIntegerĐộ lệch cuối của dải ô văn bản.
fontFamilyNameStringTên của bộ phông chữ, từ trình đơn Phông chữ trong Tài liệu hoặc Google Fonts.

Cầu thủ trả bóng

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

setFontFamily(fontFamilyName)

Đặt bộ phông chữ. Tên này có thể là phông chữ bất kỳ trong trình đơn Phông chữ trong Tài liệu hoặc Google Fonts, đồng thời có phân biệt chữ hoa chữ thường. Phông chữ không nhận dạng được tên sẽ hiển thị dưới dạng WebP. Các phương thức getFontFamily()setFontFamily(fontFamilyName) hiện sử dụng tên chuỗi cho phông chữ thay vì enum FontFamily. Mặc dù enum này là không dùng nữa, nhưng mã vẫn sẽ dùng được để tương thích với các tập lệnh cũ.

Tham số

TênLoạiMô tả
fontFamilyNameStringtên của bộ phông chữ, từ trình đơn Phông chữ trong Tài liệu hoặc Google Fonts

Cầu thủ trả bóng

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

setFontSize(startOffset, endOffsetInclusive, size)

Đặt kích thước phông chữ cho dải ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the size of the first 11 characters in the tab's body to 12.
const text = body.editAsText().setFontSize(0, 10, 12);

Tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch đầu của dải ô văn bản.
endOffsetInclusiveIntegerĐộ lệch cuối của dải ô văn bản.
sizeNumberCỡ chữ.

Cầu thủ trả bóng

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

setFontSize(size)

Đặt kích thước phông chữ.

Tham số

TênLoạiMô tả
sizeNumbercỡ chữ

Cầu thủ trả bóng

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

setForegroundColor(startOffset, endOffsetInclusive, color)

Đặt màu nền trước cho dải ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the foreground color of the first 2 characters in the
// tab's body to hex color #FF0000.
const text = body.editAsText().setForegroundColor(0, 1, '#FF0000');

// Gets the foreground color for the second character in the tab's body.
const foregroundColor = text.getForegroundColor(1);

//  Logs the foreground color to the console.
console.log(foregroundColor);

Tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch đầu của dải ô văn bản.
endOffsetInclusiveIntegerĐộ lệch cuối của dải ô văn bản.
colorStringMàu nền trước, được định dạng bằng ký hiệu CSS (như '#ffffff').

Cầu thủ trả bóng

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

setForegroundColor(color)

Đặt màu cho nền trước.

Tham số

TênLoạiMô tả
colorStringmàu nền trước, được định dạng bằng ký hiệu CSS (như '#ffffff')

Cầu thủ trả bóng

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

setItalic(italic)

Đặt chế độ cài đặt in nghiêng.

Tham số

TênLoạiMô tả
italicBooleanchế độ cài đặt in nghiêng

Cầu thủ trả bóng

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

setItalic(startOffset, endOffsetInclusive, italic)

Đặt chế độ cài đặt in nghiêng cho phạm vi ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the first 11 characters in the tab's body to italic.
const text = body.editAsText().setItalic(0, 10, true);

Tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch đầu của dải ô văn bản.
endOffsetInclusiveIntegerĐộ lệch cuối của dải ô văn bản.
italicBooleanChế độ cài đặt in nghiêng.

Cầu thủ trả bóng

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

setLinkUrl(startOffset, endOffsetInclusive, url)

Đặt URL liên kết cho dải ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Applies a link to the first 11 characters in the body.
const text = body.editAsText().setLinkUrl(0, 10, 'https://example.com');

Tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch đầu của dải ô văn bản.
endOffsetInclusiveIntegerĐộ lệch cuối của dải ô văn bản.
urlStringURL liên kết.

Cầu thủ trả bóng

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

setLinkUrl(url)

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

Tham số

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

Cầu thủ trả bóng

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

setStrikethrough(strikethrough)

Đặt chế độ cài đặt gạch ngang chữ.

Tham số

TênLoạiMô tả
strikethroughBooleanchế độ cài đặt gạch ngang chữ

Cầu thủ trả bóng

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

setStrikethrough(startOffset, endOffsetInclusive, strikethrough)

Đặt chế độ cài đặt gạch ngang chữ cho dải ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the first 11 characters in the tab's body to strikethrough.
const text = body.editAsText().setStrikethrough(0, 10, true);

Tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch đầu của dải ô văn bản.
endOffsetInclusiveIntegerĐộ lệch cuối của dải ô văn bản.
strikethroughBooleanChế độ cài đặt gạch ngang chữ.

Cầu thủ trả bóng

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

setText(text)

Thiết lập nội dung văn bản.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Replaces the contents of the body with the text, 'New body text.'
const text = body.editAsText().setText('New body text.');

Tham số

TênLoạiMô tả
textStringNội dung văn bản mới.

Cầu thủ trả bóng

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

setTextAlignment(startOffset, endOffsetInclusive, textAlignment)

Đặt căn chỉnh văn bản cho một dải ký tự nhất định. Các kiểu căn chỉnh có sẵn là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT.

// Make the first character in the first paragraph of the active tab be superscript.
var documentTab = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab();
var text = documentTab.getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(0, 0, DocumentApp.TextAlignment.SUPERSCRIPT);

Tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch đầu của dải ký tự.
endOffsetInclusiveIntegerĐộ lệch cuối của dải ký tự (bao gồm cả giá trị này).
textAlignmentTextAlignmentLoại căn chỉnh văn bản sẽ áp dụng.

Cầu thủ trả bóng

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

setTextAlignment(textAlignment)

Đặt căn chỉnh văn bản. Các kiểu căn chỉnh hiện có thể dùng là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT.

// Make the entire first paragraph in the active tab be superscript.
var documentTab = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab();
var text = documentTab.getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);

Tham số

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

Cầu thủ trả bóng

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

setUnderline(underline)

Đặt chế độ cài đặt gạch chân.

Tham số

TênLoạiMô tả
underlineBooleanchế độ cài đặt gạch chân

Cầu thủ trả bóng

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

setUnderline(startOffset, endOffsetInclusive, underline)

Đặt chế độ cài đặt gạch chân cho dải ký tự được chỉ định.

// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl('https://docs.google.com/document/d/abc123456/edit');

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

// Sets the first 11 characters in the tab's body to underline.
const text = body.editAsText().setUnderline(0, 10, true);

Tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch đầu của dải ô văn bản.
endOffsetInclusiveIntegerĐộ lệch cuối của dải ô văn bản.
underlineBooleanChế độ cài đặt gạch chân.

Cầu thủ trả bóng

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