informat.csv CSV Operations
Overview
Use informat.csv to perform read/write operations on CSV files
CsvReader
reader
Read CSV file from local sandbox. Throws exception if file doesn't exist
informat.csv.reader(file);| Parameter | Type | Description |
|---|---|---|
| file | String | File path in app's sandbox environment |
Return Value
Type CsvReader
Example
const reader = informat.csv.reader("path/to/file.csv");setFieldSeparator
Set field separator
reader.setFieldSeparator(c);Tip
- Default separator is comma
,
| Parameter | Type | Description |
|---|---|---|
| c | char | Field separator |
Example
reader.setFieldSeparator(",");setQuoteCharacter
Set quote character
reader.setQuoteCharacter(c);Tip
- Default is double quote
"
| Parameter | Type | Description |
|---|---|---|
| c | char | Quote character |
Example
reader.setQuoteCharacter('"');setCommentCharacter
Set comment character,
reader.setCommentCharacter(c);Tip
Default comment character #
| Parameter | Type | Description |
|---|---|---|
| c | char | Comment character |
Example
reader.setCommentCharacter("#");setCommentStrategy
Set comment strategy
reader.setCommentStrategy(s);| Parameter | Type | Description |
|---|---|---|
| s | String | NONE,SKIP,READ |
Strategy Description:
| Strategy | Description |
|---|---|
| NONE | Don't detect comments - treat all content as normal cell content, default value |
| SKIP | Detect comments but don't return comment lines |
| READ | Detect comments and return comment lines (entire line as one field). The comment character itself will be removed |
Example
reader.setCommentStrategy("NONE");setSkipEmptyRows
Set whether to skip empty rows
reader.setSkipEmptyRows(f);| Parameter | Type | Description |
|---|---|---|
| f | Boolean | Whether to skip empty rows |
Example
reader.setSkipEmptyRows(true);setErrorOnDifferentFieldCount
Set whether to throw exception when encountering inconsistent field counts
reader.setErrorOnDifferentFieldCount(f);| Parameter | Type | Description |
|---|---|---|
| f | Boolean | Whether to throw exception |
Example
reader.setErrorOnDifferentFieldCount(true);setCharset
Set character set (Default: UTF-8)
reader.setCharset(charset);| Parameter | Type | Description |
|---|---|---|
| charset | String | Character set, e.g.: UTF-8, GBK |
Example
reader.setCharset("UTF-8");read
Read CSV file
reader.read(accept);| Parameter | Type | Description |
|---|---|---|
| accept | Function(CsvRow) | Processing function for each row |
Example
let reader = informat.csv.reader("path/to/file.csv");
reader.read((csvRow) => {
for (let i = 0; i < csvRow.getFieldCount(); i++) {
console.log(csvRow.getField(i));
}
});CsvRow
getOriginalLineNumber
Return line number
csvRow.getOriginalLineNumber();getField
Return field
csvRow.getField(index);| Parameter | Type | Description |
|---|---|---|
| index | Integer | Index |
getFields
Return field list
csvRow.getFields();getFieldCount
Return field count
csvRow.getFieldCount();isComment
Whether this line is a comment
csvRow.isComment();isEmpty
Whether this line is empty
csvRow.isEmpty();CsvWriter
writer
Get input object and write content to CSV file in local sandbox
informat.csv.writer(file);| Parameter | Type | Description |
|---|---|---|
| file | String | File path in app's sandbox environment |
Return Value
Type CsvWriter
Example
let writer = informat.csv.writer("path/to/file.csv");
writer.writeRow(["a", "b", "c", "d"]);
writer.writeRow(["1", "2", "3", "4"]);
writer.close();setCharset
Set character set
writer.setCharset(charset);Tip
Default character set: UTF-8
| Parameter | Type | Description |
|---|---|---|
| charset | String | Character set, e.g.: UTF-8, GBK |
Example
writer.setCharset("UTF-8");setFieldSeparator
Set field separator
writer.setFieldSeparator(c);Tip
Default separator is comma ,
| Parameter | Type | Description |
|---|---|---|
| c | char | Field separator |
Example
writer.setFieldSeparator();setQuoteCharacter
Set quote character, default is double quote (""
writer.setQuoteCharacter(c);Tip
Default is double quote "
| Parameter | Type | Description |
|---|---|---|
| c | char | Quote character |
Example
writer.setQuoteCharacter();setQuoteStrategy
Set quote strategy
writer.setQuoteStrategy(s);| Parameter | Type | Description |
|---|---|---|
| s | String | REQUIRED,EMPTY,ALWAYS |
Strategy Description:
| Strategy | Description |
|---|---|
| REQUIRED | This strategy requires all fields to use quotes, default value |
| EMPTY | This strategy requires quotes only when fields contain commas or line breaks |
| ALWAYS | This strategy requires all fields to always use quotes |
Example
writer.setQuoteStrategy("REQUIRED");setCommentCharacter
Set comment character, default comment character is (#)
writer.setCommentCharacter(c);| Parameter | Type | Description |
|---|---|---|
| c | char | Comment character |
setLineDelimiter
Set line delimiter, default is (CRLF)
writer.setLineDelimiter(s);| Parameter | Type | Description |
|---|---|---|
| s | String | Line delimiter |
Line Delimiters
| Delimiter | Description |
|---|---|
| CRLF | Corresponds to \r\n, default value |
| CR | Corresponds to \r |
| LF | Corresponds to \n |
| PLATFORM | System default |
Example
writer.setLineDelimiter("CRLF");writeRow
Write a row of data
writer.writeRow(row);| Parameter | Type | Description |
|---|---|---|
| row | Array<String> | Content array |
writeComment
Write comment line
writer.writeComment(comment);| Parameter | Type | Description |
|---|---|---|
| comment | String | Comment content |
Example
writer.writeComment("Comment content");close
Close input stream
writer.close();
