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. Tất cả văn bản trong Document đều nằm trong phần tử Text. Phần tử Text có thể chứa trong Equation, EquationFunction, ListItem hoặc Paragraph, nhưng không thể chứa bất kỳ 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 phần hướng dẫn mở rộng Google Tài liệu.

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

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

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

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

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

Phương pháp

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 loạt văn bản.
editAsText()TextNhận 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 đã chỉ định bằ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 đã chỉ định, bắt đầu từ kết quả tìm kiếm nhất định.
getAttributes()ObjectTruy xuất thuộc tính của phần tử.
getAttributes(offset)ObjectTruy xuất các thuộc tính ở độ 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 ở độ 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ự đã chỉ định.
getFontSize()NumberTruy xuất chế độ cài đặt kích thước phông 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 trên nền trước.
getForegroundColor(offset)StringTruy xuất màu nền trước ở độ lệch ký tự đã chỉ định.
getLinkUrl()StringTruy xuất url liên kết.
getLinkUrl(offset)StringTruy xuất URL liên kết ở độ 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ự đơn.
getTextAttributeIndices()Integer[]Truy xuất tập hợp chỉ mục văn bản tương ứng với phần bắt đầu chạy định dạng văn bản riêng biệt.
getType()ElementTypeTruy xuất ElementType của phần tử.
insertText(offset, text)TextChèn văn bản được chỉ định ở độ lệch ký tự cho trước.
isAtDocumentEnd()BooleanXác định xem phần tử này có ở 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 ở độ lệch ký tự đã chỉ định.
isItalic()BooleanTruy xuất chế độ cài đặt in nghiêng.
isItalic(offset)BooleanTruy xuất cài đặt in nghiêng ở độ 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 vị trí bù trừ ký tự được chỉ định.
isUnderline()BooleanTruy xuất chế độ cài đặt dấu gạch dưới.
isUnderline(offset)BooleanTruy xuất chế độ cài đặt gạch dưới ở độ lệch ký tự đã chỉ định.
merge()TextHợp nhất phần tử với anh chị em cùng loại trước đó.
removeFromParent()TextXoá phần tử khỏi thành phần 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 nhất định bằng một chuỗi thay thế nhất định, sử dụng các biểu thức chính quy.
setAttributes(startOffset, endOffsetInclusive, attributes)TextÁp dụng các thuộc tính được chỉ định cho dải ký tự cho trước.
setAttributes(attributes)TextThiết lập 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 in đậm.
setBold(startOffset, endOffsetInclusive, bold)TextĐặt chế độ cài đặt in đậm cho dải ký tự cụ thể.
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 phạm vi ký tự được chỉ định.
setForegroundColor(color)TextĐặt màu 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 dải ký tự xác định.
setLinkUrl(startOffset, endOffsetInclusive, url)TextĐặt URL liên kết cho phạm vi 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 cài đặt gạch ngang chữ cho phạm vi 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ách căn chỉnh văn bản cho một dải ký tự cho trước.
setTextAlignment(textAlignment)TextĐặt cách căn chỉnh văn bản
setUnderline(underline)TextĐặt chế độ cài đặt dấu gạch dưới.
setUnderline(startOffset, endOffsetInclusive, underline)TextĐặt chế độ cài đặt dấu gạch dưới cho phạm vi ký tự 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 document body.
const body = doc.getBody();

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

Các tham số

TênLoạiMô tả
textStringVăn bản cần thêm.

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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ó phần tử mẹ.

Hai chiều

Text – Bản sao mới.

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 loạt 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 document body.
const body = doc.getBody();

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

Các 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á.

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng một hoặc nhiều phạm vi sau:

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

editAsText()

Nhận 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 với 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 chứa đầy đủ trong một phạm vi văn bản đã xóa sẽ bị xóa khỏi phần tử.

var body = DocumentApp.getActiveDocument().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);

Hai chiều

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 đã chỉ định bằng biểu thức chính quy.

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

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

Các tham số

TênLoạiMô tả
searchPatternStringmẫu để tìm

Hai chiều

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ó kết quả nào phù hợp

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 đã chỉ định, bắt đầu từ kết quả tìm kiếm nhất định.

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

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

Các tham số

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

Hai chiều

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ó kết quả nào phù hợp

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 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 bảng liệt kê DocumentApp.Attribute.

var body = DocumentApp.getActiveDocument().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]);
}

