The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search

DocumentService

DocumentService

8 ธนัธ วาคม 2019

พิมพ พ์พครรั้งทที่ 1 : 8 ธนร วาคม 2019

Google Apps Script
เซอรว์ร สิว Document

เรรียบเรียร งโดย
วสัสนต์ต คณคุ ดลดิ กเศวต

[email protected]
Line ID : wasankds

08-1459-8343
www.poeclub.org

สารบบัญ

สารบัญบ ...............................................................................4

คคานคา.................................................................................. 7

บททที่ 1 เซอร์รวิวส Document......................................................9

1.1. เซอรวร์ สวิ Document และ คลาส DocumentApp ................................................10
1.2. โครงสร้าร งของเอกสาร Google Docs .....................................................................11

บทท่ีท 2 คลาส Body..............................................................15

2.1. สว่ส น Body ของเอกสาร ......................................................................................... 16
2.2. จบัธ และล้ราง Body ................................................................................................... 16
2.3. สร้าร งไฟลร์ Google Docs และใสสเ่ นอื้น หาลงไป ..........................................................17
2.4. Methods ตระกลูก Append .................................................................................. 18

2.4.ก.) appendParagraph() (18)
2.4.ข.) appendPageBreak() (19)
2.4.ค.) appendTable() (19)
2.4.ง.) appendListItem(), getListId() , setListId() (20)
2.4.จ.) GlyphType (22)
2.4.ฉ.) appendImage()(23)
2.4.ช.) appendHorizontalRule() (24)
2.5. Methods ตระกลกู Get และ set ............................................................................24
2.5.ก.) getParagraphs(), getListItems(), getTables() (24)
2.5.ข.) getText() (25)
2.5.ค.) getMargin 4 ตัวต และ getPage 2 ตตัว (25)
2.5.ง.) setMargin 4 ตตวั และ setPage 2 ตวตั (26)
2.5.จ.) getParent() (26)
2.5.ฉ.) getAttributes() คลาส Body (27)
2.5.ช.) getAttributes() คลาส Paragraph (27)
2.6. Enum Attribute .................................................................................................. 28
2.7. setAttributes() คลาส Paragraph ......................................................................30

บททที่ 3 Heading..................................................................33

3.1. setHeading() ....................................................................................................... 34
3.2. Enum ParagraphHeading .................................................................................. 35
3.3. setHeadingAttributes() ..................................................................................... 35

บททที่ 4 Elements.................................................................37

4.1. Elements .............................................................................................................. 38
4.2. Interface Element .............................................................................................. 38
4.3. getNumChildren() .............................................................................................. 39
4.4. จบธั และดชูก นดวิ ของ Elements (คลาส Body) .........................................................40
4.5. Enum ElementType .......................................................................................... 41
4.6. ระบชุบ นดิว ELement ดว้ร ย Method ตระกูกล As ......................................................42
4.7. findElement() ..................................................................................................... 43

บททที่ 5 เคอร์รเซอร.ร์ ...............................................................45

5.1. getCursor() .......................................................................................................... 46
5.2. setCursor(), newPosition() ............................................................................... 47

บทท่ีท 6 Selection.................................................................49

6.1. getSelection(), getRangeElements(), getElement() ....................................50
6.2. Methods ตระกกลู Insert – ยร้ายไป Element .......................................................54

บททที่ 7 Text.......................................................................55

7.1. คลาส Text ............................................................................................................ 56
7.2. editAstext(), insertText() .................................................................................. 56
7.3. deleteText() ........................................................................................................ 57
7.4. replaceText() ...................................................................................................... 58

บททที่ 8 ภาพ........................................................................61

8.1. ใสภ่ส าพลงในตตาแหนงส่ Cursor ................................................................................. 62



คาค นาค

หนัตงสสือเลล่มน้นี เป็ปนหน่นึงในชชุด การเขีขยนโปรแกรม Google Apps Script โดยในเลล่มนนี้อธิธบายการใช้ชงาน
เซอร์วร สวิ Document ก็กคือส เซอรวร์ ธสิ สสาหรบตั การใชช้งาน Google Docs นัึนตน่ เอง

ผเชผ้ ขนยน เขนยนหนงัต สอสื เลม่ล นน้ี จดชุ ประสงค์ดร ันีตง้ เดิมธ กก็คสือ เกบ็ก ไวรอ้ า่ส นเอง

เมสืนึอ่ ผ้ผชเขยน นศนกษาเรสอึืน่ งอะไร ก็กจะไปเรยน นร้ผชจากส่สืนึอออนไลนร์ในอินธ เตอร์รเน็กต ทนงีัต้ คอร์รสออนไลน์ร วธิดนโอ หรืสอ เอกสาร
ทง้ีนัต ในแบบฟรแน ละเสนยเงิธน

ในยชุคปัปจจุชบันต เราตอ้ช งเรยน นร้ชผอะไรใหช้เรก็ว โดยเฉพาะเรื่ึสนองของ IT ผชผเ้ ขนยนจนงต้ังีนต ใจจะดผวธิดนโอเพนยงรอบเดนยว จนงดผไป
พธมิ พร์สรชุปไปดวช้ ย เวลาจาส อะไรไมไ่ล ด้ช มาดผจากทน่ึพธิมพร์สรุชปไวช้งลา่ ยกวล่าการไปยอช้ นดผจากวธิดนโอ นอกจากน้ีนกย็ก ัตงนาส มาทบทวน
ได้งช ่าล ย ในอนาคตสามารถเพ่นึิธมเติมธ เสรมธิ แตลง่ เนนสีือ้ หาไดเช้ รึนอืส่ ยๆด้วช ย

หนังต สอสื เลล่มน้นี ความตนี้ตังใจดีันตง้ เดิมธ ของผเชผ้ ขยน น ก็กคอสื ต้นืงัธ ใจเก็บก ไวร้อาส่ นเองคนเดีขยว เหตเุบ พราะเน้นอื หาเอามาจากของ
คนอื่อนนเป็ปนส่วส นใหญส่ ผร้กูเขียข นเขีขยนเพวมอื่ิ ไปไมส่มาก

อย่ลางไรก็ดก น อตชุ สาล่ หร์พธมิ พไร์ วช้เปปน็ หนัตงสสือแลว้ช จะเก็บก ไวช้อา่ล นคนเดนยวกก็รสชผ้ นกเสนยดาย ผผ้เช ขยน นจนงนสามาแบล่งปปัน

เนนืออ่ งดว้ร ย ผเกู้ร ขยีข นใหค้ร วามสตาคัญธ กบัธ ประเดน็ก ด้าร นลขวิ สวิทธิ์วมธ าก ฉะนั้ธนืนจงึจ ขอแจร้งไว้ร ณ ทนอืีข่ ื้ีขน ตันืง้ธ แตต่ส นร้
กค็ก อน

1. เนส้นืีอหาในหนัตงสสือ ผเช้ผ ขนยนรวบรวมมาจากแหลลง่ ตา่ล งๆในอินธ เตอรร์เน็กต ซน่ึงจะพยายามใหม้ช ากทน่ึสุชด ท่จึน ะบอกลธิงคร์
หรือส แหล่ลงทึน่มาในแตล่ละหตัวข้อช เพราะหนงัต สอืส เล่มล นน้ีถผกเขยน นไวช้นานแลว้ช บางเร่ึอสืน งลมสื ก๊กอปปีปล้ งธิ คม์ร าแปะไว้ช

2. ผเชผ้ ขยน นเปปน็ เพนยงผชผร้ วบรวมเนี้อนืส หา เขนยนเพึมนิ่ธ เติธมเพยน งเล็กก นอ้ช ย
3. หนงตั สืสอเล่มล นี้นแจกฟรน ผเผช้ ขนยนไมม่ล รน ายได้จช ากหนตงั สืสอเล่มล นน้ี

หนธังสนอเลส่มนนขื้ียัธงไม่สจบเสีขยทีขเดขียว หากผกูร้เขขียนวส่าง จะมาเขขียนเพิอวื่มเติวมเรน่อือยๆ ให้รดกูเวอรร์ชธัอ่ืนตามวัธนทขอืี่ทขี่ือปลส่อย
หนังธ สนอ

วสนัต ต์ร คณชุ ดธิลกเศวต

[email protected]
081-459-8343

Line ID : wasankds



บททีท่ 1
เซอรร์วสิว
Document

1.1. เซอรรว์ วิส Document และ คลาส DocumentApp

เซอรพว์ สพิ Document

https://developers.google.com/apps-script/reference/document

เซอร์วร ิสธ Document ใชสช้ ราช้ ง เข้ชาถนงไฟล์ร และแก้ไช ขขอ้ช มผลตา่ล งๆในไฟลร์ Google Docs เซอรวร์ สิธ
Document ประกอบไปดว้ช ยหลากหลายคลาส ท่นมึ คน วามสมัต พันต ธ์รกนตั อยา่ล งเปปน็ โครงสรา้ช ง

คลาส DocumentApp

https://developers.google.com/apps-script/reference/document/document-app

การใชง้ช านเซอรว์ร สิธ Document เรธ่ึมิน ตชน้ จาก คลาส DocumentApp

คลาส DocumentApp ประกอบไปดวช้ ย Properties(Enum) และ Methods ดตังตอล่ ไปนี้น

Properties หรอรื Enum ต่ตางๆในคลาส DocumentApp

Property Type Description
Attribute Attribute The Attribute enumeration.
ElementType ElementType The ElementType enumeration.
FontFamily FontFamily The FontFamily enumeration.
GlyphType GlyphType The GlyphType enumeration.
HorizontalAlignment HorizontalAlignment The HorizontalAlignment enumeration.
ParagraphHeading ParagraphHeading The ParagraphHeading enumeration.
PositionedLayout PositionedLayout The PositionedLayout enumeration.
TextAlignment TextAlignment The TextAlignment enumeration.
VerticalAlignment VerticalAlignment The VerticalAlignment enumeration.

Methods

Method Return type Brief description

create(name) Document Creates and returns a new document.

getActiveDocument() Document Returns the document to which the script is container-
bound.

Returns an instance of the document's user-interface

getUi() Ui environment that allows the script to add features like

menus, dialogs, and sidebars.

openById(id) Document Returns the document with the specified ID.

openByUrl(url) Document Opens and returns the document with the specified URL.

หมายเหตุบ : หนงตั สือส เล่ลมนนี้จะขอข้ชามเรอสืนึ่ ง Methods ของคลาส DocumentApp เพราะทชุกเซอรวร์ ิสธ
หลักต ๆ เชน่ล SpreadApp, FormApp ใชช้งานเหมือส นกัตน และไดอ้ช ธธบิ ายไปแล้วช ในหนตังสือส เลน่ล อนืึนส่

ตวัต อยา่ล ง โคช้ดตล่อไปนีน้ เรยน กใช้เช ซอรวร์ สิธ Document โดยเรนม่ธิึ ตนช้ จากคลาส DocumentApp

// เปดปิ ไฟลลโ์ ดยใช้ช ID
var doc = DocumentApp.openById('DOCUMENT_ID_GOES_HERE');

// สรช้างและเปปิดเอกสารใหมม่ - คนคื ค่าม กลบลั มาเป็ปน Document
doc = DocumentApp.create('Document Name');

10 Google Apps Script เซอรพ์วิพส Doucument

1.2. โครงสราร้ งของเอกสาร Google Docs

Structure of a document
https://developers.google.com/apps-script/guides/docs#structure_of_a_document

เอกสาร Google Docs ประกอบไปด้ชวยวัตตถชุหรสือคลาสจาส นวนหนนึ่ง(เรนยกรวมๆวล่า Elements) มนความ
สัมต พัตนธกร์ ัตนอยา่ล งเปป็นโครงสร้ชาง ซนึ่งสามารถเขนยนเป็ปนผัตง Structure of a document ไดช้ในหนา้ช ถัตดไป ผตังนนี้ เป็ปน
หวัต ใจสสาคัญต ในการเขยน นสครปิธ ต์ร

ตวัธ ทือขีข่ ดขี เสร้นใต้ร หมายถนง แทรกลงในเอกสารไดช้
แตท่ส ่ขีือไมม่ส ขีขดีข เสรน้ ใตร้ หมายถนง สามารถจตดั การในลตักษณะอยผล่กับต ทน่ึไดช้

เรยีร บเรีรยงโดย วสลนั ตล์ คคณุ ดลดิ กเศวต ([email protected]) 11

Document

|- 1.Body // Body ประกอบไปดวช้ ย 4 ตัลว กคก็ อคื ListItem, Paragraph, Table, TableOf Contents

| |- ListItem (1)

| | |- Equation

|| |- EquationFunction

|| |- EquationFunction…

|| |- EquationFunctionArgumentSeparator

|| |- EquationSymbol

|| |- Text

|| |- EquationSymbol

|| |- Text

| | |- Footnote

| | |- HorizontalRule

| | |- InlineDrawing

| | |- InlineImage

| | |- PageBreak

| | |- Text

||

| |- Paragraph (2)

| | |- Equation

|| |- EquationFunction

|| |- EquationFunction…

|| |- EquationFunctionArgumentSeparator

|| |- EquationSymbol

|| |- Text

|| |- EquationSymbol

|| |- Text

| | |- Footnote

| | |- HorizontalRule

| | |- InlineDrawing

| | |- InlineImage

| | |- PageBreak

| | |- Text

||

| |- Table (3)

| | |- TableRow

|| |- TableCell

|| |- Paragraph…

|| |- ListItem…

|| |- Table…

||

| |- TableOfContents (4)

| |- Paragraph…

| |- ListItem…

| |- Table…

|

(มตท อ่ต )

12 Google Apps Script เซอรว์พ สิพ Doucument

(ตอ่ต )
|
|
|- 2.HeaderSection
| |- ListItem
| |- HorizontalRule
| |- InlineDrawing
| |- InlineImage
| |- Text
| |- UnsupportedElement (page number, etc.)
| |- Paragraph
| |- HorizontalRule
| |- InlineDrawing
| |- InlineImage
| |- Text
| |- UnsupportedElement (page number, etc.)
| |- Table
| |- TableRow
| |- TableCell
| |- Paragraph…
| |- ListItem…
| |- Table...
|
|- 3.FooterSection
| |- ListItem
| |- HorizontalRule
| |- InlineDrawing
| |- InlineImage
| |- Text
| |- UnsupportedElement (page number, etc.)
| |- Paragraph
| |- HorizontalRule
| |- InlineDrawing
| |- InlineImage
| |- Text
| |- UnsupportedElement (page number, etc.)
| |- Table
| |- TableRow
| |- TableCell
| |- Paragraph…
| |- ListItem…
| |- Table...
|
|- 4.FootnoteSection
|- ListItem
|- HorizontalRule
|- Text
|- Paragraph
|- HorizontalRule
|- Text

เรียร บเรยีร งโดย วสนลั ต์ล คุณค ดิลด กเศวต ([email protected]) 13

14 Google Apps Script เซอรพ์วิสพ Doucument

บททที่ 2
คลาส Body

2.1. สวส่ น Body ของเอกสาร

คลาส Body
https://developers.google.com/apps-script/reference/document/body.html

Body ของเอกสาร กค็ก ือส พนืส้นี ทหึน่ ลตักทึเน่ ราใสเล่ นืน้ีสอหาลงในเอกสาร Google Docs
Body ประกอบไปดว้ช ย Elements หลัตกๆ 4 ตวัต ก็กคอสื ListItem(หวัล ขชอ้ อัลตโนมลัติด เชม่น Bullets หรอคื
Numbering), Paragraph(ยอ่ม หนาช้ ), Table(ตาราง) และ TableOfContents(สารบลญั ) ยกเวร้น
HeaderSection(หลวั กระดาษ), FooterSection(ท้ชายกระดาษ) และ FootnoteSection(เชงิด อรรถ)

2.2. จจบั และล้ราง Body

getBody() - Method ในคลาส Document
https://developers.google.com/apps-script/reference/document/document.html#getbody

ใชจช้ ัตบส่ลวน Body ของเอกสาร โดยจะคสืนคา่ล กลตับมาเป็นป คลาส Body

คลาส Body
https://developers.google.com/apps-script/reference/document/body.html

ตวัต อยาล่ ง
โคดช้ ตลอ่ ไปนนี้ พมิธ พ์รย่ลอหนช้าท่ึมน ขน อ้ช ความ A paragraph ตอ่ล ท้ชาย

// จบลั ไปทไรี่ท ฟลล์ Google Docs ทีรท่โปรเจคก็ Google Apps Script ฝังฝ อย่มย
var doc = DocumentApp.getActiveDocument() ;
// จบลั ไปทรี่ท Body ของเอกสาร
var body = doc.getBody() ;
// แนบท้าช ยดว้ช ยยอ่ม หน้าช
body.appendParagraph("A paragraph.");
// ขนึข้ หนช้าใหมม่
body.appendPageBreak();

หากต้อช งการลาช้ งขช้อความใน Body ใชช้ Method clear()

clear() - Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body.html#clear

ตวัต อยาล่ ง

body.clear() // ล้าช งบอดี้ขึร

16 Google Apps Script เซอร์วพ ิสพ Doucument

2.3. สรา้ร งไฟลร์ Google Docs และใสเส่ นือน้ หาลงไป

Extending Google Docs – The basics
https://developers.google.com/apps-script/guides/docs#the_basics

สรา้ช งโปรเจค็ก Google Apps Script แบบ Stand alone จากนตันน้ี พธิมพร์โคช้ดต่อล ไปนลนี้ งไป

function createDoc() {
var doc = DocumentApp.create('Sample Document') ;
var body = doc.getBody() ;
var rowsData = [ [ 'Plants' , 'Animals' ] ,
[ 'Ficus' , 'Goat' ] ,
[ 'Basil' , 'Cat' ] ,
[ 'Moss' , 'Frog' ] ] ;
// แทรกยมอ่ หน้ชาทต่ทีร ตาแหนม่ง Element ที่รท 0 (ยม่อหนช้าแรก) ข้อช ความในยอ่ม หนาช้ คคือช่ีอทรื ไฟลพ์
// และ กาต หนดสไตลล์ย่อม หนาช้ เป็นป Heading1
body.insertParagraph(0, doc.getName())
.setHeading(DocumentApp.ParagraphHeading.HEADING1) ;
// แนบทช้ายด้าช ยตาราง ใสเ่ม นอข้ืคึ หาตามค่าม ในตลัวแปรอารย์ล rowsData
table = body.appendTable(rowsData) ;
// จับล แถบแรกของตาราง + แกไ้ช ขแบบข้อช ความ + กตาหนดเปน็ป ตลัวหนา
table.getRow(0).editAsText().setBold(true) ;

}

ผล

เรยีร บเรีรยงโดย วสลันต์ล คคณุ ดดลิ กเศวต ([email protected]) 17

2.4. Methods ตระกกลู Append

Methods ตระกผล Append ใช้แช นบทา้ช ยบางอยา่ล งลงในเอกสาร

2.4.ก.) appendParagraph()

appendParagraph(text) – Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body.html#appendparagraphtext
appendParagraph(paragraph) – Method ในคลาส Body

https://developers.google.com/apps-script/reference/document/body.html#appendparagraphparagraph

คลาส Paragraph
https://developers.google.com/apps-script/reference/document/paragraph.html

appendParagraph() ใข้แช นบทา้ช ยด้ชวยย่ลอหน้าช โดยจะคสนื คาล่ กลัตบมาเป็ปนคลาส Paragraph พารามเธิ ตอร์ร
ของ appendParagraph() เปปน็ text(ขอ้ข ความ) หรสือ คลาส Paragraph กก็ได้ช การระบชเุ ปปน็ คลาส Paragraph
ใชช้ในกรณกน ๊กอปปย้ีป ่ลอหนชา้

ตวตั อยา่ล งการใชงช้ าน

function appendParas(){
var body = DocumentApp.getActiveDocument().getBody() ;
// สร้าช งยม่อหนาช้ แล้ชวจลับใสมต่ วลั แปร
var header = body.appendParagraph("A Document") ;
// กตาหนดสไตลยล์ ม่อหนช้า HEADING1 กับล ยมอ่ หนช้า
header.setHeading(DocumentApp.ParagraphHeading.HEADING1) ;
// แนบทช้ายเอกสารดช้วยย่มอหน้ชาทมรีท่ ขรี อ้ช ความ Section 1
var section = body.appendParagraph("Section 1") ;
// กตาหนดสไตล์ลยอ่ม หนช้า HEADING2 กบลั ยอม่ หนชา้
section.setHeading(DocumentApp.ParagraphHeading.HEADING2) ;
// แนบทา้ช ยเอกสารด้วช ยยมอ่ หนาช้ ที่มรท ขรี อ้ช ความ … - เปปน็ ยอม่ หนา้ช ปกติด (ใชช้สไตลล์ Normal)
var para = body.appendParagraph("This is a typical paragraph.") ;
// กอก๊ ปปย้ปี ม่อหนาช้ – พารามดิเตอรเล์ ปป็นวตลั ถยคุ ม่อหน้ชา
body.appendParagraph(para.copy()) ;

}

ผล

18 Google Apps Script เซอรว์พ ิพส Doucument

2.4.ข.) appendPageBreak()

appendPageBreak() – Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body.html#appendpagebreak

appendPageBreak(pageBreak) – Method ในคลาส Body

https://developers.google.com/apps-script/reference/document/body.html#appendpagebreakpagebreak

ตามเอกสารของ Google อธธิบายวล่า ใชช้สร้ชางและแนบคลาส PageBreak ซน่ึงอธธิบายงล่ายๆได้ชอนกอยล่างวล่า
ใชชข้ นีน้ ใหมนล่ ันึนต่ เอง โดยจะคสืนคล่ากลับต มาเปปน็ คลาส PageBreak

ตวัต อย่าล ง

body.appendPageBreak() ; // ขน้ขึ หนชา้ ใหมม่

2.4.ค.) appendTable()

appendTable() – Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body.html#appendtable

appendTable(cells) – Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body.html#appendtablecells

appendTable(table) – Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body.html#appendtabletable

คลาส Table
https://developers.google.com/apps-script/reference/document/table.html

ใชช้แนบทา้ช ยด้ชวยตาราง โดยจะคสืนคา่ล กลัตบมาเป็ปนคลาส Table พารามธิเตอร์รสามารถเปป็น cells(String[][]
หรสอื ขอข้ ความในอาเรยพ์ 2 มติพ ิพ) , เปนป็ คลาส Table หรสือ ไมต่ระบบุพารามเพิ ตอรพ์ กกไ็ ดช้

แบบ ไม่ตระบุบพารามพิเตอรพ์ จะแนบย่ลอหน้ชาวล่างๆหลัตงตาราง เพราะเอกสาร Google Docs จบด้ชวยตาราง
ไมลไ่ ดช้

แบบ คลาส Table ใชใ้ช นกรณกน ๊กอปปป้ีตาราง การใชงช้ านเชน่ล body.appendTable(table.copy()) ;

ตวตั อยล่าง

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

// อาเรย์ล 2 มิดตทิด รีจท่ ะใชชเ้ ป็นป ขช้อมลย ใหกช้ บลั ตาราง
var data = [ [ 'Plants' , 'Animals' ] ,

[ 'Ficus' , 'Goat' ] ,
[ 'Basil' , 'Cat' ] ,
[ 'Moss' , 'Frog' ] ] ;

var table = body.appendTable(data) ; // ตารางทีทร่ 1

body.appendTable(table.copy()) ; // ตารางททีร่ 2 ก๊กอปปตปี้ ารางท่ีรท 1 มา

เรีรยบเรียร งโดย วสัลนตล์ คุคณดิลด กเศวต ([email protected]) 19

ผล – ไดช้ 2 ตาราง ตารางแรกสราช้ งจากอารย์รขชอ้ ความ อนกตารางกอ๊ก ปปป้ีมาจากตารางแรก

2.4.ง.) appendListItem(), getListId() , setListId()

appendListItem(text) – Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body.html#appendlistitemtext
appendListItem(listItem) – Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body.html#appendlistitemlistitem
คลาส ListItem
https://developers.google.com/apps-script/reference/document/list-item.html

ใชช้สร้ชางและแนบ ListItem ตตัวใหมล่ โดยจะคสืนค่ลากลัตบมาเปป็นคลาส ListItem พารามิธเตอรร์สามารถเป็ปน
text หรสอื เปปน็ คลาส ListItem(ใช้ใช นกรณนกอ๊ก ปปีป้ ListItem)

1 ListItem มน 1 ย่ลอหน้ชา และมน List ID ด้ชวย โดย List ID ใช้ชเป็ปนตตัวจตัดกลชุล่มวล่าเปป็น ListItem กลชุล่ม
เดยน วกันต หรืสอไม่ล

ListItem ไม่ลสามารถมน \n (new-line characters) โดยจะถผกแปลงเป็ปน \r (line-break characters

คลชา้ ยกด <Shift><Enter>)

20 Google Apps Script เซอร์วพ ิสพ Doucument

ตวตั อย่าล ง

function appendListItem(){

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

// แนบทา้ช ยดช้วย ListItem ลตาดบลั ทีท่ร 1
var item1 = body.appendListItem('Item 1') ;

// จลบั List ID ของ item1 // เช่มนพมิด พล์ : kix.z8suvlutfedt
Logger.log(item1.getListId()) ;

// แนบทา้ช ยด้วช ยตาราง จดคุ ประสงคเล์ พคืทีอ่ ขนึ้ขลั กลางระหว่าม ง ListItem
body.appendTable([['Cell 1', 'Cell 2']]) ;

// แนบท้ชายดวช้ ย ListItem ลาต ดบลั ที่ทร 2
// และเซ็ตก List ID ตวัล เดีรยวกบลั Item1 เพที่ืคอใหเช้ ปป็นกลม่มุค เดยรี วกลนั ตวร เลขจงจึ นรบตอ่ต กรน
var item2 = body.appendListItem('Item 2') ;
item2.setListId(item1) ;

// แนบทช้ายดช้วย ListItem ลาต ดบัล ท่ีรท 3 - ข้อช ความยาวๆ
var item3 = body.appendListItem('Item 3 : ยอม่ หน้ชายาวๆ ยอ่ม หนา้ช ยาวๆ ยอม่ หนา้ช ยาวๆ ยมอ่ หน้ชายาวๆ

ยอม่ หน้าช ยาวๆ ยม่อหนช้ายาวๆ ย่มอหนาช้ ยาวๆ ยอ่ม หนช้ายาวๆ ยม่อหนาช้ ยาวๆ') ;

// แนบท้ชายดช้วย ListItem ลตาดบัล ทีรท่ 4 – มีกร ารขขนึ้ บรรทัดล ใหมม่ (คลชา้ ยการกด <Shift><Eeter>)
var item4 = body.appendListItem('Item 4 : บรรทรดทท่ี 1\n บรรทดร ท่ที 2\n บรรทรดทที่ 3\n') ;
}

ผล

getListId() - Method ในคลาส ListItem
https://developers.google.com/apps-script/reference/slides/list?hl=vi#getlistid

ใชช้จตับ List ID

settListId(listItem) - Method ในคลาส ListItem
https://developers.google.com/apps-script/reference/document/list-item#setlistidlistitem

ใชชก้ สาหนด List ID ให้กช บตั LIstItem

เรีรยบเรียร งโดย วสันล ตล์ คุณค ดลิด กเศวต ([email protected]) 21

2.4.จ.) GlyphType

Enum GlyphType

https://developers.google.com/apps-script/reference/document/glyph-type

ใชกช้ สาหนดรปผ แบบของ Bullet ใหก้ช ตับ ListItem การใช้งช านเชน่ล

DocumentApp.GlyphType.Property

Property Type Description
BULLET Enum The default bullet, circular and filled.
HOLLOW_BULLET Enum A hollow bullet.
SQUARE_BULLET Enum A square bullet.
NUMBER Enum A number based bullet.
LATIN_UPPER Enum A latin, uppercase bullet.
LATIN_LOWER Enum A latin, lowercase bullet.
ROMAN_UPPER Enum A roman numeral, uppercase bullet.
ROMAN_LOWER Enum A roman numeral, lowercase bullet.

ตวตั อยาล่ ง

function bulletType() {

var docID = "1aqAv5bzhIvxWPheMpaQJu2vPTWZQQKd3qIsyGPlq-qM" ;
var docFiles = DocumentApp.openById(docID) ;
var docBody = DocumentApp.openById(docID).getBody() ;

// Insert at list item, with the default nesting level of zero.
docBody.appendListItem("Item 1") ;

// Append a second list item, with a nesting level of one, indented one inch.
// The two items will have different bullet glyphs.

docBody.appendListItem("Item 2")
.setNestingLevel(1)
.setIndentStart(72)
.setGlyphType(DocumentApp.GlyphType.SQUARE_BULLET) ;

}

ผล

22 Google Apps Script เซอรวพ์ สิพ Doucument

2.4.ฉ.) appendImage()

appendImage(image) – Method ในคลาส Body

https://developers.google.com/apps-script/reference/document/body.html#appendimageimage

appendImage(image) – Method ในคลาส Body (ใชช้กรณรีกกอ๊ ปปภ้ีป าพ)

https://developers.google.com/apps-script/reference/document/body.html#appendimageimage_1

คลาส InlineImage
https://developers.google.com/apps-script/reference/document/inline-image.html

ใช้ชสรา้ช งและแนบท้าช ยดวช้ ยภาพ InLineImage โดยจะคนสื คาล่ กลัตบมาเป็นป คลาส InLineImage
พารามธิเตอร์ร image เป็ปน BlobSource ทม่ที ทขอ้ข มมูลเปปน็ ภาพ

ตวัต อยาล่ ง

ดรดแปลงมาจาก - Insert Inline Image in a Google Document
https://ctrlq.org/code/20078-insert-image-in-google-document

function insertImage() {

// ภาพจากเวกบ็ // จบัล Blob ใส่มตลวั แปร
// var imageLink = "https://img.labnol.org/logo.png" ;
// var blob = UrlFetchApp.fetch(imageLink).getBlob() ;

// ภาพจาก Google Drive – จบัล ใสม่ BLOB กอม่ น
var imageLink = "https://drive.google.com/open?id=11inBoKWzWZr_ip2OvBAv3BWY7TbW9JDt" ;
var imageID = imageLink.match(/[\w\_\-]{25,}/).toString() ; // จบัล มาเฉพาะ ID
var blob = DriveApp.getFileById(imageID).getBlob() ; // จบัล Blob ของภาพใสม่ตัลวแปร

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

// แนบทช้ายดว้ช ยภาพลงใน Body เอกสาร
var image = body.appendImage(blob).setWidth(100).setHeight(100/1.7777) ;
// ก๊อก ปปภปี้ าพ
body.appendImage(image.copy()).setWidth(200).setHeight(200/1.7777) ;
}

ผล

เรีรยบเรีรยงโดย วสันล ต์ล คุคณดดลิ กเศวต ([email protected]) 23

2.4.ช.) appendHorizontalRule()

appendHorizontalRule()

https://developers.google.com/apps-script/reference/document/body.html#appendhorizontalrule

ใชแช้ นบท้าช ยดว้ช ยย่อล หน้าช ท่มนึ คน ลาส HorizontalRule โดยจะคนืส คาล่ กลบตั มาเปปน็ คลาส HorizontalRule
HorizontalRule กคก็ อืส เสน้ช ทึข่น นดดชว้ ยแท็กก <hr> ในโคด้ช HTML

2.5. Methods ตระกูลก Get และ set

2.5.ก.) getParagraphs(), getListItems(), getTables()

getParagraphs() - Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body#getparagraphs

ใชช้จัตบย่ลอหน้ชาทนึ่อยลใ่ผ น Body รวมถนงย่ลอหนช้าทน่ึอยผ่ลใน ListItem และ Table ด้วช ย (ถชา้ อยล่ผในวัตตถุชบรรจชุภตัณฑร์
ตวตั อ่ืนึนส กก็จับต ย่อล หนช้าในบรรจภุช ณัต ฑ์รตวัต นนีต้ัน ) โดยจะคืนส คาล่ กลตับมาเปป็น Paragraph[] (คลาส Paragraph ในอารเยล์)

getListItems() - Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body#getlistitems

ใช้ชจตับ ListItem ทนึ่อยล่ผใน Body โดย 1 หัตว(1 ย่ลอหนช้า) จะจัตบมาเปป็น 1 คลาส ListItem(ถ้ชาอยลผ่ในวตัตถุช
บรรจชุภัตณฑ์รตตัวอ่ึืนสน กก็จตับ ListItem ในบรรจุชภตัณฑรต์ ัตวนตนีั้น) โดยจะคืสนค่ลากลัตบมาเป็ปน ListItem[] (คลาส ListItem

ในอารเย์ล)

getTables() - Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body#gettables

ใชช้จตับตารางทนึ่อยผ่ลใน Body (ถ้ชาอย่ผลในวตัตถชุบรรจชุภัตณฑ์รตตัวอืนสึ่น กก็จัตบตารางในบรรจชุภตัณฑ์รตัตวนันีต้น) โดยจะคสืน
คา่ล กลับต มาเปน็ป Table[] (คลาส Table ในอารเยล์)

ตวัต อยา่ล ง

function Tables() {
var body = DocumentApp.getActiveDocument().getBody() ;
var paras = body.getParagraphs() ;
var lists = body.getListItems() ;
var tables = body.getTables() ;

Logger.log(body) ; // พมดิ พ์ล : DocumentBodySection
Logger.log(paras) ; // พมดิ พล์ : [Paragraph, Paragraph, Paragraph, … ]
Logger.log(lists) ; // พมิด พล์ : [ListItem, ListItem]
Logger.log(tables) ; // พมิด พล์ : DocumentBodySection

Logger.log(body.getType()) ; // พมิด พ์ล : BODY_SECTION ()
Logger.log(paras[0].getType()); // พมิด พล์ : PARAGRAPH
Logger.log(lists[0].getType()); // พมิด พ์ล : LIST_ITEM
Logger.log(tables[0].getType()); // พมดิ พล์ : TABLE

tables.forEach(function(table){ // แนบท้าช ยดช้วยตารางท่กรีท ๊อก ปปไป้ี วช้ ลงไป
body.appendTable(table.copy()) ;

}) ;
}

24 Google Apps Script เซอรพ์วิพส Doucument

หมายเหตบุ
getType() เปป็น Method ในหลายคลาส ดผ Enum ElementType

https://developers.google.com/apps-script/reference/document/element-type

2.5.ข.) getText()

getText() - Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body#gettext

ใชชจ้ ตับข้ชอความทตีันง้ หมดทน่ึอย่ผลใน Body รวมถงน ข้ชอคามทึน่อย่ใผล น ListItem, Paragraph และ Table (ถาช้ อยใผล่ น
วตตั ถชบุ รรจุชภัตณฑรต์ ัวต อึนน่ืส กจ็ก บตั ขอ้ช ความในบรรจชุภณตั ฑต์ร ัวต นัี้นตน ) โดยจะคืสนคาล่ กลตับมาเป็ปนขอ้ช ความ(String)

ตวัต อย่ลาง

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

2.5.ค.) getMargin 4 ตวร และ getPage 2 ตวร

getMarginTop(), getMarginBottom(), getMarginLeft(), getMarginRight() - Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body#getMarginTop()
https://developers.google.com/apps-script/reference/document/body#getMarginBottom()
https://developers.google.com/apps-script/reference/document/body#getMarginLeft()
https://developers.google.com/apps-script/reference/document/body#getMarginRight()

ใชจ้ช ตบั ระยะ Margin(ระยะเวน้ช ขอบกระดาษ) ทีนตัง้ 4 ดาช้ น โดยจะคสนื ค่ลากลตบั มาเปปน็ จสานวนเตม็ก ในหน่วล ย

points

getPageHeight(), getPageWidth() - Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body#getpageheight
https://developers.google.com/apps-script/reference/document/body#getpagewidth

ใชจช้ ตบั ความสผงและความกวา้ช งของหน้าช กระดาษ โดยจะคืสนคาล่ กลับต มาเปน็ป จาส นวนเต็มก ในหนลว่ ย points
ตวัต อย่ลาง

function margin_page() {

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

Logger.log(body.getMarginTop()) ; // เชน่ม พิดมพล์ : 14.1732
Logger.log(body.getMarginBottom()) ; // เชนม่ พดมิ พ์ล : 14.1732
Logger.log(body.getMarginLeft()) ; // เชม่นพมิด พ์ล : 14.1732
Logger.log(body.getMarginRight()) ; // เช่นม พมิด พ์ล : 14.1732

Logger.log(body.getPageHeight()) ; // เชนม่ พดิมพล์ : 400.899887517858
Logger.log(body.getPageWidth()) ; // เชม่นพมดิ พล์ : 283.465
} // Close - function

หมายเหตบุ : 1 ซม. = 28.3465 พอช้ ยตล์
1 นึขดิ้ว
1 พชอ้ ยต์ล = 72 พอ้ช ยต์ล
1 พอ้ช ยตล์ = 0.0352778 ซม.
= 0.0138889 นดขึ้วิ

เรยรี บเรียร งโดย วสลันตล์ คณคุ ดดลิ กเศวต ([email protected]) 25

2.5.ง.) setMargin 4 ตรว และ setPage 2 ตวร

การตตง้ันี ค่ลาหน้าช กระดาษ Google Docs ทนเ่ึ มนผ File → Page Setup ไมมล่ ใน ห้เช ลือส กแบบ Custum Size
หรอืส แบบทึน่เรากาส หนดขนาดเองได้ช มนแตแ่ล บบสาส เรจก็ รผปให้ชเลืสอก เช่ลน A4, A5 เปปน็ ตช้น

หากต้ชองการตน้ีงัต คลา่ หนชา้ กระดาษแบบ Custom Size เราต้ชองเขนยน Script โดยใช้ช Method ตาล่ งๆ เชล่น
setMarginLeft หรสือ setPageHeight ในการกสาหนดขนาดและระยะตาล่ งๆของหน้ชากระดาษ

ตวัต อยา่ล ง

function CustomPageSize(){

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

body.setMarginLeft(14.1732) // ตลัขงึ้ ระยะเวช้นขอบดช้านซา้ช ย 0.5 ซม
.setMarginRight(14.1732) // ตึง้ขัล ระยะเว้ชนขอบดา้ช นขวา 0.5 ซม
.setMarginTop(14.1732) // ตง้ึขัล ระยะเว้นช ขอบดช้านบน 0.5 ซม
.setMarginBottom(14.1732) // ต้งลัึข ระยะเวนช้ ขอบดช้านลา่ม ง 0.5 ซม
.setPageWidth(283.465) // ตึลัข้งความกวา้ช งของหน้ชากระดาษ 10 ซม.
.setPageHeight(400.899887517858) ; // ตัลง้ึข ความกวชา้ งของหน้ชากระดาษ 14.143 ซม.

}

2.5.จ.) getParent()

getParent() - Method ในคลาส Body

https://developers.google.com/apps-script/reference/document/body#getParent()

ใชช้จัตบ Element ทึ่นเปน็ป วตัตถชุบรรจุชภตัณฑร์ เช่ลน Body เปปน็ วัตตถุชบรรจุชภัตณฑ์รของ Paragraph, ListItem และ
Table เปนป็ ตชน้ โดยจะคืสนคาล่ กลตบั มาเปนป็ คลาส ContainerElement (วตัล ถบุค รรจคุภณัล ฑ)์ล

คลาส ContainerElement
https://developers.google.com/apps-script/reference/document/container-element.html

ตวตั อยา่ล ง

function parent() {
var body = DocumentApp.getActiveDocument().getBody() ;
var para = body.getParagraphs()[0] ;
var table = body.getTables()[0] ;
var cell = table.getCell(0, 0) ;

Logger.log(body.getParent()) ; // พมดิ พล์ : DocumentElement
Logger.log(para.getParent()) ; // พมดิ พ์ล : DocumentBodySection
Logger.log(table.getParent()) ; // พมดิ พ์ล : DocumentBodySection
Logger.log(cell.getParent()) ; // พมดิ พ์ล : TableRow
Logger.log(cell.getParent().getParent()) ; // พมิด พล์ : Table

} // Close – function

26 Google Apps Script เซอร์วพ ิพส Doucument

2.5.ฉ.) getAttributes() คลาส Body

getAttributes() - Method ในคลาส Body และหลายคลาส
https://developers.google.com/apps-script/reference/document/body#getAttributes()

ใช้จช บัต คณชุ ลตักษณะ(Attributes) ของ Element (ในทนน่ึ นีก้ กค็ อสื Body) โดยจะคสืนค่ลากลตับมาเป็นป Object[]
หรือส วตร ถุบทเีท่ ก็กบ Enum Attribute ไวช้

ดยเพีมิทด่ เตมิด ขอช้ 2.6 Enum Attribute หนาช้ 28

ตวตั อยา่ล ง

function getAttsBody() {

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

for(var att in attsBody){
Logger.log(att + " : " + attsBody[att]) ;

}

} // Close – function

ผล

Logs

[ 01 ] FONT_SIZE : null
[ 02 ] ITALIC : null
[ 03 ] PAGE_WIDTH : 283.465
[ 04 ] LINK_URL : null
[ 05 ] UNDERLINE : null
[ 06 ] BACKGROUND_COLOR : null
[ 07 ] MARGIN_BOTTOM : 14.1732
[ 08 ] PAGE_HEIGHT : 400.899887517858
[ 09 ] MARGIN_RIGHT : 14.1732
[ 10 ] STRIKETHROUGH : null
[ 11 ] MARGIN_LEFT : 14.1732
[ 12 ] FOREGROUND_COLOR : null
[ 13 ] BOLD : null
[ 14 ] FONT_FAMILY : Sarabun
[ 15 ] MARGIN_TOP : 14.1732

2.5.ช.) getAttributes() คลาส Paragraph

getAttributes ใช้ชบ่ลอยและเป็ปน Method ในหลายคลาส ฉะน้ตันีนจนงขออธิธบาย getAttributes ในคลาส
Paragraph เพนิึ่มธ เตมธิ

getAttributes() - Method ในคลาส Paragraph และหลายคลาส
https://developers.google.com/apps-script/reference/document/paragraph#getAttributes()

เรียร บเรรยี งโดย วสลันต์ล คุคณดิดลกเศวต ([email protected]) 27

ใชจ้ช ับต คุชณลตกั ษณะ(Attributes) ของ Element (ในท่นนึ กน้ี คก็ ือส Paragraph) โดยจะคสนื คาล่ กลบตั มาเปน็ป
Object[] หรือส วตร ถุทบ ่ีทเกก็บ Enum Attribute ไวช้ ( ดยเพทิดมี่ เติดมขอช้ 2.6 Enum Attribute หนา้ช 28 )

ตวตั อยา่ล ง

function getAttsPara() {

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

// แนบทช้ายดว้ช ยยอ่ม หน้ชาตอม่ ไปนขีึร้
var par = body.appendParagraph('A bold, italicized paragraph.') ;
par.setBold(true) ;
par.setItalic(true) ;

// จบัล Attributes ของย่อม หนช้า
var atts = par.getAttributes() ;

for (var att in atts) {
Logger.log(att + " : " + atts[att]) ;

}
} // Close – function

ผล

Logs

[ 01 ] FONT_SIZE : 8
[ 02 ] ITALIC : true
[ 03 ] HORIZONTAL_ALIGNMENT : Left
[ 04 ] INDENT_END : 0
[ 05 ] INDENT_START : 0
[ 06 ] LINE_SPACING : 1.15
[ 07 ] LINK_URL : null
[ 08 ] UNDERLINE : null
[ 09 ] BACKGROUND_COLOR : null
[ 10 ] INDENT_FIRST_LINE : 0
[ 11 ] LEFT_TO_RIGHT : true
[ 12 ] SPACING_BEFORE : 0
[ 13 ] HEADING : Normal
[ 14 ] SPACING_AFTER : 0
[ 15 ] STRIKETHROUGH : null
[ 16 ] FOREGROUND_COLOR : null
[ 17 ] BOLD : true
[ 18 ] FONT_FAMILY : Sarabun

2.6. Enum Attribute

https://developers.google.com/apps-script/reference/document/attribute

Enum Attribute เป็ปนการแจกแจง(Enumeration) คุชณลตักษณะตล่างๆขององค์รประกอบหรสือวัตตถชุ หรสือ
อธธิบายอนกอย่ลางวล่า เปป็นคุชณสมบัตติธของวตัตถชุท่นึถผกกสาหนดไว้ชแลช้ว วล่าเราสามารถเขช้าไปตีต้ันงค่ลาอะไรไดช้บ้ชาง เช่ลน
คณชุ สมบัตตธิ BOLD กกค็ สอื คุบณสมบตร พิตรวหนาของอกร ษร เปน็ป ต้นช

28 Google Apps Script เซอรว์พ สพิ Doucument

เราสามารถเรนยกใชช้ Enum Attribute ไดช้จาก DocumentApp.Attribute.[Property] โดย Properties มน
ดงัต ต่ลอไปนน้ี

Property Type Description

BACKGROUND_COLOR Enum The background color of an element (Paragraph, Table, etc) or
document.

BOLD Enum The font weight setting, for rich text.

BORDER_COLOR Enum The border color, for table elements.

BORDER_WIDTH Enum The border width in points, for table elements.

CODE Enum The code contents, for equation elements.

FONT_FAMILY Enum The font family setting, for rich text.

FONT_SIZE Enum The font size setting in points, for rich text.

FOREGROUND_COLOR Enum The foreground color setting, for rich text.

HEADING Enum The heading type, for paragraph elements (for
example, DocumentApp.).

HEIGHT Enum The height setting, for image elements.

HORIZONTAL_ALIGNMENT Enum The horizontal alignment, for paragraph elements (for
example, DocumentApp.).

INDENT_END Enum The end indentation setting in points, for paragraph elements.

INDENT_FIRST_LINE Enum The first line indentation setting in points, for paragraph elements.

INDENT_START Enum The start indentation setting in points, for paragraph elements.

ITALIC Enum The font style setting, for rich text.

GLYPH_TYPE Enum The glyph type, for list item elements.

LEFT_TO_RIGHT Enum The text direction setting, for rich text.

LINE_SPACING Enum The line spacing setting as a multiplier, for paragraph elements.

LINK_URL Enum The link URL, for rich text. The default link style (foreground color,
underline) is automatically applied.

LIST_ID Enum The ID of the encompassing list, for list item elements.

MARGIN_BOTTOM Enum The bottom margin setting in points, for paragraph elements.

MARGIN_LEFT Enum The left margin setting in points, for paragraph elements.

MARGIN_RIGHT Enum The right margin setting in points, for paragraph elements.

MARGIN_TOP Enum The top margin setting in points, for paragraph elements.

NESTING_LEVEL Enum The item nesting level, for list item elements.

MINIMUM_HEIGHT Enum The minimum height setting in points, for table row elements.

PADDING_BOTTOM Enum The bottom padding setting in points, for table cell elements.

PADDING_LEFT Enum The left padding setting in points, for table cell elements.

PADDING_RIGHT Enum The right padding setting in points, for table cell elements.

PADDING_TOP Enum The top padding setting in points, for table cell elements.

PAGE_HEIGHT Enum The page height setting in points, for documents.

PAGE_WIDTH Enum The page width setting in points, for documents.

SPACING_AFTER Enum The bottom spacing setting in points, for paragraph elements.

SPACING_BEFORE Enum The top spacing setting in points, for paragraph elements.

STRIKETHROUGH Enum The strike-through setting, for rich text.

UNDERLINE Enum The underline setting, for rich text.

VERTICAL_ALIGNMENT Enum The vertical alignment setting, for table cell elements.

WIDTH Enum The width setting, for table cell and image elements.

ตวัต อยาล่ ง

https://developers.google.com/apps-script/reference/document/attribute

โค้ชดตล่อไปน้ีน เม่ืสอนึ รตนั จะพิธมพข์ร อช้ ความ Hello ไว้ชทจน่ึ ดชุ แรกของเอกสาร Google Docs

เรยรี บเรยรี งโดย วสันล ต์ล คุณค ดลิด กเศวต ([email protected]) 29

// Define a style with yellow background.
var highlightStyle = {} ;
highlightStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#FFFF00'
highlightStyle[DocumentApp.Attribute.BOLD] = true ;

// Insert "Hello", highlighted.
DocumentApp.getActiveDocument().editAsText()

.insertText(0, 'Hello\n')
.setAttributes(0, 4, highlightStyle);

ผล

2.7. setAttributes() คลาส Paragraph

setAttributes(attributes) – Method ในคลาส Paragraph และหลายคลาส
https://developers.google.com/apps-script/reference/document/paragraph#setattributesattributes

ใชกช้ าส หนด Attributes ใหก้ช ับต Element ในทนึ่นนี้กค็ก อืส Paragraph โดยจะคืนส คล่ากลบตั มาเป็นป ยอ่ล หน้าช ตวัต ทจน่ึ บัต
มา เพนยงแตล่ Attributes เปลนึ่ยนไป พารามธิเตอรร์ attributes เปป็น Object[] หรืสอ เป็ปนตวตั แปรวตัตถชุท่ึน Properties
ตา่ล งๆตาม Enum Attribute

ตวัต อยา่ล ง

function setAttsPara(){
var body = DocumentApp.getActiveDocument().getBody();
// เตรียร มตวลั แปร Object วม่างๆรอไวช้
var style = {} ;
// ปอ้ป น Item ทีเ่ทร ปน็ป Propertiy : Value ให้ชกบลั ตลัวแปร Object ที่เรท ตรรยี มไวช้
style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =

DocumentApp.HorizontalAlignment.RIGHT ;
style[DocumentApp.Attribute.FONT_FAMILY] = 'Sarabun' ;
style[DocumentApp.Attribute.FONT_SIZE] = 12 ;
style[DocumentApp.Attribute.BOLD] = false ;
style[DocumentApp.Attribute.FOREGROUND_COLOR] = '#0000FF' ;
// แนบทาช้ ยดวช้ ยยอ่ม หน้ชา para ลงใน Body ของเอกสาร
var par = body.appendParagraph('A paragraph with custom style.');
// กาต หนด Attributes ใหก้ช บัล ยอม่ หนาช้ para
body.setAttributes(style) ;
}

30 Google Apps Script เซอร์พวสพิ Doucument

ผลหลังต รตันโค้ดช

เรรียบเรียร งโดย วสันล ตล์ คุณค ดิลด กเศวต ([email protected]) 31

32 Google Apps Script เซอรพ์วิสพ Doucument

บททีท่ 3
Heading

3.1. setHeading()

คลาส Paragraph
https://developers.google.com/apps-script/reference/document/paragraph
setHeading(heading) – Method ในคลาส Paragraph
https://developers.google.com/apps-script/reference/document/paragraph#setheadingheading

กาส หนดสไตลร์ยล่อหนช้ามาตรฐานให้ชกัตบยล่อหน้ชา โดยจะคืสนคล่ากลัตบมาเป็ปนย่ลอหน้ชาปัปจจุชบัตน พารามธิเตอร์ร
heading ดไผ ดจช้ าก Enum ParagraphHeading (ดยเพมทด่ิี เตมิด ขอ้ช 3.2 Enum ParagraphHeading หนช้า 35)

getHeading() – Method ในคลาส Paragraph
https://developers.google.com/apps-script/reference/document/paragraph#getheading

ใชจ้ช ัตบไสตลย์ร ล่อหนา้ช มาตรฐานของย่อล หนช้า โดยคสืนคล่ากลบัต มาเปป็น Property ใน Enum

ParagraphHeading (ดเย พิมที่ด เตมดิ ข้อช 3.2 Enum ParagraphHeading หนาช้ 35)

ตวตั อยา่ล ง

function example() {
var body = DocumentApp.getActiveDocument().getBody() ;
body.clear() ;
// แนบทา้ช ยด้ชวยยอ่ม หนา้ช + กตาหนดสไตลล์ย่มอหนา้ช เปป็น Heading 1
var par1 = body.appendParagraph("Title") ;
par1.setHeading(DocumentApp.ParagraphHeading.HEADING1) ;
// แนบทา้ช ยดช้วยยอม่ หน้ชา + กาต หนดสไตลยล์ มอ่ หนช้าเปป็น Heading 2
var par2 = body.appendParagraph("SubTitle") ;
par2.setHeading(DocumentApp.ParagraphHeading.HEADING2) ;
// แนบทา้ช ยด้วช ยยอม่ หนา้ช + กาต หนดสไตลลย์ อ่ม หนา้ช เปนป็ Nornal text
var par3 = body.appendParagraph("Text") ;
par3.setHeading(DocumentApp.ParagraphHeading.NORMAL) ;

}

ผลหลตงั รันต โคดช้

34 Google Apps Script เซอรวพ์ ิสพ Doucument

3.2. Enum ParagraphHeading

Enum ParagraphHeading

https://developers.google.com/apps-script/reference/document/paragraph-heading

แจงแจงคชุณสมบตตั ิธสไตลย์ร ่ลอหนาช้ มาตรฐาน
การเรนยกใชง้ช าน เช่นล DocumentApp.ParagraphHeading.Property

Property Type Description
NORMAL Enum The heading option for normal text.
HEADING1 Enum The highest heading option.
HEADING2 Enum The second heading option.
HEADING3 Enum The third heading option
HEADING4 Enum The fourth heading option.
HEADING5 Enum The fifth heading option.
HEADING6 Enum The lowest heading option.
TITLE Enum The title heading option.
SUBTITLE Enum The subtitle heading option.

3.3. setHeadingAttributes()

setHeadingAttributes(paragraphHeading, attributes) – Method ในคลาส Body

https://developers.google.com/apps-script/reference/document/body#setHeadingAttributes(ParagraphHeading,Object)

ใช้ชกสาหนด Attributes ใหก้ช ัตบสไตลร์ยอล่ หน้ชามาตรฐาน โดยจะคืสนคา่ล กลตบั มาเป็ปนคลาส Body ตวตั เดมธิ

พารามิธเตอรร์ – Method ในคลาส Body

Name Type Description
paragraphHeading
ParagraphHeading the heading whose attributes will be set
attributes ดมขู อ้ข 3.2 Enum ParagraphHeading หน้ขา 35

Object a map of attributes and the values to set them to
ดมขู อ้ข 2.6 Enum Attribute หนา้ข 28

getHeadingAttributes(paragraphHeading)

https://developers.google.com/apps-script/reference/document/body#getheadingattributesparagraphheading

ใช้ชจตับ Attributes ของสไตลร์ย่ลอหนช้ามาตรฐาน โดยคสืนค่ลากลตับมาเปป็น Object ทน่ึมนสามาชิธกเป็ปน Enum
Attributes และ คา่ล ตา่ล งๆ พารามิเธ ตอรร์ paragraphHeading ก็คก ือส Enum paragraphHeading

การใชง้ข านเชต่น

body.getHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1)

เรรียบเรียร งโดย วสัลนต์ล คคณุ ดิลด กเศวต ([email protected]) 35

ผลลพตั ธ์ถร ้ชา Log ดเผ ช่นล

Logs
[ 01 ] {FONT_SIZE=24, ITALIC=false, HORIZONTAL_ALIGNMENT=Left,
INDENT_END=0.0, INDENT_START=0.0, LINE_SPACING=1.15, UNDERLINE=false,
BACKGROUND_COLOR=#ffff00, INDENT_FIRST_LINE=0.0, SPACING_BEFORE=20.0,
SPACING_AFTER=6.0, STRIKETHROUGH=false, FOREGROUND_COLOR=#00ff00,
BOLD=false, FONT_FAMILY=Sarabun, VERTICAL_ALIGNMENT=Center}

ตวตั อยา่ล ง

function HeadingAttributes() {
var myHeading1 = {} ;
myHeading1[DocumentApp.Attribute.FONT_SIZE] = 24 ;
myHeading1[DocumentApp.Attribute.FONT_FAMILY] = "Sarabun" ;
var myHeading2 = {} ;
myHeading2[DocumentApp.Attribute.FONT_SIZE] = 16 ;
myHeading2[DocumentApp.Attribute.FONT_FAMILY] = "Sarabun" ;
myHeading2[DocumentApp.Attribute.FOREGROUND_COLOR] = "#555555" ;
var myNormal = {} ;
myNormal[DocumentApp.Attribute.FONT_SIZE] = 12 ;
myNormal[DocumentApp.Attribute.FONT_FAMILY] = "Sarabun" ;
myNormal[DocumentApp.Attribute.FOREGROUND_COLOR] = "#505050" ;
myNormal[DocumentApp.Attribute.SPACING_BEFORE] = 5 ;
myNormal[DocumentApp.Attribute.SPACING_AFTER] = 0 ;
var body = DocumentApp.getActiveDocument().getBody() ;
body.setHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1, myHeading1)
.setHeadingAttributes(DocumentApp.ParagraphHeading.HEADING2, myHeading2)
.setHeadingAttributes(DocumentApp.ParagraphHeading.NORMAL, myNormal) ;

}

36 Google Apps Script เซอรว์พ สิพ Doucument

บททที่ 4
Elements

4.1. Elements

Elements คอน อะไร ?
เอกสาร Google Docs ประกอบไปด้ชวย Elements(วตร ถบุธาต)ุบ ตาล่ งๆมากมาย เช่นล ListItem,
Paragraph และ Table เป็ปนตนช้ ซึน่งทุชกตตัวกค็ก ือส Elements ภายในเอกสาร

Elements ลกกู หรอน Child elements
เม่สนึือเราจตับไปทน่ึ Element สตักตัตวหนนึ่งในเอกสาร Element ตัวต นตั้ีนนจะมน Elements ย่ลอยๆขช้างในอนก เชล่น
เมืสนึ่อจตับทึ่น Body ลผกๆของ Body กก็คืสอ Paragraph, ListITem, Table, TableOdContents เปป็นต้ชน ซ่ึนงเราเรนยกว่ลา
Elements ย่อล ยๆขา้ช งในนวนี้ าล่ Elements ลกมู หรือส Child elements

4.2. Interface Element

Interface Element – Interface ของเซอร์วล ิสด Document
https://developers.google.com/apps-script/reference/document/element

Elements ตาล่ งๆมน Methods กลลมชุ่ หนนงึ่ ทน่ึซาส้ีน กตัน ซงนึ่ เราสามารถจับต มามตดั รวมกนตั (Inherit หรือส สบืส ทอดส่งนิธึ
ทึ่นซ้ีนสากันต ออกมา) กลายเปนป็ Interface Element หรืสอ เป็ปนกลต่มุบ Methods

Methods ใน Interface Element ยกตวตั อยาล่ งเช่ลน getAttributes(), Method ตระกผล as เชนล่
asParagraph() เปนป็ ต้นช

หมายเหตุบ 1 : มสน ิึ่ธนงทึน่คลชา้ ยกตันกค็ก ืสอ คลาส Element(ตามลิธงคด์ร ้ชานลา่ล ง) แตล่เปนป็ วัตตถุชหรอสื คลาสในเซอร์รวธิส
XML

เซอรพว์ ิพส XML
https://developers.google.com/apps-script/reference/xml-service

เปน็ป บริกธ ารทนึใ่ ชช้ วิเธ คราะหร์ สาส รวจ และโปรแกรม ในการสราช้ งเอกสาร XML

คลาส Element (ในเซอรว์ล ิสด XML)
https://developers.google.com/apps-script/reference/xml-service/element

เปน็ป XML ของ Element node

หมายเหตุบ 2 : ภาษา XML มโน ครงสรา้ช งเหมืสอนกบัต HTML มนแทก็กเปดปิ แทกก็ ปดปิ แตล่ XML เราสามารถ
สรา้ช งแท็กก และกสาหนดโครงสร้ชางของข้อช มลผ ไดเช้ อง ตา่ล งจาก HTML ทนึแ่ ท็กกถกผ กหนดตายตัตว ตวตั อยาล่ ง

XML Tutorial
https://www.w3schools.com/xml/

<?xml version="1.0" encoding="UTF-8"?>
<note>

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

แตล่ล ะแทก็ก ทึเน่ ราสร้ชางเรนยกวล่า โหนด(node) เชน่ล โหนด Note เปน็ป ตน้ช

38 Google Apps Script เซอรพว์ พิส Doucument

ยกตัธวอย่สาง การดกชู นิวดของ Elements ใน Body
องคป์ร ระกอบในสว่ล น Body ในเอกสาร Google Docs มตน ามภาพ

โคช้ดต่ลอไปนีน้ จบตั Elements ใน Body ของเอกสาร Log ดวผ า่ล มชน นดธิ เปปน็ อะไรบา้ช ง

function testElem() {

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

for(var x = 0 ; x < elemInBody ; x++){ // วนหลปย Log ชนดิด Elements ทรท่จี บัล มา
Logger.log(body.getChild(x).getType()) ;

};
}

ผล

[ 01 ] PARAGRAPH // ยอ่ม หนช้าวมา่ ง
[ 02 ] PARAGRAPH
[ 03 ] TABLE
[ 04 ] LIST_ITEM
[ 05 ] LIST_ITEM
[ 06 ] LIST_ITEM
[ 07 ] PARAGRAPH

4.3. getNumChildren()

getNumChildren() - Method ในคลาส ContainerElement

https://developers.google.com/apps-script/reference/document/container-element#getnumchildren

ใช้จช ตับจสานวนของ Elements ลมกู โดยจะคสนื คาล่ กลตับมาเป็ปนเลขจสานวนเต็มก

คลาส ContainerElement
https://developers.google.com/apps-script/reference/document/container-element

Element ทนึเ่ ปปน็ ตวตั เกก็บ Elements อึ่สนืนๆ (เรยน กวาล่ Element บรรจุภช ณัต ฑกร์ กไ็ ด)้ช

เรีรยบเรียร งโดย วสนลั ตล์ คณุค ดลิด กเศวต ([email protected]) 39

ตวธั อย่สาง
ให้ใช ส่อล ะไรลงไปในเอกสาร Google Docs บา้ช งจากน้นีตัน จนงรตันโคด้ช ต่ลอไปนี้น

var body = DocumentApp.getActiveDocument().getBody();
var elemInBody = body.getNumChildren();
// ถ้ชา Log ดจย ะได้ผช ลลัลพธลเ์ ข่นม 7 (เปน็ป จาต นวนของ Elements) เปนป็ ตน้ช

4.4. จับจ และดกูชนิวดของ Elements (คลาส Body)

getChild() เปน็ป Method ในหลายคลาส หลกัต ๆกค็ก ือส ใช้ชจตบั Child elements เชล่น

getChild(name) – Method ในคลาส Element
https://developers.google.com/apps-script/reference/xml-service/element#getchildname

getChild(name, namespace) – Method ในคลาส Element
https://developers.google.com/apps-script/reference/xml-service/element#getchildname,-namespace

getChild(childIndex) – Method ในคลาส Paragraph
https://developers.google.com/apps-script/reference/document/paragraph#getchildchildindex

getChild(childIndex) – Method ในคลาส Body ***
https://developers.google.com/apps-script/reference/document/body#getchildchildindex

ใชช้จตับ Child elements ใน Body โดยคสืนค่ลากลตับมาเป็ปน คลาส Element พารามธิเตอรร์ childIndex
เปน็ป เลขดรรชลน าส ดตับ (เรธ่มึนิ จาก 0

getType() – Method ในคลาส Body
https://developers.google.com/apps-script/reference/document/body#gettype
ใชชจ้ ับล ชนิดด ของ Elements โดยจะคคนื คาม่ กลลับมาเปนป็ Enum ElementType

ตวตั อยาล่ ง

function getChildElems() {

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

// จบัล ไปท่รีท Child element ตัลวแรกใน Body
var firstChild = body.getChild(0) ;

// ถชา้ Element เปน็ป ย่มอหนช้า(PARAGRAPH) ให้ใช สข่ม ชอ้ ความ … ลงไปแทน
if (firstChild.getType() == DocumentApp.ElementType.PARAGRAPH) {

firstChild.asParagraph().setText("This is the first paragraph.") ;
}
}

ผล หลงร รนร

กอ่ต นรนร

40 Google Apps Script เซอร์พวิพส Doucument

4.5. Enum ElementType

Enum ElementType

https://developers.google.com/apps-script/reference/document/element-type

เป็นป ตัวต แจกแจงชนดิธ ของ Elements การใชง้ช านกค็ก สอื DocumentApp.ElementType.(Property)

Property Type Description

BODY_SECTION Enum The type corresponding to the Body element.

COMMENT_SECTION Enum The type corresponding to the CommentSection element.

DOCUMENT Enum The type that corresponds to the root of the document.

EQUATION Enum The type corresponding to the Equation element.

EQUATION_FUNCTION Enum The type corresponding to the EquationFunction element.

EQUATION_FUNCTION_ Enum The type corresponding to the element.
ARGUMENT_SEPARATOR EquationFunctionArgumentSeparator

EQUATION_SYMBOL Enum The type corresponding to the EquationSymbol element.

FOOTER_SECTION Enum The type corresponding to the FooterSection element.

FOOTNOTE Enum The type corresponding to the Footnoteelement.

FOOTNOTE_SECTION Enum The type corresponding to the FootnoteSection element.

HEADER_SECTION Enum The type corresponding to the HeaderSection element.

HORIZONTAL_RULE Enum The type corresponding to the HorizontalRule element.

INLINE_DRAWING Enum The type corresponding to the InlineDrawing element.

INLINE_IMAGE Enum The type corresponding to the InlineImage element.

LIST_ITEM Enum The type corresponding to the ListItem element.

PAGE_BREAK Enum The type corresponding to the PageBreak element.

PARAGRAPH Enum The type corresponding to the Paragraph element.

TABLE Enum The type corresponding to the Table element.

TABLE_CELL Enum The type corresponding to the TableCell element.

TABLE_OF_CONTENTS Enum The type corresponding to the TableOfContents element.

TABLE_ROW Enum The type corresponding to the TableRow element.

TEXT Enum The type corresponding to the Text element.

UNSUPPORTED Enum The type corresponding to UnsupportedElement. Unsupported
elements represent document portions that do not support
scripting.

เรยีร บเรีรยงโดย วสลันตล์ คคณุ ดลิด กเศวต ([email protected]) 41

4.6. ระบุชบ นวดิ ELement ด้รวย Method ตระกูกล As

เม่ึสืนอจัตบ Child elements แลช้ว หากจะเขช้าไปทสาอะไรตล่อ เราต้ชองระบุช Element ตัตวน้ีนัตนตามชนิธดด้ชวย
Method ตระกผล as เช่ลน asParagraph(), asTable() หรืสอ asListItem() เป็ปนตช้น จากน้ีัตนนเราจะสามารถใชช้
Method ทตึ่น รงกบัต ชนธิดของ Element เพ่ึสือน ทสาอะไรตอล่ กว็ก ่ลาไป

asParagraph() - Method ใน Interface Element
https://developers.google.com/apps-script/reference/document/element#asparagraph

คืนส คาล่ Element ทจน่ึ บัต มาได้เช ปน็ป Paragraph

เชนล่

function getChildElems() {

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

for(var x = 0 ; x < childsCount ; x++) { // วนหลปย จบลั ทรลี ะ Element ไปใส่ม if/else
var child = body.getChild(x).copy() ;

// ถาข้ เป็ปนย่อต หนข้า + ยอ่ต หน้ขาไมต่วต่าง

if ( child.getType() == DocumentApp.ElementType.PARAGRAPH &&

child.asParagraph().getText() != "") {

var para = child.asParagraph() ; // ระบบุ Element เปนป็ Paragraph

body.appendParagraph(para).appendText('(ตรวก๊กอปปป)') ;

// ถข้าเปน็ป ตาราง

} else if ( child.getType() == DocumentApp.ElementType.TABLE) {

var table = child.asTable() ; // ระบบุ Element เปปน็ Table

var rowAdd = table.getRow(0).copy() ; // กอก็ ปป้ีปแถวแรก

body.appendTable(table).appendTableRow(rowAdd) ; // แนบแถวแรกทีรทก่ กอ๊ ปปปี้

} // Close – else if

} // Close – for

} // Close - function

ผล หลรง

กอต่ น

42 Google Apps Script เซอรพ์วสพิ Doucument

ผลหลตังรนัต

Logs

[ 01 ] PARAGRAPH
[ 02 ] TABLE
[ 03 ] LIST_ITEM
[ 04 ] LIST_ITEM
[ 05 ] LIST_ITEM
[ 06 ] PARAGRAPH

หมายเหตบุ
ดรผ ายละเอนยดเพิ่นมธึ เตมธิ ไดจ้ช ากลธิงคตร์ ลอ่ ไปนีน้

getChild – Method ในคลาส ContainerElement/Element

https://developers.google.com/apps-script/reference/document/container-element#getchildchildindex

https://developers.google.com/apps-script/reference/xml-service/element#getchildname

ใช้ชจบัต Element ตามดรรชนนลสาดบัต เชน่ล getChild(0) เปปน็ ต้ชน คสนื ค่ลากลัตบมาเปน็ป Element

getNumChildren() – Method ในคลาส ContainerElement

https://developers.google.com/apps-script/reference/document/container-element#getnumchildren

ใช้จช ตบั จาส นวน Elements คนสื คาล่ กลัตบมาเป็นป เลขจาส นวนเตม็ก

4.7. findElement()

findElement(elementType, from) – Mehod ในหลายคลาส Body และอีรกหลายคลาส

https://developers.google.com/apps-script/reference/document/list-item.html#findelementelementtype,-from

ใชชค้ น้ช หาเนสืน้อี หาใน Element แบบเจาะจงชนธิด โดยจะคืสนค่าล กลตบั มาเปป็นคลาส RangeElement

พารามิธเตอรร์

Name Type Description

elementType ElementType the type of element to search for

from RangeElement the search result to search from

เรยีร บเรรยี งโดย วสลนั ตล์ คุคณดลิด กเศวต ([email protected]) 43

ตวัต อยาล่ ง

function example() {
var body = DocumentApp.getActiveDocument().getBody() ;
// สรา้ช งตัวล แปร
var searchType = DocumentApp.ElementType.PARAGRAPH ;
var searchHeading = DocumentApp.ParagraphHeading.HEADING1 ;
var searchResult = null ;
// Search until the paragraph is found.
while (searchResult = body.findElement(searchType, searchResult)) {
var par = searchResult.getElement().asParagraph() ;
if (par.getHeading() == searchHeading) {
// Found one, update and stop.
par.setText('This is the first header.');
return ;
}
}

}

ผล - หลัตงรตันโคช้ดย่ลอหน้ชาแรกท่ึนใช้ชสไตล์รยล่อหนช้า Heading1 ข้ชอความจะถผกเปลึ่นยนเปป็น This is the first

header

44 Google Apps Script เซอรพ์วพสิ Doucument

บททที่ 5
เคอรเ์ร ซอร์ร

5.1. getCursor()

getCursor()– Method ในคลาส Document

https://developers.google.com/apps-script/reference/document/document#getcursor

ใช้ชจัตบเคอร์รเซอรใร์ นเอกสารทน่กึ าส ลตังทสางานดว้ช ย โดยจัตบได้ชเฉพาะของยผสเซอรร์ทน่ึรัตนสครธิปตร์ และเป็ปนสครธิปต์ร
ฝัปงในไฟล์ร (ถผกหลอ่ หชุ้ม หรสือ bound ← คลดิกเพอ่คทีื ดยรายละเอรียด) โดยจะจะคืสนคล่ากลัตบมาเป็ปนคลาส Position แตล่ถช้า
จตับไมล่ไดจ้ช ตคั สืนคา่ล กลบตั มาเปปน็ null

คลาส Position
https://developers.google.com/apps-script/reference/document/position.html

ตาส แหน่ลงอา้ช งองธิ ในเอกสาร Google Docs เปป็นแบบอ้าช งอธิงจาก Element

ตวัธ อยา่ส ง : แทรกขร้อความ ณ ตตาแหน่งส เคอร์รเซอร์ร

โคด้ช ต่ลอไปนี้น รัตนจากโปรเจ็กค Google Apps Script จะคาช้ ง วธิธ รน ตันตช้องสรา้ช งเมนผ แลช้วรันต จากเมนผ ทัตน้ีงนน้ีเพนืสึ่อ
ท่ึนจะจตับตสาแหนล่งของเคอรร์เซอร์รไดช้ ถช้ารตันจาก Google Apps Script เคอร์รเซอร์รไม่ลแอ็กคทนฟทน่ึเอกสาร Google
Docs

กล่อนรตัน ให้รคลวิกแทรกในย่สอหน้ราหรนอขร้อความไวร้ จากนี้ัตนนจนงรัตนสคริธปตร์จากเมนผ แต่สถ้ราคลวิกเลนอกไว้รท่ขีอื
ภาพ จะปรากฎหนช้าต่ลางมาแจช้งวล่า Cannot insert text here ก็กคสือ ไม่ลสามารถแทรกขช้อความไดช้ (เพราะไม่ลมน
เคอรร์เซอร)ร์

function onOpen() {

DocumentApp.getUi().createMenu('My Menu')
.addItem('Insert someting at cursor' , 'insertAtCursor')
.addToUi() ;

}

function insertAtCursor(){

// จบัล ตาต แหนมง่ ของเคอร์ลเซอรล์ ไดกช้ ลบัล มาเปปน็ คลาส Postion
var cursor = DocumentApp.getActiveDocument().getCursor() ;

// ถ้ชาจบลั เซอร์ไล ดช้ - แทรกขอช้ ความ ณ ตตาแหน่มงของเคอรล์เซอรล์

if (cursor) { // if/else #1
var element = cursor.insertText('ಠ‿ಠ') ;
if (element) {
// if/else #2

element.setBold(true); // ทตาตลัวหนา

} else {

DocumentApp.getUi().alert('Cannot insert text here.');

} // Close - if/else #2

// ถชา้ จบัล เคอร์ลเซอร์ลไมม่ได้ช - แสดงหน้าช ตา่ม งแจชง้ Error // Close - if/else #1
} else {
DocumentApp.getUi().alert('Cannot find a cursor.');
}
}

46 Google Apps Script เซอร์พวสพิ Doucument

5.2. setCursor(), newPosition()

setCursor(position) – Method ในคลาส Document
https://developers.google.com/apps-script/reference/document/document#setcursorposition

ใชช้กสาหนดตสาแหนง่ล ของเคอรร์เซอร์ร ณ ตาส แหนง่ล ท่นรึ ะบุชใน postion (เป็ปน New postion)

newPosition(element, offset) – Method ในคลาส Document

https://developers.google.com/apps-script/reference/document/document#newpositionelement,-offset

สรา้ช งวัตตถชุ Position ตวร ใหมต่ โดยอ้าช งอธิงจากจาก Element ในเอกสาร

Name Type Description
element Element
Element ทจ่ีทร ะใช้บช รรจุควลัตถคุ Postion ตวร ใหมต่ ซข่งที สามารถเป็ปนได้ชท้ัึขงล Text
offset Integer หรคือ Paragraph

สสาหรบร Element ทเที่ ปปน็ Text เปนป็ ตวลั เลขจาต นวนตลวั อกัล ษรก่มอนตาต แหนม่ง
สสาหรรบ Elements อนีรทื่ ๆ เปปน็ ตลัวเลขจาต นวน Child elements กอม่ น
ตาต แหน่งม ภายในวัลตถบคุ รรจุคภัลณฑล์ตวลั เดยรี วกนัล

ตวตั อยาล่ ง

function setCurAtNewPos() {

var doc = DocumentApp.getActiveDocument() ;
var paragraph = doc.getBody().appendParagraph('My new paragraph.') ;

// สร้าช งวัลตถคุ Postion ณ ตตาแหนงม่ หลงัล ตัลวอกลั ษรทท่รี 6 ของยอม่ หนา้ช
var position = doc.newPosition(paragraph.getChild(0), 6) ;

// กาต หนดตาต แหนง่ม ใหช้กบัล เคอร์เล ซอร์ล
doc.setCursor(position) ;
}

ผล – มยน ล่อหนาช้ ตอ่ล ทาช้ ยเอกสาร โดยเคอรร์เซอรจร์ ะปรากฎอย่ลหผ ลังต ตวตั อตักษรทน่ึ 6 (หลงตั w)

เรยีร บเรีรยงโดย วสลันต์ล คุณค ดิดลกเศวต ([email protected]) 47

48 Google Apps Script เซอรพ์วิสพ Doucument

บททที่ 6
Selection

6.1. getSelection(), getRangeElements(), getElement()

getSelection() - Method ในคลาส Document
https://developers.google.com/apps-script/reference/document/document#getselection

จตับสึธิน่งทน่ึเลืสอกโดยยผสเซอรร์ ในเอกสารทนึ่แอ็กคทฟน อยลผ่ คสืนค่าล กลัตบมาเปป็นคลาส Range แตล่ถ้ชาไม่ลเลืสอกอะไรไวช้
จะคสนื คลา่ null

คลาส Range
https://developers.google.com/apps-script/reference/document/range.html

เรนช้ จร์ของ Element ในเอกสาร โดยส่นิงธึ ทนึ่ยสผ เซอรเ์ร ลอืส กกก็คอสื Range

getRangeElements() - Method ในคลาส Range
https://developers.google.com/apps-script/reference/document/range.html#getrangeelements

ใชช้จัตบ Elements ทนี้ัตงหมดใน Range (Element ท่นึถผกเลสือก) แม้ชจะเปป็นบางสล่วนของ Element ทนึ่ถผก
เลสือก ยกตตัวอยาล่ งการเลืสอกบางส่ลวนของย่ลอหนช้า โดย Method นนี้ จะคืสนค่ลากลัตบมาเป็ปน RangeElement[] หรืรอ

อารเยท์พ ี่ทบรรจไุบ ปด้ขวย คลาส RangeElement

คลาส RangeElement
https://developers.google.com/apps-script/reference/document/range-element.html

ตวัต ห่อล Element ทถ่ึน กผ เลสือก ตงตันี้ แตลจ่ ดุช เร่มนธึิ และจบครอบ Element เชลน่ ถช้าเลือส ก 1 ตวัต อกัต ษรในย่อล หนช้า
RangeElement กกค็ อืส ยอล่ หนช้าทังีต้น กอ้ช น

getElement() - Method ในคลาส RangeElement
https://developers.google.com/apps-script/reference/document/range-element#getelement

ใชจช้ ตับ Element ในวตตั ถุช RangeElement

isPartial() - Method ในคลาส RangeElement
https://developers.google.com/apps-script/reference/document/range-element.html#ispartial

ใชเช้ ช็คก วา่ล RangeElement ทน่เึ ลอืส ก ถกผ เลอืส กท้ัีตนงก้ชอน หรืสอบางสวล่ นของขอช้ ความ(Text element) โดยจะ
คนสื คาล่ กลัตบมาเปน็ป Boolean จดชุ เร่ิมนึธ ตน้ช และจุดช จบของ RangeElement สามารถจตบั ไดชโ้ ดยใช้ช Method
getStartOffset() และ getEndOffsetInclusive() ตามลสาดัตบ

getStartOffset() - Method ในคลาส RangeElement

https://developers.google.com/apps-script/reference/document/range-element.html#getStartOffset()

ตสาแหน่ลงจุชดเรนธ่ิึมตช้นของ RangeElement (Text element) อธธิบายไว้ชขช้างบน Method นี้นคืสนคล่ากลัตบมา
เปนป็ เลขจสานวนเต็มก ของตาส แหนล่ง และคนสื ค่ลากลตับมาเปน็ป -1 หากเปป็นอยาล่ งอนนึ่สื

getEndOffsetInclusive() - Method ในคลาส RangeElement

https://developers.google.com/apps-script/reference/document/range-element.html#getEndOffsetInclusive()

ตสาแหนง่ล จุชดจบของ RangeElement (Text element) อธิธบายไวข้ช าช้ งบน Method นคนี้ สนื ค่าล กลตบั มาเปปน็
เลขจสานวนเตกม็ ของตสาแหนล่ง และคืสนค่าล กลตับมาเปน็ป -1 หากเป็ปนอยล่างอสืึ่นน

50 Google Apps Script เซอรว์พ ิสพ Doucument


Click to View FlipBook Version