หน้านี้จะบอกวิธีจัดรูปแบบข้อความโดยใช้ Google เอกสาร API
เกี่ยวกับการจัดรูปแบบ
มีการจัดรูปแบบที่แตกต่างกัน 2 ประเภทที่คุณสามารถใช้กับเนื้อหาข้อความในเอกสารได้ ดังนี้
- คุณเปลี่ยนการจัดรูปแบบอักขระ เช่น แบบอักษร สี หรือการขีดเส้นใต้ได้
- คุณสามารถเปลี่ยนการจัดรูปแบบย่อหน้า เช่น การเยื้องหรือระยะห่างระหว่างบรรทัด
การเปลี่ยนการจัดรูปแบบอักขระ
การจัดรูปแบบอักขระจะกำหนดการแสดงผลของอักขระที่เป็นข้อความในเอกสารของคุณ
การจัดรูปแบบที่คุณใช้จะลบล้างการจัดรูปแบบเริ่มต้นที่ได้รับมาจาก TextStyle ของย่อหน้าที่เกี่ยวข้อง ในทางกลับกัน อักขระที่ไม่ได้ตั้งค่าการจัดรูปแบบจะยังคงรับค่ามาจากรูปแบบของย่อหน้าต่อไป
หากต้องการเปลี่ยนการจัดรูปแบบอักขระของข้อความ ให้ใช้ batchUpdate
กับ UpdateTextStyleRequest
คุณต้องระบุออบเจ็กต์ Range ที่มีข้อมูลต่อไปนี้
segmentId
ที่ระบุว่าส่วนหัว ส่วนท้าย เชิงอรรถ หรือเนื้อความ (หากไม่ได้ระบุ) ที่มีข้อความดังกล่าวstartIndex
และendIndex
ที่กำหนดช่วงข้อความภายในกลุ่มที่จะจัดรูปแบบ
ตัวอย่างต่อไปนี้ดำเนินการจัดรูปแบบข้อความหลายรายการกับข้อความที่อยู่ในส่วนหัว
- ตั้งค่าแบบอักษรของอักขระ 1-5 เป็นตัวเอียงหนา
- ตั้งค่าสีของอักขระ 6-10 ถึง
blue
ขนาด 14 พอยต์ Times New Roman แบบอักษร - อักขระไฮเปอร์ลิงก์ 11-15 ถึง
www.example.com
วิธีที่ตรงไปตรงมาคือการสร้างรายการคำขอ แล้วใช้การเรียกใช้ batchUpdate ครั้งเดียวดังนี้
Java
List<Request> requests = new ArrayList<>(); requests.add(new Request().setUpdateTextStyle(new UpdateTextStyleRequest() .setTextStyle(new TextStyle() .setBold(true) .setItalic(true)) .setRange(new Range() .setStartIndex(1) .setEndIndex(5)) .setFields("bold"))); requests.add(new Request() .setUpdateTextStyle(new UpdateTextStyleRequest() .setRange(new Range() .setStartIndex(6) .setEndIndex(10)) .setTextStyle(new TextStyle() .setWeightedFontFamily(new WeightedFontFamily() .setFontFamily("Times New Roman")) .setFontSize(new Dimension() .setMagnitude(14.0) .setUnit("PT")) .setForegroundColor(new OptionalColor() .setColor(new Color().setRgbColor(new RgbColor() .setBlue(1.0F) .setGreen(0.0F) .setRed(0.0F))))) .setFields("foregroundColor,weightedFontFamily,fontSize"))); requests.add(new Request() .setUpdateTextStyle(new UpdateTextStyleRequest() .setRange(new Range() .setStartIndex(11) .setEndIndex(15)) .setTextStyle(new TextStyle() .setLink(new Link() .setUrl("www.example.com"))) .setFields("link"))); BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests); BatchUpdateDocumentResponse response = docsService.documents() .batchUpdate(DOCUMENT_ID, body).execute();
Python
requests = [ { 'updateTextStyle': { 'range': { 'startIndex': 1, 'endIndex': 5 }, 'textStyle': { 'bold': True, 'italic': True }, 'fields': 'bold,italic' } }, { 'updateTextStyle': { 'range': { 'startIndex': 6, 'endIndex': 10 }, 'textStyle': { 'weightedFontFamily': { 'fontFamily': 'Times New Roman' }, 'fontSize': { 'magnitude': 14, 'unit': 'PT' }, 'foregroundColor': { 'color': { 'rgbColor': { 'blue': 1.0, 'green': 0.0, 'red': 0.0 } } } }, 'fields': 'foregroundColor,weightedFontFamily,fontSize' } }, { 'updateTextStyle': { 'range': { 'startIndex': 11, 'endIndex': 15 }, 'textStyle': { 'link': { 'url': 'www.example.com' } }, 'fields': 'link' } } ] result = service.documents().batchUpdate( documentId=DOCUMENT_ID, body={'requests': requests}).execute()
การเปลี่ยนการจัดรูปแบบย่อหน้า
Google เอกสาร API ช่วยให้คุณอัปเดตการจัดรูปแบบย่อหน้า ซึ่งจะกำหนดวิธีแสดงผลบล็อกข้อความในเอกสาร รวมถึงฟีเจอร์ต่างๆ เช่น การจัดข้อความและการเยื้อง
การจัดรูปแบบที่คุณใช้จะลบล้างการจัดรูปแบบเริ่มต้นที่รับช่วงมาจากรูปแบบย่อหน้าที่เกี่ยวข้อง ในทางกลับกัน คุณลักษณะการจัดรูปแบบใดๆ ที่ คุณยังไม่ได้ตั้งค่าจะยังคงรับค่ามาจากรูปแบบย่อหน้า ดูข้อมูลเพิ่มเติมเกี่ยวกับรูปแบบย่อหน้าและการสืบทอดได้ที่ ParagraphStyle
ตัวอย่างด้านล่างจะระบุการจัดรูปแบบต่อไปนี้สำหรับย่อหน้า
- ส่วนหัวในรูปแบบที่มีชื่อ
- กำหนดระยะห่างเองด้านบน
- กำหนดระยะห่างเองด้านล่าง
- เส้นขอบด้านซ้ายที่กำหนดเอง
คุณลักษณะการจัดรูปแบบที่เหลือทั้งหมดของย่อหน้าจะยังคงรับช่วงมาจากรูปแบบที่มีชื่อตามที่ระบุ
Java
List<Request> requests = new ArrayList<>(); requests.add(new Request().setUpdateParagraphStyle(new UpdateParagraphStyleRequest() .setRange(new Range() .setStartIndex(1) .setEndIndex(10)) .setParagraphStyle(new ParagraphStyle() .setNamedStyleType("HEADING_1") .setSpaceAbove(new Dimension() .setMagnitude(10.0) .setUnit("PT")) .setSpaceBelow(new Dimension() .setMagnitude(10.0) .setUnit("PT"))) .setFields("namedStyleType,spaceAbove,spaceBelow") )); requests.add(new Request().setUpdateParagraphStyle(new UpdateParagraphStyleRequest() .setRange(new Range() .setStartIndex(10) .setEndIndex(20)) .setParagraphStyle(new ParagraphStyle() .setBorderLeft(new ParagraphBorder() .setColor(new OptionalColor() .setColor(new Color() .setRgbColor(new RgbColor() .setBlue(1.0F) .setGreen(0.0F) .setRed(0.0F) ) ) ) .setDashStyle("DASH") .setPadding(new Dimension() .setMagnitude(20.0) .setUnit("PT")) .setWidth(new Dimension() .setMagnitude(15.0) .setUnit("PT") ) ) ) .setFields("borderLeft") )); BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests); BatchUpdateDocumentResponse response = docsService.documents() .batchUpdate(DOCUMENT_ID, body).execute();
Python
requests = [ { 'updateParagraphStyle': { 'range': { 'startIndex': 1, 'endIndex': 10 }, 'paragraphStyle': { 'namedStyleType': 'HEADING_1', 'spaceAbove': { 'magnitude': 10.0, 'unit': 'PT' }, 'spaceBelow': { 'magnitude': 10.0, 'unit': 'PT' } }, 'fields': 'namedStyleType,spaceAbove,spaceBelow' } }, { 'updateParagraphStyle': { 'range': { 'startIndex': 10, 'endIndex': 20 }, 'paragraphStyle': { 'borderLeft': { 'color': { 'color': { 'rgbColor': { 'blue': 1.0, 'green': 0.0, 'red': 0.0 } } }, 'dashStyle': 'DASH', 'padding': { 'magnitude': 20.0, 'unit': 'PT' }, 'width': { 'magnitude': 15.0, 'unit': 'PT' }, } }, 'fields': 'borderLeft' } } ] result = service.documents().batchUpdate( documentId=DOCUMENT_ID, body={'requests': requests}).execute()