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

メソッド

メソッド戻り値の型概要
format(document)String指定された Document をフォーマットされた文字列として出力します。
format(element)String指定された Element ノードをフォーマットされた文字列として出力します。
setEncoding(encoding)Formatフォーマッタが使用する文字エンコードを設定します。
setIndent(indent)Format親ノードに対する子ノードのインデントに使用する文字列を設定します。
setLineSeparator(separator)Formatフォーマッタが通常改行を挿入するたびに挿入する文字列を設定します。
setOmitDeclaration(omitDeclaration)Formatフォーマッタが <?xml version="1.0" encoding="UTF-8"?> などの XML 宣言を省略するかどうかを設定します。
setOmitEncoding(omitEncoding)Formatフォーマッタが XML 宣言のエンコード(<?xml version="1.0" encoding="UTF-8"?> のエンコード フィールドなど)を省略するかどうかを設定します。

詳細なドキュメント

format(document)

指定された Document をフォーマットされた文字列として出力します。

パラメータ

名前説明
documentDocument書式設定するドキュメント。

戻る

String - フォーマットされたドキュメント。


format(element)

指定された Element ノードをフォーマットされた文字列として出力します。

パラメータ

名前説明
elementElementフォーマットする要素。

戻る

String - 書式設定された要素。


setEncoding(encoding)

フォーマッタが使用する文字エンコードを設定します。encoding 引数は、ISO-8859-1US-ASCIIUTF-8UTF-16 などの XML エンコードである必要があります。

// 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);

パラメータ

名前説明
encodingString使用するエンコード。

戻る

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

パラメータ

名前説明
indentString使用するインデント。

戻る

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

パラメータ

名前説明
separatorString使用する区切り文字。

戻る

Format - チェーン用のフォーマッタ。


setOmitDeclaration(omitDeclaration)

フォーマッタが <?xml version="1.0" encoding="UTF-8"?> などの XML 宣言を省略するかどうかを設定します。

パラメータ

名前説明
omitDeclarationBooleantrue は XML 宣言を省略し、false は XML 宣言を含めます。

戻る

Format - チェーン用のフォーマッタ。


setOmitEncoding(omitEncoding)

フォーマッタが XML 宣言のエンコード(<?xml version="1.0" encoding="UTF-8"?> のエンコード フィールドなど)を省略するかどうかを設定します。

パラメータ

名前説明
omitEncodingBooleantrue: XML 宣言でエンコードを省略します。false: エンコードを含めます。

戻る

Format - チェーン用のフォーマッタ。