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

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

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

มีการจัดรูปแบบ 2 ประเภทที่คุณสามารถใช้กับเนื้อหาข้อความของเอกสาร ได้แก่

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

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

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

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

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

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

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

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

วิธีการง่ายๆ ก็คือการสร้างรายการคำขอ จากนั้นใช้การเรียก batchUpdate 1 ครั้ง ดังนี้

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()