จัดรูปแบบข้อความ

หน้านี้จะบอกวิธีจัดรูปแบบข้อความโดยใช้ Google Docs API

เกี่ยวกับการจัดรูปแบบ

การจัดรูปแบบข้อความในเอกสารมี 2 ประเภทดังนี้

  • คุณเปลี่ยนการจัดรูปแบบอักขระได้ เช่น แบบอักษร สี หรือขีดเส้นใต้
  • คุณเปลี่ยนการจัดรูปแบบย่อหน้าได้ เช่น การเยื้องหรือระยะบรรทัด

การเปลี่ยนการจัดรูปแบบอักขระ

การจัดรูปแบบอักขระจะเป็นตัวกำหนดการแสดงผลอักขระข้อความในเอกสาร

การจัดรูปแบบใดก็ตามที่คุณใช้จะลบล้างการจัดรูปแบบเริ่มต้นที่รับช่วงมาจาก TextStyle TextStyle ในทางกลับกัน อักขระใดก็ตามที่คุณไม่ได้ตั้งค่าการจัดรูปแบบจะยังคงรับช่วงมาจากสไตล์ของย่อหน้า

หากต้องการเปลี่ยนการจัดรูปแบบอักขระของข้อความ ให้ใช้ batchUpdate กับ UpdateTextStyleRequest โดยคุณต้องระบุออบเจ็กต์ Range ที่มีข้อมูลต่อไปนี้

  • segmentId ที่ระบุส่วนหัว ส่วนท้าย หรือเชิงอรรถ ซึ่งหากไม่ได้ระบุไว้จะเป็นเนื้อหาที่มีข้อความ
  • startIndex และ endIndex ที่กำหนดช่วงข้อความภายในส่วนที่จะจัดรูปแบบ
  • tabId ที่ระบุแท็บที่มีข้อความ

ตัวอย่างต่อไปนี้ดำเนินการจัดสไตล์ข้อความหลายรายการกับข้อความที่อยู่ในส่วนหัว

  • กำหนดแบบอักษรของอักขระที่ 1-5 เป็นตัวเอียงหนา
  • กำหนดสีของอักขระที่ 6-10 เป็นสีblue แบบอักษร Times New Roman ขนาด 14 จุด
  • กำหนดไฮเปอร์ลิงก์ให้อักขระที่ 11-15 ไปยัง www.example.com

วิธีที่ตรงไปตรงมาในการดำเนินการนี้คือการสร้างรายการคำขอ แล้ว ใช้การเรียก batchUpdate เพียงครั้งเดียว

Java

List requests = new ArrayList<>();
requests.add(new Request().setUpdateTextStyle(new UpdateTextStyleRequest()
        .setTextStyle(new TextStyle()
                .setBold(true)
                .setItalic(true))
        .setRange(new Range()
                .setStartIndex(1)
                .setEndIndex(5)
                .setTabId(TAB_ID))
        .setFields("bold")));

requests.add(new Request()
        .setUpdateTextStyle(new UpdateTextStyleRequest()
                .setRange(new Range()
                        .setStartIndex(6)
                        .setEndIndex(10)
                        .setTabId(TAB_ID))
                .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)
                        .setTabId(TAB_ID))
                .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,
                'tabId': 'TAB_ID'
            },
            'textStyle': {
                'bold': True,
                'italic': True
            },
            'fields': 'bold,italic'
        }
    },
    {
        'updateTextStyle': {
            'range': {
                'startIndex': 6,
                'endIndex': 10,
                'tabId': 'TAB_ID'
            },
            '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,
                'tabId': 'TAB_ID'
            },
            'textStyle': {
                'link': {
                    'url': 'www.example.com'
                }
            },
            'fields': 'link'
        }
    }
]

result = service.documents().batchUpdate(
    documentId='DOCUMENT_ID', body={'requests': requests}).execute()

การเปลี่ยนการจัดรูปแบบย่อหน้า

Google Docs API ช่วยให้คุณอัปเดตการจัดรูปแบบย่อหน้า ซึ่งจะเป็นตัวกำหนดวิธีแสดงผลบล็อกข้อความในเอกสาร รวมถึงฟีเจอร์ต่างๆ เช่น การจัดแนวและการเยื้อง

การจัดรูปแบบใดก็ตามที่คุณใช้จะลบล้างการจัดรูปแบบเริ่มต้นที่รับช่วงมาจากสไตล์ย่อหน้าด้านล่าง ในทางกลับกัน ฟีเจอร์การจัดรูปแบบใดก็ตามที่คุณไม่ได้ตั้งค่าจะยังคงรับช่วงมาจากสไตล์ย่อหน้า ดูข้อมูลเพิ่มเติมเกี่ยวกับสไตล์ย่อหน้าและการรับช่วงได้ที่ ParagraphStyle

ตัวอย่างด้านล่างระบุการจัดรูปแบบต่อไปนี้สำหรับย่อหน้า

  • หัวเรื่องเป็นสไตล์ที่มีชื่อ
  • ระยะห่างด้านบนที่กำหนดเอง
  • ระยะห่างด้านล่างที่กำหนดเอง
  • เส้นขอบด้านซ้ายที่กำหนดเอง

ฟีเจอร์การจัดรูปแบบอื่นๆ ทั้งหมดของย่อหน้าจะยังคงรับช่วงมาจากสไตล์ที่มีชื่อด้านล่าง

Java

List requests = new ArrayList<>();
requests.add(new Request().setUpdateParagraphStyle(new UpdateParagraphStyleRequest()
        .setRange(new Range()
                .setStartIndex(1)
                .setEndIndex(10)
                .setTabId(TAB_ID))
        .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)
                .setTabId(TAB_ID))
        .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,
                'tabId': TAB_ID
            },
            '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,
                'tabId': TAB_ID
            },
            '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()