Class Format
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
รูปแบบ
โปรแกรมจัดรูปแบบสำหรับแสดงผลเอกสาร XML โดยมีรูปแบบที่กำหนดไว้ล่วงหน้า 3 รูปแบบที่ปรับแต่งเพิ่มเติมได้
// Log an XML document with specified formatting options.
const xml = '<root><a><b>Text!</b><b>More text!</b></a></root>';
const document = XmlService.parse(xml);
const output = XmlService.getCompactFormat()
.setLineSeparator('\n')
.setEncoding('UTF-8')
.setIndent(' ')
.format(document);
Logger.log(output);
เอกสารประกอบโดยละเอียด
setEncoding(encoding)
กำหนดการเข้ารหัสอักขระที่เครื่องมือจัดรูปแบบควรใช้ อาร์กิวเมนต์ encoding
ต้องเป็นการเข้ารหัส XML ที่ยอมรับ เช่น ISO-8859-1
, US-ASCII
, UTF-8
หรือ UTF-16
// Log an XML document with encoding that does not support certain special
// characters.
const xml = '<root><a><b>ಠ‿ಠ</b><b>ಠ‿ಠ</b></a></root>';
const document = XmlService.parse(xml);
const output =
XmlService.getRawFormat().setEncoding('ISO-8859-1').format(document);
Logger.log(output);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
encoding | String | การเข้ารหัสที่จะใช้ |
รีเทิร์น
Format
— ตัวจัดรูปแบบสำหรับใช้ต่อเชื่อม
setIndent(indent)
ตั้งค่าสตริงที่ใช้เยื้องโหนดย่อยตามโหนดหลัก การตั้งค่าการเยื้องที่ไม่ใช่ null
จะทำให้โปรแกรมจัดรูปแบบแทรกการขึ้นบรรทัดใหม่หลังโหนดทุกโหนด
// Log an XML document with each child node indented four spaces.
const xml = '<root><a><b>Text!</b><b>More text!</b></a></root>';
const document = XmlService.parse(xml);
const output = XmlService.getCompactFormat().setIndent(' ').format(document);
Logger.log(output);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
indent | String | การเยื้องที่จะใช้ |
รีเทิร์น
Format
— ตัวจัดรูปแบบสำหรับใช้ต่อเชื่อม
setLineSeparator(separator)
ตั้งค่าสตริงที่จะแทรกทุกครั้งที่โปรแกรมจัดรูปแบบจะแทรกตัวแบ่งบรรทัดตามปกติ เครื่องมือจัดรูปแบบที่กําหนดไว้ล่วงหน้า 3 รายการมีเงื่อนไขที่แตกต่างกันในการแทรกการขึ้นบรรทัดใหม่ ตัวแบ่งบรรทัดเริ่มต้นคือ \r\n
// Log an XML document with several spaces and a pipe character in place of line
// breaks.
const xml = '<root><a><b>Text!</b><b>More text!</b></a></root>';
const document = XmlService.parse(xml);
const output =
XmlService.getRawFormat().setLineSeparator(' | ').format(document);
Logger.log(output);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
separator | String | ตัวคั่นที่จะใช้ |
รีเทิร์น
Format
— ตัวจัดรูปแบบสำหรับใช้ต่อเชื่อม
setOmitDeclaration(omitDeclaration)
ตั้งค่าว่าควรละเว้นการประกาศ XML หรือไม่ เช่น <?xml version="1.0"
encoding="UTF-8"?>
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
omitDeclaration | Boolean | true เพื่อละเว้นการประกาศ XML และ false เพื่อรวมการประกาศ |
รีเทิร์น
Format
— ตัวจัดรูปแบบสำหรับใช้ต่อเชื่อม
setOmitEncoding(omitEncoding)
ตั้งค่าว่าเครื่องมือจัดรูปแบบควรละเว้นการเข้ารหัสในการประกาศ XML หรือไม่ เช่น ช่องการเข้ารหัสใน <?xml version="1.0" encoding="UTF-8"?>
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
omitEncoding | Boolean | true เพื่อละเว้นการเข้ารหัสในการประกาศ XML และ false เพื่อรวมการเข้ารหัส |
รีเทิร์น
Format
— ตัวจัดรูปแบบสำหรับใช้ต่อเชื่อม
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eThe \u003ccode\u003eXmlService\u003c/code\u003e provides a formatter to output XML documents with customizable formatting options like encoding, indentation, and line separators.\u003c/p\u003e\n"],["\u003cp\u003eThree predefined formats are available and can be accessed using \u003ccode\u003eXmlService.getPrettyFormat()\u003c/code\u003e, \u003ccode\u003eXmlService.getCompactFormat()\u003c/code\u003e, and \u003ccode\u003eXmlService.getRawFormat()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eformat()\u003c/code\u003e method outputs either a given \u003ccode\u003eDocument\u003c/code\u003e or \u003ccode\u003eElement\u003c/code\u003e node as a formatted string based on the set formatting options.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can customize the output by setting the encoding, indent, line separator, and whether to omit the XML declaration or encoding using the respective methods on the \u003ccode\u003eFormat\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eThe formatter allows for chaining methods to easily configure multiple formatting options in a single line of code.\u003c/p\u003e\n"]]],[],null,["# Class Format\n\nFormat\n\nA formatter for outputting an XML document, with three pre-defined formats that can be further\ncustomized.\n\n```javascript\n// Log an XML document with specified formatting options.\nconst xml = '\u003croot\u003e\u003ca\u003e\u003cb\u003eText!\u003c/b\u003e\u003cb\u003eMore text!\u003c/b\u003e\u003c/a\u003e\u003c/root\u003e';\nconst document = XmlService.parse(xml);\nconst output = XmlService.getCompactFormat()\n .setLineSeparator('\\n')\n .setEncoding('UTF-8')\n .setIndent(' ')\n .format(document);\nLogger.log(output);\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|---------------------------------------------------------------------|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|\n| [format(document)](#format(Document)) | `String` | Outputs the given [Document](/apps-script/reference/xml-service/document) as a formatted string. |\n| [format(element)](#format(Element)) | `String` | Outputs the given [Element](/apps-script/reference/xml-service/element) node as a formatted string. |\n| [setEncoding(encoding)](#setEncoding(String)) | [Format](#) | Sets the character encoding that the formatter should use. |\n| [setIndent(indent)](#setIndent(String)) | [Format](#) | Sets the string used to indent child nodes relative to their parents. |\n| [setLineSeparator(separator)](#setLineSeparator(String)) | [Format](#) | Sets the string to insert whenever the formatter would normally insert a line break. |\n| [setOmitDeclaration(omitDeclaration)](#setOmitDeclaration(Boolean)) | [Format](#) | Sets whether the formatter should omit the XML declaration, such as `\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e`. |\n| [setOmitEncoding(omitEncoding)](#setOmitEncoding(Boolean)) | [Format](#) | Sets whether the formatter should omit the encoding in the XML declaration, such as the encoding field in `\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e`. |\n\nDetailed documentation\n----------------------\n\n### `format(document)`\n\nOutputs the given [Document](/apps-script/reference/xml-service/document) as a formatted string.\n\n#### Parameters\n\n| Name | Type | Description |\n|------------|---------------------------------------------------------|------------------------|\n| `document` | [Document](/apps-script/reference/xml-service/document) | the document to format |\n\n#### Return\n\n\n`String` --- the formatted document\n\n*** ** * ** ***\n\n### `format(element)`\n\nOutputs the given [Element](/apps-script/reference/xml-service/element) node as a formatted string.\n\n#### Parameters\n\n| Name | Type | Description |\n|-----------|-------------------------------------------------------|-----------------------|\n| `element` | [Element](/apps-script/reference/xml-service/element) | the element to format |\n\n#### Return\n\n\n`String` --- the formatted element\n\n*** ** * ** ***\n\n### `set``Encoding(encoding)`\n\nSets the character encoding that the formatter should use. The `encoding` argument must\nbe an accepted XML encoding like `ISO-8859-1`, `US-ASCII`, `UTF-8`, or `UTF-16`.\n\n```javascript\n// Log an XML document with encoding that does not support certain special\n// characters.\nconst xml = '\u003croot\u003e\u003ca\u003e\u003cb\u003eಠ‿ಠ\u003c/b\u003e\u003cb\u003eಠ‿ಠ\u003c/b\u003e\u003c/a\u003e\u003c/root\u003e';\nconst document = XmlService.parse(xml);\nconst output =\n XmlService.getRawFormat().setEncoding('ISO-8859-1').format(document);\nLogger.log(output);\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|------------|----------|---------------------|\n| `encoding` | `String` | the encoding to use |\n\n#### Return\n\n\n[Format](#) --- the formatter, for chaining\n\n*** ** * ** ***\n\n### `set``Indent(indent)`\n\nSets the string used to indent child nodes relative to their parents. Setting an indent other\nthan `null` will cause the formatter to insert a line break after every node.\n\n```javascript\n// Log an XML document with each child node indented four spaces.\nconst xml = '\u003croot\u003e\u003ca\u003e\u003cb\u003eText!\u003c/b\u003e\u003cb\u003eMore text!\u003c/b\u003e\u003c/a\u003e\u003c/root\u003e';\nconst document = XmlService.parse(xml);\nconst output = XmlService.getCompactFormat().setIndent(' ').format(document);\nLogger.log(output);\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|----------|----------|-------------------|\n| `indent` | `String` | the indent to use |\n\n#### Return\n\n\n[Format](#) --- the formatter, for chaining\n\n*** ** * ** ***\n\n### `set``Line``Separator(separator)`\n\nSets the string to insert whenever the formatter would normally insert a line break. The three\npre-defined formatters have different conditions under which they insert a line break. The\ndefault line separator is `\\r\\n`.\n\n```javascript\n// Log an XML document with several spaces and a pipe character in place of line\n// breaks.\nconst xml = '\u003croot\u003e\u003ca\u003e\u003cb\u003eText!\u003c/b\u003e\u003cb\u003eMore text!\u003c/b\u003e\u003c/a\u003e\u003c/root\u003e';\nconst document = XmlService.parse(xml);\nconst output =\n XmlService.getRawFormat().setLineSeparator(' | ').format(document);\nLogger.log(output);\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|-------------|----------|----------------------|\n| `separator` | `String` | the separator to use |\n\n#### Return\n\n\n[Format](#) --- the formatter, for chaining\n\n*** ** * ** ***\n\n### `set``Omit``Declaration(omitDeclaration)`\n\nSets whether the formatter should omit the XML declaration, such as `\u003c?xml version=\"1.0\"\nencoding=\"UTF-8\"?\u003e`.\n\n#### Parameters\n\n| Name | Type | Description |\n|---------------------|-----------|-----------------------------------------------------------|\n| `omit``Declaration` | `Boolean` | `true` to omit the XML declaration; `false` to include it |\n\n#### Return\n\n\n[Format](#) --- the formatter, for chaining\n\n*** ** * ** ***\n\n### `set``Omit``Encoding(omitEncoding)`\n\nSets whether the formatter should omit the encoding in the XML declaration, such as the\nencoding field in `\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e`.\n\n#### Parameters\n\n| Name | Type | Description |\n|------------------|-----------|---------------------------------------------------------------------------|\n| `omit``Encoding` | `Boolean` | `true` to omit the encoding in the XML declaration; `false` to include it |\n\n#### Return\n\n\n[Format](#) --- the formatter, for chaining"]]