องค์ประกอบที่แสดงถึงภูมิภาค Rich Text ข้อความทั้งหมดใน Document จะอยู่ในองค์ประกอบ Text
องค์ประกอบ Text สามารถอยู่ภายใน Equation, Equation, List หรือ Paragraph แต่ไม่สามารถมีองค์ประกอบอื่นได้ ดูข้อมูลเพิ่มเติมเกี่ยวกับโครงสร้างเอกสารได้ที่คำแนะนำในการขยาย Google เอกสาร
// 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');
เมธอด
| วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
|---|---|---|
append | Text | เพิ่มข้อความที่ระบุไปยังส่วนท้ายของภูมิภาคข้อความนี้ |
copy() | Text | แสดงผลสำเนาแบบลึกที่แยกออกมาขององค์ประกอบปัจจุบัน |
delete | Text | ลบข้อความช่วงหนึ่ง |
edit | Text | รับองค์ประกอบปัจจุบันเวอร์ชัน Text เพื่อแก้ไข |
find | Range | ค้นหารูปแบบข้อความที่ระบุในเนื้อหาขององค์ประกอบโดยใช้นิพจน์ทั่วไป |
find | Range | ค้นหารูปแบบข้อความที่ระบุในเนื้อหาขององค์ประกอบ โดยเริ่มจากผลการค้นหาที่ระบุ |
get | Object | ดึงแอตทริบิวต์ขององค์ประกอบ |
get | Object | ดึงข้อมูลแอตทริบิวต์ที่ออฟเซ็ตอักขระที่ระบุ |
get | String|null | เรียกการตั้งค่าสีพื้นหลัง |
get | String|null | เรียกข้อมูลสีพื้นหลังที่ออฟเซ็ตอักขระที่ระบุ |
get | String|null | ดึงการตั้งค่าชุดแบบอักษร |
get | String|null | ดึงข้อมูลชุดแบบอักษรที่ออฟเซ็ตอักขระที่ระบุ |
get | Number|null | ดึงการตั้งค่าขนาดแบบอักษร |
get | Number|null | ดึงข้อมูลขนาดแบบอักษรที่ออฟเซ็ตอักขระที่ระบุ |
get | String|null | ดึงข้อมูลการตั้งค่าสีด้านหน้า |
get | String|null | ดึงสีด้านหน้าในออฟเซ็ตอักขระที่ระบุ |
get | String|null | เรียก URL ของลิงก์ |
get | String|null | ดึง URL ของลิงก์ที่ออฟเซ็ตอักขระที่ระบุ |
get | Element|null | ดึงองค์ประกอบพี่น้องถัดไปขององค์ประกอบ |
get | Container | ดึงองค์ประกอบหลักขององค์ประกอบ |
get | Element|null | ดึงองค์ประกอบพี่น้องก่อนหน้าขององค์ประกอบ |
get | String | ดึงเนื้อหาขององค์ประกอบเป็นสตริงข้อความ |
get | Text | รับการจัดข้อความ |
get | Text | รับการจัดข้อความสำหรับอักขระเดียว |
get | Integer[] | ดึงข้อมูลชุดดัชนีข้อความที่สอดคล้องกับการเริ่มต้นของ การเรียกใช้การจัดรูปแบบข้อความที่แตกต่างกัน |
get | Element | ดึง Element ขององค์ประกอบ |
insert | Text | แทรกข้อความที่ระบุที่ออฟเซ็ตอักขระที่กำหนด |
is | Boolean | กำหนดว่าองค์ประกอบอยู่ท้าย Document หรือไม่ |
is | Boolean|null | เรียกการตั้งค่าตัวหนา |
is | Boolean|null | ดึงการตั้งค่าตัวหนาที่ออฟเซ็ตอักขระที่ระบุ |
is | Boolean|null | เรียกการตั้งค่าตัวเอียง |
is | Boolean|null | ดึงการตั้งค่าตัวเอียงที่ออฟเซ็ตอักขระที่ระบุ |
is | Boolean|null | เรียกการตั้งค่าขีดทับ |
is | Boolean|null | ดึงการตั้งค่าขีดทับที่ออฟเซ็ตอักขระที่ระบุ |
is | Boolean|null | เรียกการตั้งค่าขีดเส้นใต้ |
is | Boolean|null | ดึงการตั้งค่าขีดเส้นใต้ที่ออฟเซ็ตอักขระที่ระบุ |
merge() | Text|null | ผสานองค์ประกอบกับองค์ประกอบที่อยู่ก่อนหน้าซึ่งเป็นประเภทเดียวกัน |
remove | Text|null | นำองค์ประกอบออกจากองค์ประกอบหลัก |
replace | Element | แทนที่รูปแบบข้อความที่กำหนดทั้งหมดด้วยสตริงทดแทนที่กำหนดโดยใช้นิพจน์ทั่วไป |
set | Text | ใช้แอตทริบิวต์ที่ระบุกับช่วงอักขระที่กำหนด |
set | Text | ตั้งค่าแอตทริบิวต์ขององค์ประกอบ |
set | Text | ตั้งค่าสีพื้นหลังสำหรับช่วงอักขระที่ระบุ |
set | Text | ตั้งค่าสีพื้นหลัง |
set | Text | ตั้งค่าตัวหนา |
set | Text | ตั้งค่าตัวหนาสําหรับช่วงอักขระที่ระบุ |
set | Text | กำหนดชุดแบบอักษรสำหรับช่วงอักขระที่ระบุ |
set | Text | ตั้งค่าชุดแบบอักษร |
set | Text | กำหนดขนาดแบบอักษรให้กับช่วงอักขระที่ระบุ |
set | Text | กำหนดขนาดแบบอักษร |
set | Text | กำหนดสีพื้นหน้าสำหรับช่วงอักขระที่ระบุ |
set | Text | ตั้งค่าสีด้านหน้า |
set | Text | ตั้งค่าตัวเอียง |
set | Text | ตั้งค่าตัวเอียงสำหรับช่วงอักขระที่ระบุ |
set | Text | ตั้งค่า URL ของลิงก์สำหรับช่วงอักขระที่ระบุ |
set | Text | ตั้งค่า URL ของลิงก์ |
set | Text | กำหนดการตั้งค่าขีดทับ |
set | Text | ตั้งค่าขีดทับสำหรับช่วงอักขระที่ระบุ |
set | Text | ตั้งค่าเนื้อหาข้อความ |
set | Text | ตั้งค่าการจัดแนวข้อความสำหรับช่วงอักขระที่กำหนด |
set | Text | ตั้งค่าการจัดข้อความ |
set | Text | ตั้งค่าขีดเส้นใต้ |
set | Text | ตั้งค่าขีดเส้นใต้สำหรับช่วงอักขระที่ระบุ |
เอกสารโดยละเอียด
append Text(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/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');
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
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
delete Text(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
start | Integer | ออฟเซ็ตอักขระของอักขระแรกที่จะลบ |
end | Integer | ออฟเซ็ตอักขระของอักขระสุดท้ายที่จะลบ |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
edit As Text()
รับองค์ประกอบปัจจุบันเวอร์ชัน Text เพื่อแก้ไข
ใช้ edit เพื่อจัดการเนื้อหาขององค์ประกอบเป็น Rich Text edit จะไม่สนใจองค์ประกอบที่ไม่ใช่ข้อความ (เช่น Inline และ Horizontal)
ระบบจะนำองค์ประกอบย่อยที่อยู่ในช่วงข้อความที่ถูกลบออกอย่างสมบูรณ์ออกจากองค์ประกอบ
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);
รีเทิร์น
Text - ข้อความขององค์ประกอบปัจจุบัน
find Text(searchPattern)
ค้นหารูปแบบข้อความที่ระบุในเนื้อหาขององค์ประกอบโดยใช้นิพจน์ทั่วไป
ระบบไม่รองรับฟีเจอร์นิพจน์ทั่วไปของ JavaScript บางส่วนอย่างเต็มที่ เช่น กลุ่มจับภาพและตัวแก้ไขโหมด
รูปแบบนิพจน์ทั่วไปที่ระบุจะได้รับการจับคู่กับบล็อกข้อความแต่ละบล็อก ที่อยู่ในองค์ประกอบปัจจุบันโดยอิสระ
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
search | String | รูปแบบที่จะค้นหา |
รีเทิร์น
Range — ผลการค้นหาที่ระบุตำแหน่งของข้อความค้นหา หรือ null หากไม่มี
รายการที่ตรงกัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
find Text(searchPattern, from)
ค้นหารูปแบบข้อความที่ระบุในเนื้อหาขององค์ประกอบ โดยเริ่มจากผลการค้นหาที่ระบุ
ระบบไม่รองรับฟีเจอร์นิพจน์ทั่วไปของ JavaScript บางส่วนอย่างเต็มที่ เช่น กลุ่มจับภาพและตัวแก้ไขโหมด
รูปแบบนิพจน์ทั่วไปที่ระบุจะได้รับการจับคู่กับบล็อกข้อความแต่ละบล็อก ที่อยู่ในองค์ประกอบปัจจุบันโดยอิสระ
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
search | String | รูปแบบที่จะค้นหา |
from | Range | ผลการค้นหาที่จะค้นหา |
รีเทิร์น
Range — ผลการค้นหาที่ระบุตำแหน่งถัดไปของข้อความค้นหา หรือ null หากไม่มี
รายการที่ตรงกัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Attributes()
ดึงแอตทริบิวต์ขององค์ประกอบ
ผลลัพธ์คือออบเจ็กต์ที่มีพร็อพเพอร์ตี้สำหรับแอตทริบิวต์ขององค์ประกอบที่ถูกต้องแต่ละรายการ โดยที่ชื่อพร็อพเพอร์ตี้แต่ละชื่อจะสอดคล้องกับรายการในDocumentการแจงนับ
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]}`); }
รีเทิร์น
Object — แอตทริบิวต์ขององค์ประกอบ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Attributes(offset)
ดึงข้อมูลแอตทริบิวต์ที่ออฟเซ็ตอักขระที่ระบุ
ผลลัพธ์คือออบเจ็กต์ที่มีพร็อพเพอร์ตี้สำหรับแอตทริบิวต์ข้อความที่ถูกต้องแต่ละรายการ โดยที่ชื่อพร็อพเพอร์ตี้แต่ละชื่อจะสอดคล้องกับรายการในDocumentการแจงนับ
// 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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
รีเทิร์น
Object — แอตทริบิวต์ขององค์ประกอบ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Background Color()
เรียกการตั้งค่าสีพื้นหลัง
รีเทิร์น
String|null - สีพื้นหลังที่จัดรูปแบบในสัญกรณ์ CSS (เช่น '#ffffff') หรือ null
หากองค์ประกอบมีค่าหลายค่าสำหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Background Color(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
รีเทิร์น
String|null — สีพื้นหลังที่จัดรูปแบบในสัญกรณ์ CSS (เช่น '#ffffff')
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Font Family()
ดึงการตั้งค่าชุดแบบอักษร ชื่อแบบอักษรสามารถเป็นแบบอักษรใดก็ได้จากเมนูแบบอักษรในเอกสารหรือ Google Fonts และคำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ ตอนนี้เมธอด get และ set ใช้ชื่อสตริงสำหรับแบบอักษรแทน
enum แล้ว แม้ว่าระบบจะ
เลิกใช้งาน</0x0A>การแจงนับนี้แล้ว แต่จะยังคงใช้งานได้เพื่อความเข้ากันได้กับสคริปต์รุ่นเก่าFont
รีเทิร์น
String|null - ตระกูลแบบอักษร หรือ null หากองค์ประกอบมีค่าหลายค่าสำหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Font Family(offset)
ดึงข้อมูลชุดแบบอักษรที่ออฟเซ็ตอักขระที่ระบุ ชื่ออาจเป็นแบบอักษรใดก็ได้จากเมนู
แบบอักษรในเอกสารหรือ Google Fonts และต้อง
ตรงตามตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ ตอนนี้เมธอด get และ set
ใช้ชื่อสตริงสำหรับแบบอักษรแทนการใช้ Enum แล้ว แม้ว่าระบบจะ
เลิกใช้งาน</0x0A>การแจงนับนี้แล้ว แต่จะยังคงใช้งานได้เพื่อความเข้ากันได้กับสคริปต์รุ่นเก่า
Font
// 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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
รีเทิร์น
String|null — ชุดแบบอักษร
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Font Size()
ดึงการตั้งค่าขนาดแบบอักษร
รีเทิร์น
Number|null — ขนาดแบบอักษร หรือ null หากองค์ประกอบมีค่าหลายค่าสำหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Font Size(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
รีเทิร์น
Number|null — ขนาดแบบอักษร
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Foreground Color()
ดึงข้อมูลการตั้งค่าสีด้านหน้า
รีเทิร์น
String|null - สีพื้นหน้าซึ่งจัดรูปแบบในสัญกรณ์ CSS (เช่น '#ffffff') หรือ null
หากองค์ประกอบมีค่าหลายค่าสำหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Foreground Color(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
รีเทิร์น
String|null — สีพื้นหน้าซึ่งจัดรูปแบบในรูปแบบสัญกรณ์ CSS (เช่น '#ffffff')
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Link Url()
เรียก URL ของลิงก์
รีเทิร์น
String|null — URL ของลิงก์ หรือ null หากองค์ประกอบมีค่าหลายค่าสำหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Link Url(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
รีเทิร์น
String|null — URL ของลิงก์
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Next Sibling()
ดึงองค์ประกอบพี่น้องถัดไปขององค์ประกอบ
องค์ประกอบระดับข้างเคียงถัดไปมีองค์ประกอบหลักเดียวกันและอยู่ต่อจากองค์ประกอบปัจจุบัน
รีเทิร์น
Element|null — องค์ประกอบร่วมถัดไป
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Parent()
ดึงองค์ประกอบหลักขององค์ประกอบ
องค์ประกอบหลักมีองค์ประกอบปัจจุบัน
รีเทิร์น
Container — องค์ประกอบระดับบนสุด
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Previous Sibling()
ดึงองค์ประกอบพี่น้องก่อนหน้าขององค์ประกอบ
องค์ประกอบระดับข้างเคียงก่อนหน้ามีองค์ประกอบระดับบนสุดเดียวกันและอยู่ก่อนองค์ประกอบปัจจุบัน
รีเทิร์น
Element|null — องค์ประกอบระดับข้างเคียงก่อนหน้า
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Text()
ดึงเนื้อหาขององค์ประกอบเป็นสตริงข้อความ
รีเทิร์น
String — เนื้อหาขององค์ประกอบเป็นสตริงข้อความ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Text Alignment()
รับการจัดข้อความ ประเภทการจัดแนวที่ใช้ได้คือ Document, Document และ Document
รีเทิร์น
Text — ประเภทการจัดแนวข้อความ หรือ null หากข้อความมีการจัดแนวข้อความหลายประเภท หรือหากไม่เคยตั้งค่าการจัดแนวข้อความ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Text Alignment(offset)
รับการจัดข้อความสำหรับอักขระเดียว ประเภทการจัดแนวที่ใช้ได้คือ Document, Document และ Document
// 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());
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
offset | Integer | ออฟเซ็ตของอักขระ |
รีเทิร์น
Text — ประเภทการจัดข้อความ หรือ null หากไม่เคยตั้งค่าการจัดข้อความ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Text Attribute Indices()
ดึงข้อมูลชุดดัชนีข้อความที่สอดคล้องกับการเริ่มต้นของ การเรียกใช้การจัดรูปแบบข้อความที่แตกต่างกัน
// 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());
รีเทิร์น
Integer[] — ชุดดัชนีข้อความที่มีการเปลี่ยนแปลงการจัดรูปแบบข้อความ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Type()
ดึง Element ขององค์ประกอบ
ใช้ get เพื่อระบุประเภทที่แน่นอนขององค์ประกอบที่กำหนด
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.'); }
รีเทิร์น
Element — ประเภทองค์ประกอบ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert Text(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/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');
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
offset | Integer | ออฟเซ็ตอักขระที่จะแทรกข้อความ |
text | String | ข้อความที่จะแทรก |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is At Document End()
is Bold()
เรียกการตั้งค่าตัวหนา
รีเทิร์น
Boolean|null — ข้อความเป็นตัวหนาหรือไม่ หรือเป็น Null หากองค์ประกอบมีค่าหลายค่าสำหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Bold(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
รีเทิร์น
Boolean|null — การตั้งค่าตัวหนา
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Italic()
เรียกการตั้งค่าตัวเอียง
รีเทิร์น
Boolean|null — ข้อความเป็นตัวเอียงหรือไม่ หรือเป็น Null หากองค์ประกอบมีค่าหลายค่าสำหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Italic(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
รีเทิร์น
Boolean|null — การตั้งค่าตัวเอียง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Strikethrough()
เรียกการตั้งค่าขีดทับ
รีเทิร์น
Boolean|null - ข้อความมีขีดทับหรือไม่ หรือเป็น Null หากองค์ประกอบมีค่าหลายค่าสำหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Strikethrough(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
รีเทิร์น
Boolean|null — การตั้งค่าขีดทับ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Underline()
เรียกการตั้งค่าขีดเส้นใต้
รีเทิร์น
Boolean|null — ข้อความขีดเส้นใต้ หรือ null หากองค์ประกอบมีค่าหลายค่าสำหรับแอตทริบิวต์นี้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Underline(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
offset | Integer | ออฟเซ็ตอักขระ |
รีเทิร์น
Boolean|null — การตั้งค่าขีดเส้นใต้
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
merge()
ผสานองค์ประกอบกับองค์ประกอบที่อยู่ก่อนหน้าซึ่งเป็นประเภทเดียวกัน
รวมได้เฉพาะองค์ประกอบที่มี Element เดียวกัน ระบบจะย้ายองค์ประกอบย่อยที่อยู่ในองค์ประกอบปัจจุบันไปยังองค์ประกอบร่วมก่อนหน้า
ระบบจะนำองค์ประกอบปัจจุบันออกจากเอกสาร
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();
รีเทิร์น
Text|null — องค์ประกอบที่ผสาน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
remove From Parent()
นำองค์ประกอบออกจากองค์ประกอบหลัก
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(); }
รีเทิร์น
Text|null — องค์ประกอบที่นำออก
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
replace Text(searchPattern, replacement)
แทนที่รูปแบบข้อความที่กำหนดทั้งหมดด้วยสตริงทดแทนที่กำหนดโดยใช้นิพจน์ทั่วไป
ระบบจะส่งรูปแบบการค้นหาเป็นสตริง ไม่ใช่ออบเจ็กต์นิพจน์ทั่วไปของ JavaScript ด้วยเหตุนี้ คุณจึงต้องหลีกเลี่ยงการใช้แบ็กสแลชในรูปแบบ
วิธีการนี้ใช้ไลบรารีนิพจน์ทั่วไป RE2 ของ Google ซึ่งจำกัดไวยากรณ์ที่รองรับ
รูปแบบนิพจน์ทั่วไปที่ระบุจะได้รับการจับคู่กับบล็อกข้อความแต่ละบล็อก ที่อยู่ในองค์ประกอบปัจจุบันโดยอิสระ
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Clear the text surrounding "Apps Script", with or without text. body.replaceText('^.*Apps ?Script.*$', 'Apps Script');
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
search | String | รูปแบบนิพจน์ทั่วไปที่จะค้นหา |
replacement | String | ข้อความที่จะใช้แทน |
รีเทิร์น
Element — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Attributes(startOffset, endOffsetInclusive, attributes)
ใช้แอตทริบิวต์ที่ระบุกับช่วงอักขระที่กำหนด
พารามิเตอร์แอตทริบิวต์ที่ระบุต้องเป็นออบเจ็กต์ที่ชื่อพร็อพเพอร์ตี้แต่ละรายการเป็นรายการใน
การแจงนับ Document และค่าพร็อพเพอร์ตี้แต่ละรายการเป็นค่าใหม่ที่จะ
ใช้
// 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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
start | Integer | ออฟเซ็ตเริ่มต้นของช่วงข้อความ |
end | Integer | ออฟเซ็ตสิ้นสุดของช่วงข้อความ |
attributes | Object | แอตทริบิวต์ขององค์ประกอบ |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Attributes(attributes)
ตั้งค่าแอตทริบิวต์ขององค์ประกอบ
พารามิเตอร์แอตทริบิวต์ที่ระบุต้องเป็นออบเจ็กต์ที่ชื่อพร็อพเพอร์ตี้แต่ละรายการเป็นรายการใน
การแจงนับ Document และค่าพร็อพเพอร์ตี้แต่ละรายการเป็นค่าใหม่ที่จะ
ใช้
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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
attributes | Object | แอตทริบิวต์ขององค์ประกอบ |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Background Color(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/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');
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
start | Integer | ออฟเซ็ตเริ่มต้นของช่วงข้อความ |
end | Integer | ออฟเซ็ตสิ้นสุดของช่วงข้อความ |
color | String | สีพื้นหลังที่จัดรูปแบบในสัญกรณ์ CSS (เช่น '#ffffff') |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Background Color(color)
ตั้งค่าสีพื้นหลัง
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
color | String | สีพื้นหลังที่จัดรูปแบบในรูปแบบ CSS (เช่น '#ffffff') |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Bold(bold)
set Bold(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
start | Integer | ออฟเซ็ตเริ่มต้นของช่วงข้อความ |
end | Integer | ออฟเซ็ตสิ้นสุดของช่วงข้อความ |
bold | Boolean | การตั้งค่าตัวหนา |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Font Family(startOffset, endOffsetInclusive, fontFamilyName)
กำหนดชุดแบบอักษรสำหรับช่วงอักขระที่ระบุ ชื่อแบบอักษรอาจเป็นแบบอักษรใดก็ได้จากเมนูแบบอักษรในเอกสารหรือ Google Fonts และคำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่
ชื่อแบบอักษรที่ไม่รู้จักจะแสดงเป็น Arial ตอนนี้เมธอด get และ
set ใช้ชื่อสตริงสำหรับแบบอักษรแทน enum แล้ว แม้ว่าระบบจะ
เลิกใช้งาน</0x0A>การแจงนับนี้แล้ว แต่จะยังคงใช้งานได้เพื่อความเข้ากันได้กับสคริปต์รุ่นเก่า
Font
// 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');
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
start | Integer | ออฟเซ็ตเริ่มต้นของช่วงข้อความ |
end | Integer | ออฟเซ็ตสิ้นสุดของช่วงข้อความ |
font | String | ชื่อชุดแบบอักษรจากเมนูแบบอักษรในเอกสารหรือ Google Fonts |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Font Family(fontFamilyName)
ตั้งค่าชุดแบบอักษร ชื่อแบบอักษรสามารถเป็นแบบอักษรใดก็ได้จากเมนูแบบอักษรในเอกสารหรือ Google Fonts และคำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ ชื่อแบบอักษรที่ระบบไม่รู้จักจะแสดงเป็น Arial ตอนนี้เมธอด get และ set ใช้ชื่อสตริงสำหรับแบบอักษรแทนการแจงนับ แล้ว แม้ว่าระบบจะ
เลิกใช้งาน</0x0A>การแจงนับนี้แล้ว แต่จะยังคงใช้งานได้เพื่อความเข้ากันได้กับสคริปต์รุ่นเก่าFont
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
font | String | ชื่อชุดแบบอักษรจากเมนูแบบอักษรในเอกสารหรือ Google Fonts |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Font Size(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
start | Integer | ออฟเซ็ตเริ่มต้นของช่วงข้อความ |
end | Integer | ออฟเซ็ตสิ้นสุดของช่วงข้อความ |
size | Number | ขนาดแบบอักษร |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Font Size(size)
set Foreground Color(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
start | Integer | ออฟเซ็ตเริ่มต้นของช่วงข้อความ |
end | Integer | ออฟเซ็ตสิ้นสุดของช่วงข้อความ |
color | String | สีข้อความที่จัดรูปแบบในรูปแบบ CSS (เช่น '#ffffff') |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Foreground Color(color)
ตั้งค่าสีด้านหน้า
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
color | String | สีข้อความที่จัดรูปแบบในรูปแบบ CSS (เช่น '#ffffff') |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Italic(italic)
set Italic(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
start | Integer | ออฟเซ็ตเริ่มต้นของช่วงข้อความ |
end | Integer | ออฟเซ็ตสิ้นสุดของช่วงข้อความ |
italic | Boolean | การตั้งค่าตัวเอียง |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Link Url(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/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');
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
start | Integer | ออฟเซ็ตเริ่มต้นของช่วงข้อความ |
end | Integer | ออฟเซ็ตสิ้นสุดของช่วงข้อความ |
url | String | URL ของลิงก์ |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Link Url(url)
set Strikethrough(strikethrough)
กำหนดการตั้งค่าขีดทับ
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
strikethrough | Boolean | การตั้งค่าขีดทับ |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Strikethrough(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
start | Integer | ออฟเซ็ตเริ่มต้นของช่วงข้อความ |
end | Integer | ออฟเซ็ตสิ้นสุดของช่วงข้อความ |
strikethrough | Boolean | การตั้งค่าขีดทับ |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Text(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/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.');
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
text | String | เนื้อหาข้อความใหม่ |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Text Alignment(startOffset, endOffsetInclusive, textAlignment)
ตั้งค่าการจัดแนวข้อความสำหรับช่วงอักขระที่กำหนด ประเภทการจัดแนวที่ใช้ได้มีดังนี้
Document, Document และ
Document
// 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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
start | Integer | ออฟเซ็ตเริ่มต้นของช่วงอักขระ |
end | Integer | ออฟเซ็ตสิ้นสุดของช่วงอักขระ (รวม) |
text | Text | ประเภทการจัดแนวข้อความที่จะใช้ |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Text Alignment(textAlignment)
ตั้งค่าการจัดข้อความ ประเภทการจัดแนวที่ใช้ได้คือ Document, Document และ Document
// 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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
text | Text | ประเภทการจัดแนวข้อความที่จะใช้ |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Underline(underline)
ตั้งค่าขีดเส้นใต้
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
underline | Boolean | การตั้งค่าขีดเส้นใต้ |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Underline(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/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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
start | Integer | ออฟเซ็ตเริ่มต้นของช่วงข้อความ |
end | Integer | ออฟเซ็ตสิ้นสุดของช่วงข้อความ |
underline | Boolean | การตั้งค่าขีดเส้นใต้ |
รีเทิร์น
Text — องค์ประกอบปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents