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

ตวธั อยาส่ งท่ือีข 1

function catchElem() {

// กมอ่ นรนัล โคช้ดใหช้เลคือก ไว้สช ลักตวัล อลักษรหนีทง่ข ในยอม่ หน้าช เปน็ป อยา่ม งนชอ้ ย หรืคอ เลอืค กตารางกไ็ก ด้ช

var selection = DocumentApp.getActiveDocument().getSelection();

// Logger.log(selection); // พมดิ พล์ : Range

var rangeElems = selection.getRangeElements() ;

// Logger.log(rangeElems); // พมดิ พ์ล : [RangeElement, RangeElement]

// ( กรณีรเลืคอกบางส่มวนของ 2 ยอม่ หนช้าไว้ช )

var elemInRangeElems = rangeElems[0].getElement().getType() ;
// Logger.log(elemInRangeElems); // พมดิ พล์ชนดดิ ของ Element

// เชมน่ TEXT / TABLE_CELL

var text = selection.getRangeElements()[0].getElement().getText();

Logger.log(text); // พมิด พ์ล : ขอ้ช ความทัง้ึขล ยมอ่ หนา้ช ทีท่รถยกเลือค กอยย่ม

}

กลอ่ นรันต โคด้ช เลสือกข้ชอความบางสลว่ นในยอ่ล หนช้าไว้ช

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

Logs

[ 01 ] Range

[ 02 ] [RangeElement]

[ 03 ] TEXT // ถ้าช เลอืค กตารางไว้ช ขนขึ้ เปปน็ TABLE_CELL

[ 04 ] ยอต่ หนข้านั้รท ใชขเ้ ป็ปนตวร อยตา่ งสสาหรรบอธบพิ าย Google Apps Script เซอรวพ์ สิพ Document

ตวัธ อยาส่ งท่ืีขอ 2

function Example2() {
var selection = DocumentApp.getActiveDocument().getSelection();
var Elements = selection.getRangeElements();
Logger.log(Elements);

Elements.forEach(function(element,i){
Logger.log(element+' '+i+' type : ' +element.getElement().getType());

});
}

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

ผล – กอ่ล นรันต โคด้ช เลอสื กไวช้หลายอยาล่ ง ทงตั้ีน ตารางและยล่อหนช้า

Logs
[ 01 ] [RangeElement, RangeElement, RangeElement, RangeElement,
RangeElement, RangeElement, RangeElement, RangeElement, RangeElement]
[ 02 ] RangeElement 0 type : PARAGRAPH
[ 03 ] RangeElement 1 type : TABLE
[ 04 ] RangeElement 2 type : PARAGRAPH
[ 05 ] RangeElement 3 type : TABLE
[ 06 ] RangeElement 4 type : PARAGRAPH
[ 07 ] RangeElement 5 type : TABLE
[ 08 ] RangeElement 6 type : PARAGRAPH
[ 09 ] RangeElement 7 type : LIST_ITEM
[ 10 ] RangeElement 8 type : LIST_ITEM

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

ใชจช้ ตับชนิธดของ ELement คสนื คา่ล กลับต มาเป็นป Enum ElementType

Enum ElementType – แจกแจงชนดิด ของ Elements
https://developers.google.com/apps-script/reference/document/element-type.html

ตวธั อย่าส งท่ขีอื 3

function Example3() {
var selection = DocumentApp.getActiveDocument().getSelection() ;
// จบลั Element ตัลวแรกทีทเ่ร ลอคื ก
var rangeElement = selection.getRangeElements()[0] ;
Logger.log('Element type: ' + rangeElement.getElement().getType());
// ถชา้ ถยกเลอคื กบางสว่ม น
if (rangeElement.isPartial()) {
Logger.log('The character range begins at '+ rangeElement.getStartOffset());
Logger.log('The character range ends at '+ rangeElement.getEndOffsetInclusive());
// ถา้ช ถยกเลคือกทึข้ลัง Element
} else {
Logger.log('The entire range element is included.');

}

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

รนัธ ครธัง้นื ทอ่ืขี 1 : กลอ่ นรันต โคด้ช เลสือกข้ชอความบางส่ลวนในย่ลอหนช้าไว้ช

ผลการรนตั ครตันี้งทน่ึ 1

[ 01 ] Element type: TEXT
[ 02 ] The character range begins at 18
[ 03 ] The character range ends at 25

รนัธ ครง้ืธัน ทีขื่อ 2 : กอ่ล นรนตั โคด้ช เลอืส กบางสวล่ นของเซลลร์ในตารางไวก้ช อ่ล น

ผลการรนัต ครีนั้ตงทึน่ 2

[ 01 ] Element type: TABLE_CELL
[ 02 ] The entire range element is included.

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

6.2. Methods ตระกูลก Insert – ยาร้ ยไป Element

insertParagraph(childIndex, paragraph)

https://developers.google.com/apps-script/reference/document/body#insertparagraphchildindex,-paragraph

แทรกยอ่ล หนา้ช ณ ตสาแหนงล่ ทก่ึน สาหนด

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

Name Type Description
childIndex Integer the index at which to insert
paragraph Paragraph the paragraph to insert

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

deleteText(startOffset, endOffsetInclusive)

https://developers.google.com/apps-script/reference/document/text#deletetextstartoffset,-endoffsetinclusive

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

Name Type Description
startOffset Integer the character offset of the first character to delete
endOffsetInclusive Integer the character offset of the last character to delete

ตวัต อยา่ล ง

function example1() {

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

// แทรก 2 ยมอ่ หนช้า โดยมเีร สช้น 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.
// ลบข้ชอความ ‘n editAsText’
body.editAsText().deleteText(14, 25) ;
}

ผล

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

บทท่ที 7
Text

7.1. คลาส Text

คลาส Text
https://developers.google.com/apps-script/reference/document/text

Element ท่นึบรรจดชุ ช้วย ข้ชอความ หรืสอ Text โดย Text ทกุช ตัตวทน่ึอยผ่ลในเอกสารถผกบรรจุชใน Element ชนิธด
Text

Text เป็นป สลว่ นประกอบเล็กกๆ ภายในองคป์ร ระกอบอึนืสน่ ๆมากมาย เชน่ล เป็นป สล่วนประกอบของ Document,
Equation, ListItem หรอืส Paragraph เป็นป ต้นช แตต่ล ัตวมตันเอง(Text) ไมส่ล ามารถบรรจอชุ ะไรไดเช้ ลย

เราใชร้ Method ของคลาส Text ไดรเ้ ยอะมาก อะไรทีขม่ือ ีข Text ใชร้ได้รหมด

7.2. editAstext(), insertText()

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

แกชไ้ ข Element ตวัต นนั้ีตนในแบบ Text โดยจะคืนส คาล่ กลตบั มาเปป็น Text แตล่ใชไช้ มไล่ ดก้ช บัต Element ประเภท
Non-text อยา่ล ง InlineImage และ HorizontalRule

insertText(text) - Method ใน คลาส Position
https://developers.google.com/apps-script/reference/document/position#inserttexttext
insertText(offset, text) - Method ใน คลาส Text
https://developers.google.com/apps-script/reference/document/text#inserttextoffset,-text

ใช้ชแทรกข้อช ความ ณ ตสาแหนลง่ ท่นึระบุช ซน่ึงเป็นป ระยะออฟเซต็ก ตวัต อตักษร

Name Type Description
offset Integer the character offset at which to insert
text String the text to insert

ตวัต อยา่ล ง

function example() {
var body = DocumentApp.getActiveDocument().getBody() ;

// แกไช้ ข Body แบบ Text
var text = body.editAsText() ;

// แทรก Text ตรงๆ ลงใน Body จะ Error
// body.insertText(0, 'Inserted text.\n') ;

// แทรกขอช้ ความ Inserted text. ททีจ่ร ดคุ แรก (0) + กด Enter
text.insertText(0, 'Inserted text.\n') ;

// แนบทชา้ ยดว้ช ย Text
text.appendText('\nAppended text.') ;

// ระบายสรี เรทิีด่มจาก ตัวล แรก ถงข แบง่ม ครง่ทขี Text ใน Body สรีแดง
text.setForegroundColor(0, text.getText().length / 2, '#FF0000') ;
}

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

ผล

7.3. deleteText()

deleteText(startOffset, endOffsetInclusive) - Method ใน คลาส Text

https://developers.google.com/apps-script/reference/document/text#deletetextstartoffset,-endoffsetinclusive

ใชลช้ บข้ชอความแบบเปน็ป เรช้นจ์ร

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

Name Type Description
startOffset Integer the character offset of the first character to delete
endOffsetInclusive Integer the character offset of the last character to delete

ตวัต อยาล่ ง

function example() {

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

// แทรก 2 ยอ่ม หนช้า ขึัลนข้ โดยเสชย้ Horizontal rule // ยม่อหน้าช ทที่ร 2
body.insertParagraph(0, "An editAsText sample.") ; // เสช้น hr
body.insertHorizontalRule(0) ; // ยอ่ม หน้าช แรก
body.insertParagraph(0, "An example.") ;

// ลตลัวอกัล ษร “n editAsText” ในย่อม หนาช้ ทีรท่ 2
body.editAsText().deleteText(14, 25) ;
}

ผล

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

7.4. replaceText()

Replacing text
https://developers.google.com/apps-script/guides/docs#replacing_text

มาตรฐาน RE2
https://github.com/google/re2

replaceText(searchPattern, replacement) - Method ในคลาส Text

https://developers.google.com/apps-script/reference/document/text#replacetextsearchpattern,-replacement

ใชคช้ น้ช หาและแทนทึ่ขน อ้ช ความ โดยจะคืนส คาล่ กลบัต มาเปปน็ Element เดมิธ
พารามเิธ ตอร์ร

Name Type Description
searchPattern String the regex pattern to search for
replacement String ( ขอ้ช ความธรรมดา หรคือ
Regular Expression มาตรฐาน RE2 )
ตวธั อย่าส ง
ขช้อความททีรจ่ ะมาแทนทที่ร

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

// ล้าช งยอ่ม หนช้าที่มรท ีรขอช้ ความ Apps Script อยดม่ย วช้ ย
// ใหเช้ หลคือเพีรยงขอช้ ความ Apps Script เทาม่ น้นลัึข
body.replaceText("^.*Apps ?Script.*$", "Apps Script");

ตวธั อยาส่ งทือีข่ 2

โคดช้ ต่ลอไปน้ีน ใชค้ช ชน้ หาขช้อความ เช่ลน {name} จากน้ีนันต แทนท่ดนึ ชว้ ยคาล่ ในตวตั แปรแบบวตตั ถชุ เช่นล
instructor.name คาล่ กก็คสอื Wasan Khunnadiloksawet เปน็ป ตน้ช

ตวัต อยาล่ งโคช้ดนนี้ มกัต เอาไปใชทช้ สา MailMerge

// พมิด พล์ย่อม หนช้า สตาหรลับทดสอบแทนทที่รขอช้ ความ
function createParas() {

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

// พมดิ พยล์ ่มอหน้าช ท่ทรมี รีข้อช ความต่าม งๆลงไป รวมแลว้ช มีร 4 ยอ่ม หน้ชา
body.appendParagraph('{name}') ;
body.appendParagraph('{address}') ;
body.appendParagraph('{city} {province}') ;
body.appendParagraph('{zip}') ;

}

// มีรตอ่ม

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

// ตอม่

function Search_n_Replace() {

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

var instructor = { name: 'Wasan Khunnadiloksawet' ,

address: '1 Moo 2 Nongpha' ,

city: 'Nongphai' ,

province: 'Petchaboon' ,

zip: 67140 };

body.replaceText('{name}', instructor.name)
.replaceText('{address}', instructor.address)
.replaceText('{city}', instructor.city)
.replaceText('{province}', instructor.state)
.replaceText('{zip}', instructor.zip) ;

}

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

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

บททีท่ 8
ภาพ

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

Insert Inline Image in a Google Document
https://ctrlq.org/code/20078-insert-image-in-google-document

insertInlineImage(image) – Method ในคลาส Position

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

สรชา้ งและแทรกภาพ InlineImage ณ ตสาแหนง่ล วตัต ถชุ Position พารามเิธ ตอรร์ image ก็คก สอื Blob ของภาพ

insertImage(childIndex, image) – Method ในคลาส Body

สร้ชางและแทรกภาพ InlineImage ณ ตาส แหนง่ล ChildIndex(ตลัวเลขตตาแหนมง่ ของ Child element ใน
Body) พารามธิเตอร์ร image กค็ก ือส Blob ของภาพ insertImage() คืสนคา่ล กลัตบมาเป็นป คลาส InlineImage

ตวตั อยา่ล ง

function insertImage() {

// ภาพจากเวบ็ก
var image = "https://img.labnol.org/logo.png" ;
var blob = UrlFetchApp.fetch(image).getBlob() ;

// ภาพจาก Google Drive
var image = "https://drive.google.com/open?id=11inBoKWzWZr_ip2OvBAv3BWY7TbW9JDt" ;
// จบัล มาเฉพาะ ID ของภาพ โดยใช้ช match ตดัล มาตาม Regular Expression ที่รทร ะบคุ
var fileID = image.match(/[\w\_\-]{25,}/).toString() ;

// [\w\_\-] เฉพาะ ตวร เลขและตวร อกร ษร และ _ และ -
// {25,} ตึล้ัขงแต่มตวัล ท่ีทร 25 เปนป็ ต้นช ไป (นบัล ตัลวที่ทร Match [\w\_\-] )
var blob = DriveApp.getFileById(fileID).getBlob() ;

var doc = DocumentApp.getActiveDocument() ; // จบลั ตาต แหนง่ม ของ Cursor
var cursor = doc.getCursor() ;

if (cursor) { // ถช้ามีร Cursor ในเอกสาร – ใสมภ่ าพ ณ Cursor

cursor.insertInlineImage(blob).setWidth(300).setHeight(168.75) ;

} else { // ถชา้ ไม่มมีร Cursor

doc.getBody().insertImage(0, image).setWidth(300).setHeight(168.75) ;

}

}

ผล

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


Click to View FlipBook Version