Hai chiều

Object — Các 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 yêu cầu uỷ quyền bằng 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 ở độ lệch ký tự đã chỉ định.

Kết quả là một đối tượng chứa một thuộc tính cho mỗi thuộc tính văn bản hợp lệ, trong đó mỗi tên thuộc tính tương ứng với một mục trong bảng 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 document body.
const body = doc.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 document 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);

Các tham số

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

Hai chiều

Object — Các 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 yêu cầu uỷ quyền bằng 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.

Hai chiều

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

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 ở độ 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 document body.
const body = doc.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);

Các tham số

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

Hai chiều

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

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 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à 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 không được dùng nữa, nhưng nó vẫn sẽ có sẵn để tương thích với các tập lệnh cũ.

Hai chiều

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

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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ự đã 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 sử dụng tên chuỗi cho phông chữ thay vì enum FontFamily. Mặc dù enum này không được dùng nữa, nhưng nó vẫn sẽ có sẵn để 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 document body.
const body = doc.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 document body.
const fontFamily = text.getFontFamily(15);

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

Các tham số

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

Hai chiều

String – Bộ phông chữ.

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 kích thước phông chữ.

Hai chiều

Number — kích thước 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

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 document body.
const body = doc.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);

Các tham số

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

Hai chiều

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

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 trên nền trước.

Hai chiều

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

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 ở độ 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 document body.
const body = doc.getBody();

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

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

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

Các tham số

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

Hai chiều

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

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 liên kết.

Hai chiều

String — url 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 yêu cầu uỷ quyền bằng 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 ở độ 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 document body.
const body = doc.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);

Các tham số

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

Hai chiều

String — URL của đường liên kết.

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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ử.

Anh chị em tiếp theo có cùng cha mẹ và tuân theo phần tử hiện tại.

Hai chiều

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

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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.

Hai chiều

ContainerElement – Phần tử mẹ.

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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ử phụ trước có cùng phần tử mẹ và đứng trước phần tử hiện tại.

Hai chiều

Element – Phần tử đồng cấp trước.

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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.

Hai chiều

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 yêu cầu uỷ quyền bằng 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 loại căn chỉnh hiện có là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT.

Hai chiều

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 căn chỉnh văn bản hoặc nếu chưa 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 yêu cầu uỷ quyền bằng 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ự đơn. Các loại căn chỉnh hiện có 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 document body.
const body = doc.getBody();

// Sets the text alignment of the document 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());

Các tham số

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

Hai chiều

TextAlignment – Loại căn chỉnh văn bản, hoặc null nếu bạn chưa từng 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 yêu cầu uỷ quyền bằng 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 phần bắt đầu chạy định dạng văn bản riêng biệt.

// 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 document body.
const body = doc.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());

Hai chiều

Integer[] — Tập hợp các chỉ mục văn bản mà thay đổi về định dạng văn bản.

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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ử.

Hãy dùng getType() để xác định đúng loại của một phần tử nhất định.

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

// Obtain the first element in the document 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.');
}

Hai chiều

ElementType – Loại phần tử.

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 được chỉ định ở độ lệch ký tự cho trướ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 document body.
const body = doc.getBody();

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

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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ử này có ở cuối Document hay không.

Hai chiều

Boolean — Phần tử có ở cuối tài liệu hay không.

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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.

Hai chiều

Boolean – cho dù văn bản có in đậm hay khô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 yêu cầu uỷ quyền bằng 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 ở độ 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 document body.
const body = doc.getBody();

// Bolds the first 4 characters in the document 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);

Các tham số

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

Hai chiều

Boolean — Chế độ cài đặt in đậm.

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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.

Hai chiều

Boolean – cho dù văn bản được in nghiêng hay 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 yêu cầu uỷ quyền bằng 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 cài đặt in nghiêng ở độ 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 document body.
const body = doc.getBody();

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

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

Các tham số

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

Hai chiều

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

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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ữ.

Hai chiều

Boolean – cho dù 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

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 vị trí bù trừ 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 document body.
const body = doc.getBody();

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

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

Các tham số

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

Hai chiều

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

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 dấu gạch dưới.

Hai chiều

Boolean – cho dù văn bản được gạch chân hay 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 yêu cầu uỷ quyền bằng 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 dướ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 document body.
const body = doc.getBody();

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

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

Các tham số

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

Hai chiều

Boolean — Chế độ cài đặt dấu gạch dưới.

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 anh chị em cùng loại trước đó.

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 đều được chuyển sang phần tử phụ trước đó.

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

var body = DocumentApp.getActiveDocument().getBody();
// Example 1: Merge paragraphs
// Append two paragraphs to the document.
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();

Hai chiều

Text – Phần tử được hợp nhất.

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 thành phần mẹ.

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

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

Hai chiều

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

Ủy quyền

Các tập lệnh sử dụng phương thức này yêu cầu uỷ quyền bằng 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 nhất định bằng một chuỗi thay thế nhất định, sử dụng các 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 một đối tượng biểu thức chính quy JavaScript. Do đó, bạn sẽ cần thoát khỏ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, thư viện này giới hạn cú pháp được hỗ trợ.

Mẫu biểu thức chính quy đã cung cấp đượ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().getBody();

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

Các tham số

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

Hai chiều

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 yêu cầu uỷ quyền bằng 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 được chỉ định cho dải ký tự cho trước.

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ảng liệt kê DocumentApp.Attribute và mỗi giá trị thuộc tính là giá trị mới được á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 document body and edits as text.
const body = doc.getBody().editAsText();

// 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 document body.
const text = body.setAttributes(0, 8, style);

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 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ảng liệt kê DocumentApp.Attribute và mỗi giá trị thuộc tính là giá trị mới được áp dụng.

var body = DocumentApp.getActiveDocument().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);

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 document body.
const body = doc.getBody();

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

Các tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch bắt đầu của phạm vi 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 trong ký hiệu CSS (như '#ffffff').

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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.

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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.

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 dải ký tự cụ thể.

// 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 document body.
const body = doc.getBody();

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

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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à phông chữ bất kỳ từ trình đơn Phông chữ trong Tài liệu hoặc Phông chữ của Google và có phân biệt chữ hoa chữ thường. Tên phông chữ không nhận dạng được sẽ hiển thị dưới dạng chú thích. 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 không được dùng nữa, nhưng nó vẫn sẽ có sẵn để 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 body of the document.
const body = doc.getBody();

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

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 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à có phân biệt chữ hoa chữ thường. Tên phông chữ không được công nhận sẽ hiển thị dưới dạng phông chữ phông chữ. 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 không được dùng nữa, nhưng nó vẫn sẽ có sẵn để tương thích với các tập lệnh cũ.

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 document body.
const body = doc.getBody();

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

Các tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch bắt đầu của phạm vi văn bản.
endOffsetInclusiveIntegerĐộ lệch cuối của dải văn bản.
sizeNumberKích thước phông chữ.

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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ữ.

Các tham số

TênLoạiMô tả
sizeNumberkích thước phông chữ

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 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 document body.
const body = doc.getBody();

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

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

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

Các tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch bắt đầu của phạm vi 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 trong ký hiệu CSS (như '#ffffff').

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 nền trước.

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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.

Các tham số

TênLoạiMô tả
italicBooleancài đặt in nghiêng

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 dải ký tự xác đị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 document body.
const body = doc.getBody();

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

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 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 document body.
const body = doc.getBody();

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

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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.

Các tham số

TênLoạiMô tả
urlStringurl liên kết

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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ữ.

Các tham số

TênLoạiMô tả
strikethroughBooleancài đặt gạch ngang chữ

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 cài đặt gạch ngang chữ 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 document body.
const body = doc.getBody();

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

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 document body.
const body = doc.getBody();

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

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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ách căn chỉnh văn bản cho một dải ký tự cho trước. Các loại căn chỉnh có sẵn là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT.

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

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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ách căn chỉnh văn bản Các loại căn chỉnh hiện có là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT.

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

Các tham số

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

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 dấu gạch dưới.

Các tham số

TênLoạiMô tả
underlineBooleancài đặt gạch dưới

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng 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 dấu gạch dưới cho phạm vi 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 document body.
const body = doc.getBody();

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

Các tham số

TênLoạiMô tả
startOffsetIntegerĐộ lệch bắt đầu của phạm vi văn bản.
endOffsetInclusiveIntegerĐộ lệch cuối của dải văn bản.
underlineBooleanCài đặt dấu gạch dưới.

Hai chiều

Text – 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 yêu cầu uỷ quyền bằng một hoặc nhiều phạm vi sau:

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