Một phần tử đại diện cho vùng văn bản đa dạng thức. Tất cả văn bản trong Document đều nằm trong các phần tử Text.
Phần tử Text có thể nằm trong phần tử Equation, EquationFunction, ListItem hoặc Paragraph, nhưng bản thân phần tử này 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 hướng dẫn về cách mở rộng Google Tài liệu.
// Gets the body contents of the active tab. const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Use editAsText to obtain a single text element containing // all the characters in the tab. const 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ức | Kiểu dữ liệu trả về | Mô tả ngắn |
|---|---|---|
append | Text | Thêm văn bản đã chỉ định vào cuối vùng văn bản này. |
copy() | Text | Trả về một bản sao tách rời, sao chép sâu của phần tử hiện tại. |
delete | Text | Xoá một dải văn bản. |
edit | Text | Lấy phiên bản Text của phần tử hiện tại để chỉnh sửa. |
find | Range | Tìm kiếm nội dung của phần tử theo mẫu văn bản đã chỉ định bằng cách sử dụng biểu thức chính quy. |
find | Range | Tìm kiếm nội dung của phần tử theo mẫu văn bản đã chỉ định, bắt đầu từ một kết quả tìm kiếm nhất định. |
get | Object | Truy xuất các thuộc tính của phần tử. |
get | Object | Truy xuất các thuộc tính tại khoảng cách ký tự được chỉ định. |
get | String|null | Truy xuất chế độ cài đặt màu nền. |
get | String|null | Truy xuất màu nền tại khoảng cách ký tự được chỉ định. |
get | String|null | Truy xuất chế độ cài đặt bộ phông chữ. |
get | String|null | Truy xuất bộ phông chữ tại khoảng cách ký tự được chỉ định. |
get | Number|null | Truy xuất chế độ cài đặt cỡ chữ. |
get | Number|null | Truy xuất cỡ chữ tại khoảng cách ký tự được chỉ định. |
get | String|null | Truy xuất chế độ cài đặt màu nền trước. |
get | String|null | Truy xuất màu nền trước tại khoảng cách ký tự được chỉ định. |
get | String|null | Truy xuất URL của đường liên kết. |
get | String|null | Truy xuất URL của đường liên kết tại khoảng cách ký tự được chỉ định. |
get | Element|null | Truy xuất phần tử anh chị em tiếp theo của phần tử. |
get | Container | Truy xuất phần tử mẹ của phần tử. |
get | Element|null | Truy xuất phần tử anh chị em trước đó của phần tử. |
get | String | Truy xuất nội dung của phần tử dưới dạng một chuỗi văn bản. |
get | Text | Lấy chế độ căn chỉnh văn bản. |
get | Text | Lấy chế độ căn chỉnh văn bản cho một ký tự. |
get | 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 của các lần chạy định dạng văn bản riêng biệt. |
get | Element | Truy xuất Element của phần tử. |
insert | Text | Chèn văn bản đã chỉ định tại độ lệch ký tự đã cho. |
is | Boolean | Xác định xem phần tử có ở cuối Document hay không. |
is | Boolean|null | Truy xuất chế độ cài đặt in đậm. |
is | Boolean|null | Truy xuất chế độ cài đặt in đậm tại mức chênh lệch ký tự được chỉ định. |
is | Boolean|null | Truy xuất chế độ cài đặt in nghiêng. |
is | Boolean|null | Truy xuất chế độ cài đặt chữ nghiêng tại khoảng cách ký tự được chỉ định. |
is | Boolean|null | Truy xuất chế độ cài đặt gạch ngang. |
is | Boolean|null | Truy xuất chế độ cài đặt gạch ngang tại khoảng cách ký tự được chỉ định. |
is | Boolean|null | Truy xuất chế độ cài đặt gạch chân. |
is | Boolean|null | Truy xuất chế độ cài đặt gạch chân tại khoảng cách ký tự được chỉ định. |
merge() | Text|null | Hợp nhất phần tử này với phần tử cùng loại đứng trước. |
remove | Text|null | Xoá phần tử khỏi phần tử mẹ. |
replace | Element | Thay thế tất cả các lần xuất hiện của một mẫu văn bản nhất định bằng một chuỗi thay thế nhất định, bằng cách sử dụng biểu thức chính quy. |
set | Text | Áp dụng các thuộc tính đã chỉ định cho dải ký tự đã cho. |
set | Text | Đặt các thuộc tính của phần tử. |
set | Text | Đặt màu nền cho dải ký tự được chỉ định. |
set | Text | Đặt màu nền. |
set | Text | Đặt chế độ cài đặt in đậm. |
set | Text | Đặt chế độ in đậm cho phạm vi ký tự được chỉ định. |
set | Text | Đặt bộ phông chữ cho dải ký tự đã chỉ định. |
set | Text | Đặt bộ phông chữ. |
set | Text | Đặt cỡ chữ cho dải ký tự đã chỉ định. |
set | Text | Đặt cỡ chữ. |
set | Text | Đặt màu nền trước cho dải ký tự đã chỉ định. |
set | Text | Đặt màu nền trước. |
set | Text | Đặt chế độ cài đặt in nghiêng. |
set | Text | Đặt chế độ in nghiêng cho dải ký tự được chỉ định. |
set | Text | Đặt URL đường liên kết cho dải ký tự được chỉ định. |
set | Text | Đặt URL của đường liên kết. |
set | Text | Đặt chế độ gạch ngang. |
set | Text | Đặt chế độ gạch ngang cho phạm vi ký tự được chỉ định. |
set | Text | Đặt nội dung văn bản. |
set | Text | Đặt căn chỉnh văn bản cho một dải ký tự nhất định. |
set | Text | Đặt chế độ căn chỉnh văn bản. |
set | Text | Đặt chế độ gạch chân. |
set | Text | Đặt chế độ 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 đã 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // Adds the text, 'Sample body text,' to the end of the tab body. const text = body.editAsText().appendText('Sample body text');
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
text | String | Văn bản cần nối. |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
copy()
Trả về một bản sao tách rời, sao chép sâu của phần tử hiện tại.
Mọi phần tử con có trong phần tử cũng sẽ được sao chép. Phần tử mới không có phần tử mẹ.
Cầu thủ trả bóng
Text – Bản sao mới.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // Deletes the first 10 characters in the body. const text = body.editAsText().deleteText(0, 9);
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
start | Integer | Độ lệch ký tự của ký tự đầu tiên cần xoá. |
end | Integer | Độ 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
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
editAsText()
Lấy phiên bản Text của phần tử hiện tại để chỉnh sửa.
Sử dụng editAsText để thao tác nội dung của các phần tử dưới dạng văn bản đa dạng thức. Chế độ editAsText bỏ qua các phần tử không phải văn bản (chẳng hạn như InlineImage và HorizontalRule).
Các phần tử con nằm hoàn toàn trong một dải văn bản đã bị xoá sẽ bị xoá khỏi phần tử.
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Insert two paragraphs separated by a paragraph containing an // horizontal rule. body.insertParagraph(0, 'An editAsText sample.'); body.insertHorizontalRule(0); body.insertParagraph(0, 'An example.'); // Delete " sample.\n\n An" removing the horizontal rule in the process. body.editAsText().deleteText(14, 25);
Cầu thủ trả bóng
Text – phiên bản văn bản của phần tử hiện tại
findText(searchPattern)
Tìm kiếm nội dung của phần tử theo mẫu văn bản đã chỉ định bằng cách sử dụng biểu thức chính quy.
Một số tính năng biểu thức chính quy JavaScript không được hỗ trợ đầy đủ, chẳng hạn như nhóm chụp và bộ sửa đổi chế độ.
Mẫu biểu thức chính quy được cung cấp sẽ được so khớp độc lập với từng khối văn bản có trong phần tử hiện tại.
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
search | String | mẫu cần tìm |
Cầu thủ trả bóng
RangeElement|null – kết quả tìm kiếm cho biết vị trí của văn bản tìm kiếm hoặc giá trị rỗng nếu không có kết quả khớp
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
findText(searchPattern, from)
Tìm kiếm nội dung của phần tử theo mẫu văn bản đã chỉ định, bắt đầu từ một kết quả tìm kiếm nhất định.
Một số tính năng biểu thức chính quy JavaScript không được hỗ trợ đầy đủ, chẳng hạn như nhóm chụp và bộ sửa đổi chế độ.
Mẫu biểu thức chính quy được cung cấp sẽ được so khớp độc lập với từng khối văn bản có trong phần tử hiện tại.
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
search | String | mẫu cần tìm |
from | Range | kết quả tìm kiếm để tìm |
Cầu thủ trả bóng
RangeElement|null – kết quả tìm kiếm cho biết vị trí tiếp theo của văn bản tìm kiếm hoặc giá trị rỗng nếu không có kết quả khớp
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getAttributes()
Truy xuất các thuộc tính của phần tử.
Kết quả là một đối tượng chứa một thuộc tính cho mỗi thuộc tính phần tử hợp lệ, trong đó mỗi tên thuộc tính tương ứng với một mục trong phép liệt kê DocumentApp.Attribute.
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Append a styled paragraph. const par = body.appendParagraph('A bold, italicized paragraph.'); par.setBold(true); par.setItalic(true); // Retrieve the paragraph's attributes. const atts = par.getAttributes(); // Log the paragraph attributes. for (const att in atts) { Logger.log(`${att}:${atts[att]}`); }
Cầu thủ trả bóng
Object – Thuộc tính của phần tử.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getAttributes(offset)
Truy xuất các thuộc tính tại khoảng cách ký tự được 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 quá trình 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
offset | Integer | Độ lệch ký tự. |
Cầu thủ trả bóng
Object – Thuộc tính của phần tử.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getBackgroundColor()
Truy xuất chế độ cài đặt màu nền.
Cầu thủ trả bóng
String|null – màu nền, được định dạng theo ký hiệu CSS (chẳng hạn 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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getBackgroundColor(offset)
Truy xuất màu nền tại khoảng cá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/DOCUMENT_ID', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
offset | Integer | Độ lệch ký tự. |
Cầu thủ trả bóng
String|null – Màu nền, được định dạng theo ký hiệu CSS (chẳng hạn như '#ffffff').
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
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 trong trình đơn Phông chữ của 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() và setFontFamily(fontFamilyName) hiện sử dụng tên chuỗi cho phông chữ thay vì enum . Mặc dù enum này không được dùng nữa, nhưng vẫn sẽ có sẵn để tương thích với các tập lệnh cũ.FontFamily
Cầu thủ trả bóng
String|null – họ 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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getFontFamily(offset)
Truy xuất bộ phông chữ tại khoảng cách ký tự được chỉ định. Tên có thể là bất kỳ phông chữ nào trong 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() và setFontFamily(fontFamilyName) hiện dùng tên chuỗi cho phông chữ thay vì enum . Mặc dù enum này không được dùng nữa, nhưng vẫn sẽ có sẵn để tương thích với các tập lệnh cũ.
FontFamily
// 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
offset | Integer | Độ lệch ký tự. |
Cầu thủ trả bóng
String|null – Bộ phông chữ.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getFontSize()
Truy xuất chế độ cài đặt cỡ chữ.
Cầu thủ trả bóng
Number|null – 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
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getFontSize(offset)
Truy xuất cỡ chữ tại khoảng cá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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
offset | Integer | Độ lệch ký tự. |
Cầu thủ trả bóng
Number|null – Cỡ chữ.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getForegroundColor()
Truy xuất chế độ cài đặt màu nền trước.
Cầu thủ trả bóng
String|null – màu nền trước, được định dạng theo ký hiệu CSS (chẳng hạn 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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getForegroundColor(offset)
Truy xuất màu nền trước tại khoảng cá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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
offset | Integer | Độ lệch ký tự. |
Cầu thủ trả bóng
String|null – Màu nền trước, được định dạng theo ký hiệu CSS (chẳng hạn như '#ffffff').
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getLinkUrl()
Truy xuất URL của đường liên kết.
Cầu thủ trả bóng
String|null – URL của đường liên kết hoặc giá trị rỗng nếu phần tử chứa nhiều giá trị cho thuộc tính này
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getLinkUrl(offset)
Truy xuất URL của đường liên kết tại khoảng cá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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
offset | Integer | Độ lệch ký tự. |
Cầu thủ trả bóng
String|null – 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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getNextSibling()
Truy xuất phần tử anh chị em tiếp theo của phần tử.
Phần tử anh chị em tiếp theo có cùng phần tử mẹ và nằm sau phần tử hiện tại.
Cầu thủ trả bóng
Element|null – Phần tử anh chị em tiếp theo.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getParent()
Truy xuất phần tử mẹ của phần tử.
Phần tử gốc chứa phần tử hiện tại.
Cầu thủ trả bóng
ContainerElement|null – Phần tử mẹ.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getPreviousSibling()
Truy xuất phần tử anh chị em trước đó của phần tử.
Phần tử anh chị em trước có cùng phần tử mẹ và đứng trước phần tử hiện tại.
Cầu thủ trả bóng
Element|null – Phần tử anh chị em trước đó.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getText()
Truy xuất nội dung của phần tử dưới dạng một chuỗi văn bản.
Cầu thủ trả bóng
String – nội dung của phần tử dưới dạng chuỗi văn bản
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getTextAlignment()
Lấy chế độ căn chỉnh văn bản. Các loại căn chỉnh có sẵn là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPT và DocumentApp.TextAlignment.SUPERSCRIPT.
Cầu thủ trả bóng
TextAlignment|null – loại căn chỉnh văn bản hoặc null nếu văn bản có nhiều loại căn chỉnh văn bản hoặc nếu bạn chưa bao giờ đặt chế độ căn chỉnh văn bản
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getTextAlignment(offset)
Lấy chế độ căn chỉnh văn bản cho một ký tự. Các loại căn chỉnh có sẵn là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPT và DocumentApp.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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
offset | Integer | Độ lệch của ký tự. |
Cầu thủ trả bóng
TextAlignment|null – Loại căn chỉnh văn bản hoặc null nếu bạn chưa từng đặt chế độ căn chỉnh văn bản.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
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 của các lần 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // Gets the 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 tại đó định dạng văn bản thay đổi.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
getType()
Truy xuất ElementType của phần tử.
Dùng getType() để xác định chính xác loại của một phần tử nhất định.
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Obtain the first element in the active tab's body. const firstChild = body.getChild(0); // Use getType() to determine the element's type. if (firstChild.getType() === DocumentApp.ElementType.PARAGRAPH) { Logger.log('The first element is a paragraph.'); } else { Logger.log('The first element is not a paragraph.'); }
Cầu thủ trả bóng
ElementType – Loại phần tử.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insertText(offset, text)
Chèn văn bản đã chỉ định tại độ 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
offset | Integer | Độ lệch ký tự tại vị trí cần chèn văn bản. |
text | String | Văn bản cần chèn. |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
isAtDocumentEnd()
Xác định xem phần tử có ở cuối Document hay không.
Cầu thủ trả bóng
Boolean – Cho biết phần tử có nằm ở cuối thẻ hay không.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
isBold()
Truy xuất chế độ cài đặt in đậm.
Cầu thủ trả bóng
Boolean|null – văn bản có in đậm hay không, hoặc giá trị rỗng nếu phần tử chứa nhiều giá trị cho thuộc tính này
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
isBold(offset)
Truy xuất chế độ cài đặt in đậm tại mức chênh 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
offset | Integer | Độ lệch ký tự. |
Cầu thủ trả bóng
Boolean|null – Chế độ cài đặt in đậm.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
isItalic()
Truy xuất chế độ cài đặt in nghiêng.
Cầu thủ trả bóng
Boolean|null – văn bản có in nghiêng hay không, hoặc giá trị rỗng nếu phần tử chứa nhiều giá trị cho thuộc tính này
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
isItalic(offset)
Truy xuất chế độ cài đặt chữ nghiêng tại khoảng cá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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
offset | Integer | Độ lệch ký tự. |
Cầu thủ trả bóng
Boolean|null – Chế độ cài đặt chữ nghiêng.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
isStrikethrough()
Truy xuất chế độ cài đặt gạch ngang.
Cầu thủ trả bóng
Boolean|null – văn bản có bị gạch ngang hay không, hoặc giá trị rỗng nếu phần tử chứa nhiều giá trị cho thuộc tính này
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
isStrikethrough(offset)
Truy xuất chế độ cài đặt gạch ngang tại khoảng cá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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
offset | Integer | Độ lệch ký tự. |
Cầu thủ trả bóng
Boolean|null – 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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
isUnderline()
Truy xuất chế độ cài đặt gạch chân.
Cầu thủ trả bóng
Boolean|null – văn bản có được gạch chân hay không, hoặc giá trị rỗng nếu phần tử chứa nhiều giá trị cho thuộc tính này
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
isUnderline(offset)
Truy xuất chế độ cài đặt gạch chân tại khoảng cá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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
offset | Integer | Độ lệch ký tự. |
Cầu thủ trả bóng
Boolean|null – Chế độ cài đặt gạch chân.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
merge()
Hợp nhất phần tử này với phần tử cùng loại đứng trước.
Bạn chỉ có thể hợp nhất các phần tử có cùng ElementType. Mọi phần tử con có trong phần tử hiện tại đều được chuyển đến phần tử anh chị em trước đó.
Phần tử hiện tại sẽ bị xoá khỏi tài liệu.
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Example 1: Merge paragraphs // Append two paragraphs to the document's active tab. const par1 = body.appendParagraph('Paragraph 1.'); const 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. const cells = [ ['Row 1, Cell 1', 'Row 1, Cell 2'], ['Row 2, Cell 1', 'Row 2, Cell 2'], ]; // Build a table from the array. const table = body.appendTable(cells); // Get the first row in the table. const row = table.getRow(0); // Get the two cells in this row. const cell1 = row.getCell(0); const cell2 = row.getCell(1); // Merge the current cell into its preceding sibling element. const merged = cell2.merge();
Cầu thủ trả bóng
Text|null – Phần tử đã hợp nhất.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
removeFromParent()
Xoá phần tử khỏi phần tử mẹ.
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Remove all images in the active tab's body. const imgs = body.getImages(); for (let i = 0; i < imgs.length; i++) { imgs[i].removeFromParent(); }
Cầu thủ trả bóng
Text|null – Phần tử đã bị xoá.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
replaceText(searchPattern, replacement)
Thay thế tất cả các lần xuất hiện của một mẫu văn bản nhất định bằng một chuỗi thay thế nhất định, bằng cách sử dụng biểu thức chính quy.
Mẫu tìm kiếm được truyền dưới dạng một chuỗi, chứ không phải là một đối tượng biểu thức chính quy JavaScript. Do đó, bạn cần thoát mọi dấu gạch chéo ngược trong mẫu.
Phương thức này sử dụng thư viện biểu thức chính quy RE2 của Google, giúp giới hạn cú pháp được hỗ trợ.
Mẫu biểu thức chính quy được cung cấp sẽ được so khớp độc lập với từng khối văn bản có trong phần tử hiện tại.
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Clear the text surrounding "Apps Script", with or without text. body.replaceText('^.*Apps ?Script.*$', 'Apps Script');
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
search | String | mẫu biểu thức chính quy để tìm kiếm |
replacement | String | văn bản dùng để thay thế |
Cầu thủ trả bóng
Element – phần tử hiện tại
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setAttributes(startOffset, endOffsetInclusive, attributes)
Áp dụng các thuộc tính đã chỉ định cho dải ký tự đã cho.
Tham số thuộc tính được chỉ định phải là một đối tượng trong đó mỗi tên thuộc tính là một mục trong phép liệt kê DocumentApp.Attribute và mỗi giá trị thuộc tính là giá trị mới sẽ được áp dụng.
// 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
start | Integer | Mức chênh lệch bắt đầu của dải ô văn bản. |
end | Integer | Độ lệch cuối của dải văn bản. |
attributes | Object | 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
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setAttributes(attributes)
Đặt các thuộc tính của phần tử.
Tham số thuộc tính được chỉ định phải là một đối tượng trong đó mỗi tên thuộc tính là một mục trong phép liệt kê DocumentApp.Attribute và mỗi giá trị thuộc tính là giá trị mới sẽ được áp dụng.
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Define a custom paragraph style. const style = {}; style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.RIGHT; style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri'; style[DocumentApp.Attribute.FONT_SIZE] = 18; style[DocumentApp.Attribute.BOLD] = true; // Append a plain paragraph. const par = body.appendParagraph('A paragraph with custom style.'); // Apply the custom style. par.setAttributes(style);
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
attributes | Object | 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
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
start | Integer | Mức chênh lệch bắt đầu của dải ô văn bản. |
end | Integer | Độ lệch cuối của dải văn bản. |
color | String | Màu nền, được định dạng theo ký hiệu CSS (chẳng hạn như '#ffffff'). |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setBackgroundColor(color)
Đặt màu nền.
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
color | String | màu nền, được định dạng theo ký hiệu CSS (chẳng hạn như '#ffffff') |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setBold(bold)
Đặt chế độ cài đặt in đậm.
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
bold | Boolean | chế độ cài đặt in đậm |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setBold(startOffset, endOffsetInclusive, bold)
Đặt chế độ 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // Sets the first 11 characters in the tab's body to bold. const text = body.editAsText().setBold(0, 10, true);
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
start | Integer | Mức chênh lệch bắt đầu của dải ô văn bản. |
end | Integer | Độ lệch cuối của dải văn bản. |
bold | Boolean | Chế độ cài đặt in đậm. |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setFontFamily(startOffset, endOffsetInclusive, fontFamilyName)
Đặt bộ phông chữ cho dải ký tự đã chỉ định. Tên có thể là bất kỳ phông chữ nào trong trình đơn Phông chữ của Tài liệu hoặc Google Fonts và có phân biệt chữ hoa chữ thường.
Những tên phông chữ không được nhận dạng sẽ hiển thị dưới dạng Arial. Các phương thức getFontFamily(offset) và setFontFamily(fontFamilyName) hiện sử dụng tên chuỗi cho phông chữ thay vì enum . Mặc dù enum này không được dùng nữa, nhưng vẫn sẽ có sẵn để tương thích với các tập lệnh cũ.
FontFamily
// 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
start | Integer | Mức chênh lệch bắt đầu của dải ô văn bản. |
end | Integer | Độ lệch cuối của dải văn bản. |
font | String | Tên của bộ phông chữ, trong trình đơn Phông chữ của 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
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setFontFamily(fontFamilyName)
Đặt bộ phông chữ. Tên có thể là bất kỳ phông chữ nào trong trình đơn Phông chữ của Tài liệu hoặc Google Fonts và phân biệt chữ hoa chữ thường. Các tên phông chữ không được nhận dạng sẽ hiển thị dưới dạng Arial. Các phương thức getFontFamily() và setFontFamily(fontFamilyName) hiện sử dụng tên chuỗi cho phông chữ thay vì enum . Mặc dù enum này không được dùng nữa, nhưng vẫn sẽ có sẵn để tương thích với các tập lệnh cũ.FontFamily
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
font | String | tên của bộ phông chữ, trong trình đơn Phông chữ của 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
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setFontSize(startOffset, endOffsetInclusive, size)
Đặt cỡ chữ cho dải 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
start | Integer | Mức chênh lệch bắt đầu của dải ô văn bản. |
end | Integer | Độ lệch cuối của dải văn bản. |
size | Number | Cỡ chữ. |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setFontSize(size)
setForegroundColor(startOffset, endOffsetInclusive, color)
Đặt màu nền trước cho dải 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // 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ên | Loại | Mô tả |
|---|---|---|
start | Integer | Mức chênh lệch bắt đầu của dải ô văn bản. |
end | Integer | Độ lệch cuối của dải văn bản. |
color | String | Màu nền trước, được định dạng theo ký hiệu CSS (chẳng hạn như '#ffffff'). |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setForegroundColor(color)
Đặt màu nền trước.
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
color | String | màu nền trước, được định dạng theo ký hiệu CSS (chẳng hạn như '#ffffff') |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setItalic(italic)
Đặt chế độ cài đặt in nghiêng.
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
italic | Boolean | chế độ cài đặt in nghiêng |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setItalic(startOffset, endOffsetInclusive, italic)
Đặt chế độ in nghiêng 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // Sets the first 11 characters in the tab's body to italic. const text = body.editAsText().setItalic(0, 10, true);
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
start | Integer | Mức chênh lệch bắt đầu của dải ô văn bản. |
end | Integer | Độ lệch cuối của dải văn bản. |
italic | Boolean | Chế độ cài đặt chữ in nghiêng. |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setLinkUrl(startOffset, endOffsetInclusive, url)
Đặt URL đường 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // Applies a link to the first 11 characters in the body. const text = body.editAsText().setLinkUrl(0, 10, 'https://example.com');
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
start | Integer | Mức chênh lệch bắt đầu của dải ô văn bản. |
end | Integer | Độ lệch cuối của dải văn bản. |
url | String | URL của đường liên kết. |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setLinkUrl(url)
Đặt URL của đường liên kết.
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
url | String | URL của đường liên kết |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setStrikethrough(strikethrough)
Đặt chế độ gạch ngang.
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
strikethrough | Boolean | chế độ cài đặt gạch ngang chữ |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setStrikethrough(startOffset, endOffsetInclusive, strikethrough)
Đặt chế độ gạch ngang 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // Sets the first 11 characters in the tab's body to strikethrough. const text = body.editAsText().setStrikethrough(0, 10, true);
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
start | Integer | Mức chênh lệch bắt đầu của dải ô văn bản. |
end | Integer | Độ lệch cuối của dải văn bản. |
strikethrough | Boolean | Chế độ gạch ngang chữ. |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setText(text)
Đặt 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // Replaces the contents of the body with the text, 'New body text.' const text = body.editAsText().setText('New body text.');
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
text | String | Nộ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
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setTextAlignment(startOffset, endOffsetInclusive, textAlignment)
Đặt căn chỉnh văn bản cho một dải ký tự nhất định. Các loại căn chỉnh có sẵn là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPT và DocumentApp.TextAlignment.SUPERSCRIPT.
// Make the first character in the first paragraph of the active tab be // superscript. const documentTab = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab(); const text = documentTab.getBody().getParagraphs()[0].editAsText(); text.setTextAlignment(0, 0, DocumentApp.TextAlignment.SUPERSCRIPT);
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
start | Integer | Độ lệch bắt đầu của phạm vi ký tự. |
end | Integer | Độ lệch cuối của phạm vi ký tự (bao gồm). |
text | Text | Loại căn chỉnh văn bản cần áp dụng. |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setTextAlignment(textAlignment)
Đặt chế độ căn chỉnh văn bản. Các loại căn chỉnh có sẵn là DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPT và DocumentApp.TextAlignment.SUPERSCRIPT.
// Make the entire first paragraph in the active tab be superscript. const documentTab = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab(); const text = documentTab.getBody().getParagraphs()[0].editAsText(); text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
text | Text | loại căn chỉnh văn bản cần áp dụng |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setUnderline(underline)
Đặt chế độ gạch chân.
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
underline | Boolean | chế độ cài đặt gạch chân |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
setUnderline(startOffset, endOffsetInclusive, underline)
Đặt chế độ 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/DOCUMENT_ID/edit', ); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // Sets the first 11 characters in the tab's body to underline. const text = body.editAsText().setUnderline(0, 10, true);
Tham số
| Tên | Loại | Mô tả |
|---|---|---|
start | Integer | Mức chênh lệch bắt đầu của dải ô văn bản. |
end | Integer | Độ lệch cuối của dải văn bản. |
underline | Boolean | Chế độ cài đặt gạch chân. |
Cầu thủ trả bóng
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 cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents