องค์ประกอบที่แสดงถึงภูมิภาคของ Rich Text ข้อความทั้งหมดใน Document
จะอยู่ในองค์ประกอบ Text
เอลิเมนต์ Text
อาจอยู่ใน Equation
, EquationFunction
,ListItem
หรือ Paragraph
แต่มีองค์ประกอบอื่นไม่ได้ ดูข้อมูลเพิ่มเติมเกี่ยวกับโครงสร้างเอกสารได้ในคู่มือการขยาย Google เอกสาร
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');
วิธีการ
วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
---|---|---|
appendText(text) | Text | เพิ่มข้อความที่ระบุต่อท้ายเขตข้อความนี้ |
copy() | Text | แสดงผลสําเนาที่แยกออกมาขององค์ประกอบปัจจุบัน |
deleteText(startOffset, endOffsetInclusive) | Text | ลบช่วงของข้อความ |
editAsText() | Text | รับองค์ประกอบปัจจุบันเวอร์ชัน Text สําหรับการแก้ไข |
findText(searchPattern) | RangeElement | ค้นหาเนื้อหาขององค์ประกอบสําหรับรูปแบบข้อความที่ระบุโดยใช้นิพจน์ทั่วไป |
findText(searchPattern, from) | RangeElement | ค้นหาเนื้อหาขององค์ประกอบสําหรับรูปแบบข้อความที่ระบุ โดยเริ่มจากผลการค้นหาที่ระบุ |
getAttributes() | Object | เรียกดูแอตทริบิวต์ขององค์ประกอบ |
getAttributes(offset) | Object | เรียกดูแอตทริบิวต์ในออฟเซ็ตอักขระที่ระบุ |
getBackgroundColor() | String | เรียกข้อมูลการตั้งค่าสีพื้นหลัง |
getBackgroundColor(offset) | String | เรียกข้อมูลสีพื้นหลังที่ออฟเซ็ตอักขระที่ระบุ |
getFontFamily() | String | เรียกดูการตั้งค่าชุดแบบอักษร |
getFontFamily(offset) | String | เรียกดูชุดแบบอักษรในออฟเซ็ตอักขระที่ระบุ |
getFontSize() | Number | เรียกดูการตั้งค่าขนาดแบบอักษร |
getFontSize(offset) | Number | ดึงขนาดแบบอักษรที่ออฟเซ็ตอักขระที่ระบุ |
getForegroundColor() | String | เรียกข้อมูลการตั้งค่าสีเบื้องหน้า |
getForegroundColor(offset) | String | เรียกดูสีเบื้องหน้าตามออฟเซ็ตอักขระที่ระบุ |
getLinkUrl() | String | เรียก URL ของลิงก์ |
getLinkUrl(offset) | String | เรียก URL ของลิงก์ที่ออฟเซ็ตอักขระที่ระบุ |
getNextSibling() | Element | เรียกองค์ประกอบระดับถัดไปขององค์ประกอบนี้ |
getParent() | ContainerElement | เรียกดูองค์ประกอบหลักขององค์ประกอบ |
getPreviousSibling() | Element | เรียกองค์ประกอบระดับก่อนหน้าขององค์ประกอบนี้ |
getText() | String | ดึงเนื้อหาขององค์ประกอบเป็นสตริงข้อความ |
getTextAlignment() | TextAlignment | รับการจัดข้อความ |
getTextAlignment(offset) | TextAlignment | รับการจัดข้อความสําหรับอักขระเดียว |
getTextAttributeIndices() | Integer[] | เรียกชุดดัชนีข้อความที่ตรงกับจุดเริ่มต้นของการจัดรูปแบบข้อความที่แตกต่างกัน |
getType() | ElementType | เรียกดู ElementType ขององค์ประกอบ |
insertText(offset, text) | Text | แทรกข้อความที่ระบุที่ออฟเซ็ตอักขระที่ระบุ |
isAtDocumentEnd() | Boolean | กําหนดว่าองค์ประกอบอยู่ท้าย Document หรือไม่ |
isBold() | Boolean | เรียกการตั้งค่าตัวหนา |
isBold(offset) | Boolean | เรียกข้อมูลการตั้งค่าตัวหนาที่ออฟเซ็ตอักขระที่ระบุ |
isItalic() | Boolean | เรียกการตั้งค่าตัวเอียง |
isItalic(offset) | Boolean | เรียกการตั้งค่าตัวเอียงที่ออฟเซ็ตอักขระที่ระบุ |
isStrikethrough() | Boolean | เรียกดูการตั้งค่าขีดฆ่า |
isStrikethrough(offset) | Boolean | เรียกการตั้งค่าขีดฆ่าที่ออฟเซ็ตอักขระที่ระบุ |
isUnderline() | Boolean | เรียกการตั้งค่าขีดเส้นใต้ |
isUnderline(offset) | Boolean | เรียกการตั้งค่าขีดเส้นใต้ที่ออฟเซ็ตอักขระที่ระบุ |
merge() | Text | ผสานองค์ประกอบกับโดเมนย่อยข้างเคียงที่เป็นประเภทเดียวกัน |
removeFromParent() | Text | นําองค์ประกอบออกจากระดับบนสุด |
replaceText(searchPattern, replacement) | Element | แทนที่รายการรูปแบบข้อความทั้งหมดด้วยสตริงการแทนที่ที่ระบุโดยใช้นิพจน์ปกติ |
setAttributes(startOffset, endOffsetInclusive, attributes) | Text | ใช้แอตทริบิวต์ที่ระบุกับช่วงอักขระที่ระบุ |
setAttributes(attributes) | Text | ตั้งค่าแอตทริบิวต์ขององค์ประกอบ |
setBackgroundColor(startOffset, endOffsetInclusive, color) | Text | ตั้งค่าสีพื้นหลังสําหรับช่วงอักขระที่ระบุ |
setBackgroundColor(color) | Text | ตั้งค่าสีพื้นหลัง |
setBold(bold) | Text | กําหนดการตั้งค่าตัวหนา |
setBold(startOffset, endOffsetInclusive, bold) | Text | กําหนดการตั้งค่าตัวหนาสําหรับช่วงอักขระที่ระบุ |
setFontFamily(startOffset, endOffsetInclusive, fontFamilyName) | Text | ตั้งค่าชุดแบบอักษรสําหรับช่วงอักขระที่ระบุ |
setFontFamily(fontFamilyName) | Text | ตั้งค่าชุดแบบอักษร |
setFontSize(startOffset, endOffsetInclusive, size) | Text | กําหนดขนาดแบบอักษรสําหรับช่วงอักขระที่ระบุ |
setFontSize(size) | Text | กําหนดขนาดแบบอักษร |
setForegroundColor(startOffset, endOffsetInclusive, color) | Text | ตั้งค่าสีเบื้องหน้าสําหรับช่วงอักขระที่ระบุ |
setForegroundColor(color) | Text | ตั้งค่าสีเบื้องหน้า |
setItalic(italic) | Text | ตั้งค่าตัวเอียง |
setItalic(startOffset, endOffsetInclusive, italic) | Text | ตั้งค่าตัวเอียงสําหรับช่วงอักขระที่ระบุ |
setLinkUrl(startOffset, endOffsetInclusive, url) | Text | ตั้งค่า URL ของลิงก์สําหรับช่วงอักขระที่ระบุ |
setLinkUrl(url) | Text | ตั้งค่า URL ของลิงก์ |
setStrikethrough(strikethrough) | Text | ตั้งค่าขีดฆ่า |
setStrikethrough(startOffset, endOffsetInclusive, strikethrough) | Text | ตั้งค่าขีดฆ่าสําหรับช่วงอักขระที่ระบุ |
setText(text) | Text | ตั้งค่าเนื้อหาของข้อความ |
setTextAlignment(startOffset, endOffsetInclusive, textAlignment) | Text | ตั้งค่าการจัดข้อความสําหรับช่วงอักขระที่ระบุ |
setTextAlignment(textAlignment) | Text | ตั้งค่าการจัดข้อความ |
setUnderline(underline) | Text | กําหนดการตั้งค่าขีดเส้นใต้ |
setUnderline(startOffset, endOffsetInclusive, underline) | Text | ตั้งค่าขีดเส้นใต้สําหรับช่วงอักขระที่ระบุ |
เอกสารโดยละเอียด
appendText(text)
เพิ่มข้อความที่ระบุต่อท้ายเขตข้อความนี้
// 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');
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
text | String | ข้อความที่จะต่อท้าย |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
copy()
แสดงผลสําเนาที่แยกออกมาขององค์ประกอบปัจจุบัน
ระบบจะคัดลอกองค์ประกอบย่อยที่อยู่ในองค์ประกอบด้วย องค์ประกอบใหม่ไม่มีองค์ประกอบหลัก
ไปกลับ
Text
— สําเนาใหม่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
deleteText(startOffset, endOffsetInclusive)
ลบช่วงของข้อความ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
startOffset | Integer | ออฟเซ็ตอักขระของอักขระตัวแรกที่จะลบ |
endOffsetInclusive | Integer | การชดเชยอักขระของอักขระตัวสุดท้ายที่จะลบ |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
editAsText()
รับองค์ประกอบปัจจุบันเวอร์ชัน Text
สําหรับการแก้ไข
ใช้ editAsText
สําหรับการบิดเบือนเนื้อหาองค์ประกอบเป็น Rich Text โหมด editAsText
จะไม่สนใจองค์ประกอบที่ไม่ใช่ข้อความ (เช่น InlineImage
และ HorizontalRule
)
ระบบจะนําองค์ประกอบย่อยที่อยู่ในช่วงข้อความที่ถูกลบออกจากองค์ประกอบ
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);
ไปกลับ
Text
— ข้อความขององค์ประกอบปัจจุบันในรูปแบบข้อความ
findText(searchPattern)
ค้นหาเนื้อหาขององค์ประกอบสําหรับรูปแบบข้อความที่ระบุโดยใช้นิพจน์ทั่วไป
ระบบไม่รองรับชุดย่อยของฟีเจอร์นิพจน์ทั่วไปของ JavaScript เช่น แคปเจอร์กรุ๊ปและตัวปรับแต่งโหมด
ระบบจะจับคู่รูปแบบนิพจน์ทั่วไปที่ระบุอย่างอิสระกับบล็อกข้อความแต่ละรายการ ที่มีอยู่ในองค์ประกอบปัจจุบัน
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
searchPattern | String | รูปแบบที่จะค้นหา |
ไปกลับ
RangeElement
- ผลการค้นหาที่ระบุตําแหน่งของข้อความค้นหา หรือไม่มีข้อมูลหากไม่พบรายการที่ตรงกัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findText(searchPattern, from)
ค้นหาเนื้อหาขององค์ประกอบสําหรับรูปแบบข้อความที่ระบุ โดยเริ่มจากผลการค้นหาที่ระบุ
ระบบไม่รองรับชุดย่อยของฟีเจอร์นิพจน์ทั่วไปของ JavaScript เช่น แคปเจอร์กรุ๊ปและตัวปรับแต่งโหมด
ระบบจะจับคู่รูปแบบนิพจน์ทั่วไปที่ระบุอย่างอิสระกับบล็อกข้อความแต่ละรายการ ที่มีอยู่ในองค์ประกอบปัจจุบัน
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
searchPattern | String | รูปแบบที่จะค้นหา |
from | RangeElement | ผลการค้นหาที่จะค้นหา |
ไปกลับ
RangeElement
- ผลการค้นหาที่ระบุตําแหน่งถัดไปของข้อความค้นหา หรือค่าว่างหากไม่มีรายการที่ตรงกัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getAttributes()
เรียกดูแอตทริบิวต์ขององค์ประกอบ
ผลลัพธ์จะเป็นออบเจ็กต์ที่มีพร็อพเพอร์ตี้สําหรับแอตทริบิวต์ขององค์ประกอบที่ถูกต้องแต่ละรายการ โดยชื่อพร็อพเพอร์ตี้แต่ละรายการจะสอดคล้องกับรายการในการแจกแจง 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]); }
ไปกลับ
Object
— แอตทริบิวต์ขององค์ประกอบ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getAttributes(offset)
เรียกดูแอตทริบิวต์ในออฟเซ็ตอักขระที่ระบุ
ผลลัพธ์จะเป็นออบเจ็กต์ที่มีพร็อพเพอร์ตี้สําหรับแอตทริบิวต์ข้อความที่ถูกต้องแต่ละรายการ โดยที่ชื่อพร็อพเพอร์ตี้แต่ละรายการสอดคล้องกับรายการในการแจงนับ 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
ไปกลับ
Object
— แอตทริบิวต์ขององค์ประกอบ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getBackgroundColor()
เรียกข้อมูลการตั้งค่าสีพื้นหลัง
ไปกลับ
String
- สีพื้นหลังที่อยู่ในรูปแบบ CSS (เช่น '#ffffff'
) หรือเป็น Null
หากองค์ประกอบมีค่าหลายค่าสําหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getBackgroundColor(offset)
เรียกข้อมูลสีพื้นหลังที่ออฟเซ็ตอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
ไปกลับ
String
— สีพื้นหลังที่อยู่ในรูปแบบ CSS (เช่น '#ffffff'
)
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getFontFamily()
เรียกดูการตั้งค่าชุดแบบอักษร ชื่อสามารถเป็นแบบอักษรใดก็ได้จากเมนูแบบอักษรในเอกสารหรือแบบอักษรของ Google และคํานึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ ตอนนี้เมธอด getFontFamily()
และ setFontFamily(fontFamilyName)
ใช้ชื่อสตริงสําหรับแบบอักษรแทน Enum ของ
แม้ว่า Enum นี้จะเลิกใช้งานไปแล้ว แต่โค้ดจะยังคงเข้ากันได้กับสคริปต์เก่าFontFamily
ไปกลับ
String
— ชุดแบบอักษรหรือค่า Null หากองค์ประกอบมีค่าหลายค่าสําหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getFontFamily(offset)
เรียกดูชุดแบบอักษรในออฟเซ็ตอักขระที่ระบุ ชื่อสามารถเป็นแบบอักษรใดก็ได้จากเมนู "แบบอักษร" ในเอกสารหรือ Google Fonts และคํานึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ ตอนนี้เมธอด getFontFamily()
และ setFontFamily(fontFamilyName)
ใช้ชื่อสตริงสําหรับแบบอักษรแทน Enum ของ
แม้ว่า Enum นี้จะเลิกใช้งานไปแล้ว แต่โค้ดจะยังคงเข้ากันได้กับสคริปต์เก่า
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/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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
ไปกลับ
String
— ชุดแบบอักษร
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getFontSize()
เรียกดูการตั้งค่าขนาดแบบอักษร
ไปกลับ
Number
- ขนาดแบบอักษร หรือค่าว่างหากองค์ประกอบมีค่าหลายค่าสําหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getFontSize(offset)
ดึงขนาดแบบอักษรที่ออฟเซ็ตอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
ไปกลับ
Number
— ขนาดแบบอักษร
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getForegroundColor()
เรียกข้อมูลการตั้งค่าสีเบื้องหน้า
ไปกลับ
String
- สีเบื้องหน้าซึ่งจัดรูปแบบในรูปแบบ CSS (เช่น '#ffffff'
) หรือเป็น Null
หากองค์ประกอบมีค่าหลายค่าสําหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getForegroundColor(offset)
เรียกดูสีเบื้องหน้าตามออฟเซ็ตอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
ไปกลับ
String
— สีเบื้องหน้าซึ่งอยู่ในรูปแบบสัญลักษณ์ CSS (เช่น '#ffffff'
)
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getLinkUrl()
เรียก URL ของลิงก์
ไปกลับ
String
— URL ของลิงก์ หรือเป็น Null หากองค์ประกอบมีค่าหลายค่าสําหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getLinkUrl(offset)
เรียก URL ของลิงก์ที่ออฟเซ็ตอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
ไปกลับ
String
— URL ของลิงก์
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getNextSibling()
เรียกองค์ประกอบระดับถัดไปขององค์ประกอบนี้
พี่น้องถัดไปมีระดับบนสุดและทําตามองค์ประกอบปัจจุบัน
ไปกลับ
Element
— องค์ประกอบข้างเคียง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getParent()
เรียกดูองค์ประกอบหลักขององค์ประกอบ
องค์ประกอบระดับบนมีองค์ประกอบปัจจุบัน
ไปกลับ
ContainerElement
— องค์ประกอบระดับบน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getPreviousSibling()
เรียกองค์ประกอบระดับก่อนหน้าขององค์ประกอบนี้
พี่น้องคนก่อนหน้ามีผู้เผยแพร่โฆษณาระดับบนสุดเหมือนกันและอยู่หน้าองค์ประกอบปัจจุบัน
ไปกลับ
Element
— องค์ประกอบข้างเคียงก่อนหน้า
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getText()
ดึงเนื้อหาขององค์ประกอบเป็นสตริงข้อความ
ไปกลับ
String
— เนื้อหาขององค์ประกอบเป็นสตริงข้อความ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getTextAlignment()
รับการจัดข้อความ ประเภทการจัดข้อความที่ใช้ได้คือ DocumentApp.TextAlignment.NORMAL
, DocumentApp.TextAlignment.SUBSCRIPT
และ DocumentApp.TextAlignment.SUPERSCRIPT
ไปกลับ
TextAlignment
— ประเภทของการจัดข้อความ หรือ null
หากข้อความมีการจัดข้อความหลายประเภทหรือไม่เคยมีการตั้งค่าการจัดข้อความ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getTextAlignment(offset)
รับการจัดข้อความสําหรับอักขระเดียว ประเภทการจัดข้อความที่ใช้ได้คือ DocumentApp.TextAlignment.NORMAL
, DocumentApp.TextAlignment.SUBSCRIPT
และ 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/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());
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
offset | Integer | ออฟเซ็ตของอักขระ |
ไปกลับ
TextAlignment
— ประเภทการจัดข้อความ หรือ null
หากไม่เคยตั้งค่าการจัดข้อความ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getTextAttributeIndices()
เรียกชุดดัชนีข้อความที่ตรงกับจุดเริ่มต้นของการจัดรูปแบบข้อความที่แตกต่างกัน
// 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());
ไปกลับ
Integer[]
— ชุดดัชนีข้อความที่มีการเปลี่ยนแปลงการจัดรูปแบบข้อความ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getType()
เรียกดู ElementType
ขององค์ประกอบ
ใช้ getType()
เพื่อกําหนดประเภทองค์ประกอบที่กําหนด
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.'); }
ไปกลับ
ElementType
— ประเภทองค์ประกอบ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertText(offset, text)
แทรกข้อความที่ระบุที่ออฟเซ็ตอักขระที่ระบุ
// 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');
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
offset | Integer | ออฟเซ็ตอักขระที่ใช้แทรกข้อความ |
text | String | ข้อความที่จะแทรก |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
isAtDocumentEnd()
isBold()
เรียกการตั้งค่าตัวหนา
ไปกลับ
Boolean
— ไม่ว่าข้อความจะเป็นตัวหนาหรือเป็นค่าว่าง หากองค์ประกอบมีค่าหลายค่าสําหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
isBold(offset)
เรียกข้อมูลการตั้งค่าตัวหนาที่ออฟเซ็ตอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
ไปกลับ
Boolean
— การตั้งค่าตัวหนา
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
isItalic()
เรียกการตั้งค่าตัวเอียง
ไปกลับ
Boolean
- ข้อความเป็นตัวเอียงหรือค่าว่าง หากองค์ประกอบมีค่าหลายค่าสําหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
isItalic(offset)
เรียกการตั้งค่าตัวเอียงที่ออฟเซ็ตอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
ไปกลับ
Boolean
— การตั้งค่าตัวเอียง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
isStrikethrough()
เรียกดูการตั้งค่าขีดฆ่า
ไปกลับ
Boolean
- ขีดทับข้อความหรือค่า Null หากองค์ประกอบมีค่าหลายค่าสําหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
isStrikethrough(offset)
เรียกการตั้งค่าขีดฆ่าที่ออฟเซ็ตอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
ไปกลับ
Boolean
— การตั้งค่าขีดฆ่า
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
isUnderline()
เรียกการตั้งค่าขีดเส้นใต้
ไปกลับ
Boolean
- เช่น ขีดเส้นใต้ข้อความหรือค่าว่าง หากองค์ประกอบมีค่าหลายค่าสําหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
isUnderline(offset)
เรียกการตั้งค่าขีดเส้นใต้ที่ออฟเซ็ตอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
ไปกลับ
Boolean
— การตั้งค่าขีดเส้นใต้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
merge()
ผสานองค์ประกอบกับโดเมนย่อยข้างเคียงที่เป็นประเภทเดียวกัน
ผสานได้เฉพาะองค์ประกอบของ ElementType
เดียวกันเท่านั้น ระบบจะย้ายองค์ประกอบย่อยที่อยู่ในองค์ประกอบปัจจุบันไปยังองค์ประกอบข้างเคียงก่อนหน้า
ระบบจะนําองค์ประกอบปัจจุบันออกจากเอกสาร
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();
ไปกลับ
Text
— องค์ประกอบที่ผสาน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
removeFromParent()
นําองค์ประกอบออกจากระดับบนสุด
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(); }
ไปกลับ
Text
— องค์ประกอบที่นําออก
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
replaceText(searchPattern, replacement)
แทนที่รายการรูปแบบข้อความทั้งหมดด้วยสตริงการแทนที่ที่ระบุโดยใช้นิพจน์ปกติ
รูปแบบการค้นหาจะส่งเป็นสตริง ไม่ใช่ออบเจ็กต์นิพจน์ทั่วไปของ JavaScript ด้วยเหตุนี้ คุณจึงต้องใช้แบ็กสแลชเป็นอักขระหลีกในรูปแบบนี้
วิธีนี้ใช้ไลบรารีนิพจน์ทั่วไป RE2 ของ Google ซึ่งจํากัดไวยากรณ์ที่รองรับ
ระบบจะจับคู่รูปแบบนิพจน์ทั่วไปที่ระบุอย่างอิสระกับบล็อกข้อความแต่ละรายการ ที่มีอยู่ในองค์ประกอบปัจจุบัน
var body = DocumentApp.getActiveDocument().getBody(); // Clear the text surrounding "Apps Script", with or without text. body.replaceText("^.*Apps ?Script.*$", "Apps Script");
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
searchPattern | String | รูปแบบนิพจน์ทั่วไปที่จะค้นหา |
replacement | String | ข้อความที่จะใช้แทนที่ |
ไปกลับ
Element
- องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setAttributes(startOffset, endOffsetInclusive, attributes)
ใช้แอตทริบิวต์ที่ระบุกับช่วงอักขระที่ระบุ
พารามิเตอร์แอตทริบิวต์ที่ระบุต้องเป็นออบเจ็กต์ที่ชื่อพร็อพเพอร์ตี้แต่ละรายการเป็นรายการในการแจงนับ 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 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
startOffset | Integer | ออฟเซ็ตจุดเริ่มต้นของช่วงข้อความ |
endOffsetInclusive | Integer | ออฟเซ็ตช่วงข้อความ |
attributes | Object | แอตทริบิวต์ขององค์ประกอบ |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setAttributes(attributes)
ตั้งค่าแอตทริบิวต์ขององค์ประกอบ
พารามิเตอร์แอตทริบิวต์ที่ระบุต้องเป็นออบเจ็กต์ที่ชื่อพร็อพเพอร์ตี้แต่ละรายการเป็นรายการในการแจงนับ DocumentApp.Attribute
และแต่ละค่าพร็อพเพอร์ตี้คือค่าใหม่ที่จะใช้
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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
attributes | Object | แอตทริบิวต์ขององค์ประกอบ |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setBackgroundColor(startOffset, endOffsetInclusive, color)
ตั้งค่าสีพื้นหลังสําหรับช่วงอักขระที่ระบุ
// 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');
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
startOffset | Integer | ออฟเซ็ตจุดเริ่มต้นของช่วงข้อความ |
endOffsetInclusive | Integer | ออฟเซ็ตช่วงข้อความ |
color | String | สีพื้นหลังที่อยู่ในรูปแบบสัญลักษณ์ CSS (เช่น '#ffffff' ) |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setBackgroundColor(color)
ตั้งค่าสีพื้นหลัง
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
color | String | สีพื้นหลังในรูปแบบสัญลักษณ์ CSS (เช่น '#ffffff' ) |
ไปกลับ
Text
- องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setBold(bold)
setBold(startOffset, endOffsetInclusive, bold)
กําหนดการตั้งค่าตัวหนาสําหรับช่วงอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
startOffset | Integer | ออฟเซ็ตจุดเริ่มต้นของช่วงข้อความ |
endOffsetInclusive | Integer | ออฟเซ็ตช่วงข้อความ |
bold | Boolean | การตั้งค่าตัวหนา |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setFontFamily(startOffset, endOffsetInclusive, fontFamilyName)
ตั้งค่าชุดแบบอักษรสําหรับช่วงอักขระที่ระบุ ชื่อสามารถเป็นแบบอักษรใดก็ได้จากเมนูแบบอักษร
ในเอกสารหรือแบบอักษรของ Google และคํานึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่
ชื่อแบบอักษรที่ไม่รู้จักจะแสดงผลเป็น Arial ตอนนี้เมธอด getFontFamily(offset)
และ
setFontFamily(fontFamilyName)
ใช้ชื่อสตริงสําหรับแบบอักษรแทน Enum ของ
แม้ว่า Enum นี้จะเลิกใช้งานไปแล้ว แต่โค้ดจะยังคงเข้ากันได้กับสคริปต์เก่า
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/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');
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
startOffset | Integer | ออฟเซ็ตจุดเริ่มต้นของช่วงข้อความ |
endOffsetInclusive | Integer | ออฟเซ็ตช่วงข้อความ |
fontFamilyName | String | ชื่อชุดแบบอักษรจากเมนูแบบอักษรในเอกสารหรือ Google Fonts |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setFontFamily(fontFamilyName)
ตั้งค่าชุดแบบอักษร ชื่อสามารถเป็นแบบอักษรใดก็ได้จากเมนูแบบอักษรในเอกสารหรือแบบอักษรของ Google และคํานึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ ชื่อแบบอักษรที่ไม่รู้จักจะแสดงผลเป็น Arial ตอนนี้เมธอด getFontFamily()
และ setFontFamily(fontFamilyName)
ใช้ชื่อสตริงสําหรับแบบอักษรแทน Enum ของ
แม้ว่า Enum นี้จะเลิกใช้งานไปแล้ว แต่โค้ดจะยังคงเข้ากันได้กับสคริปต์เก่าFontFamily
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
fontFamilyName | String | ชื่อของชุดแบบอักษรจากเมนูแบบอักษรในเอกสารหรือ Google Fonts |
ไปกลับ
Text
- องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setFontSize(startOffset, endOffsetInclusive, size)
กําหนดขนาดแบบอักษรสําหรับช่วงอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
startOffset | Integer | ออฟเซ็ตจุดเริ่มต้นของช่วงข้อความ |
endOffsetInclusive | Integer | ออฟเซ็ตช่วงข้อความ |
size | Number | ขนาดแบบอักษร |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setFontSize(size)
setForegroundColor(startOffset, endOffsetInclusive, color)
ตั้งค่าสีเบื้องหน้าสําหรับช่วงอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
startOffset | Integer | ออฟเซ็ตจุดเริ่มต้นของช่วงข้อความ |
endOffsetInclusive | Integer | ออฟเซ็ตช่วงข้อความ |
color | String | สีเบื้องหน้าซึ่งอยู่ในรูปแบบสัญลักษณ์ CSS (เช่น '#ffffff' ) |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setForegroundColor(color)
ตั้งค่าสีเบื้องหน้า
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
color | String | สีเบื้องหน้าซึ่งจัดรูปแบบในรูปแบบ CSS (เช่น '#ffffff' ) |
ไปกลับ
Text
- องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setItalic(italic)
setItalic(startOffset, endOffsetInclusive, italic)
ตั้งค่าตัวเอียงสําหรับช่วงอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
startOffset | Integer | ออฟเซ็ตจุดเริ่มต้นของช่วงข้อความ |
endOffsetInclusive | Integer | ออฟเซ็ตช่วงข้อความ |
italic | Boolean | การตั้งค่าตัวเอียง |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setLinkUrl(startOffset, endOffsetInclusive, url)
ตั้งค่า URL ของลิงก์สําหรับช่วงอักขระที่ระบุ
// 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');
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
startOffset | Integer | ออฟเซ็ตจุดเริ่มต้นของช่วงข้อความ |
endOffsetInclusive | Integer | ออฟเซ็ตช่วงข้อความ |
url | String | URL ของลิงก์ |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setLinkUrl(url)
setStrikethrough(strikethrough)
setStrikethrough(startOffset, endOffsetInclusive, strikethrough)
ตั้งค่าขีดฆ่าสําหรับช่วงอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
startOffset | Integer | ออฟเซ็ตจุดเริ่มต้นของช่วงข้อความ |
endOffsetInclusive | Integer | ออฟเซ็ตช่วงข้อความ |
strikethrough | Boolean | การตั้งค่าขีดฆ่า |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setText(text)
ตั้งค่าเนื้อหาของข้อความ
// 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.');
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
text | String | เนื้อหาข้อความใหม่ |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setTextAlignment(startOffset, endOffsetInclusive, textAlignment)
ตั้งค่าการจัดข้อความสําหรับช่วงอักขระที่ระบุ ประเภทการจัดข้อความที่ใช้ได้คือ
DocumentApp.TextAlignment.NORMAL
, DocumentApp.TextAlignment.SUBSCRIPT
และ
DocumentApp.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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
startOffset | Integer | ออฟเซ็ตจุดเริ่มต้นของช่วงอักขระ |
endOffsetInclusive | Integer | ออฟเซ็ตจุดสิ้นสุดของช่วงอักขระ (รวม) |
textAlignment | TextAlignment | ประเภทของการจัดข้อความที่จะใช้ |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setTextAlignment(textAlignment)
ตั้งค่าการจัดข้อความ ประเภทการจัดข้อความที่ใช้ได้คือ DocumentApp.TextAlignment.NORMAL
, DocumentApp.TextAlignment.SUBSCRIPT
และ DocumentApp.TextAlignment.SUPERSCRIPT
// Make the entire first paragraph be superscript. var text = DocumentApp.getActiveDocument().getBody().getParagraphs()[0].editAsText(); text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
textAlignment | TextAlignment | ประเภทการจัดข้อความที่จะใช้ |
ไปกลับ
Text
- องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setUnderline(underline)
กําหนดการตั้งค่าขีดเส้นใต้
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
underline | Boolean | การตั้งค่าขีดเส้นใต้ |
ไปกลับ
Text
- องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setUnderline(startOffset, endOffsetInclusive, underline)
ตั้งค่าขีดเส้นใต้สําหรับช่วงอักขระที่ระบุ
// 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);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
---|---|---|
startOffset | Integer | ออฟเซ็ตจุดเริ่มต้นของช่วงข้อความ |
endOffsetInclusive | Integer | ออฟเซ็ตช่วงข้อความ |
underline | Boolean | การตั้งค่าขีดเส้นใต้ |
ไปกลับ
Text
— องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents