Skip to content

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

javascript
informat.csv.reader(file);
ParameterTypeDescription
fileStringFile path in app's sandbox environment

Return Value

Type CsvReader

Example

javascript
const reader = informat.csv.reader("path/to/file.csv");

setFieldSeparator ​

Set field separator

js
reader.setFieldSeparator(c);

Tip

  • Default separator is comma ,
ParameterTypeDescription
ccharField separator

Example

js
reader.setFieldSeparator(",");

setQuoteCharacter ​

Set quote character

js
reader.setQuoteCharacter(c);

Tip

  • Default is double quote "
ParameterTypeDescription
ccharQuote character

Example

js
reader.setQuoteCharacter('"');

setCommentCharacter ​

Set comment character,

js
reader.setCommentCharacter(c);

Tip

Default comment character #

ParameterTypeDescription
ccharComment character

Example

js
reader.setCommentCharacter("#");

setCommentStrategy ​

Set comment strategy

js
reader.setCommentStrategy(s);
ParameterTypeDescription
sStringNONE,SKIP,READ

Strategy Description:

StrategyDescription
NONEDon't detect comments - treat all content as normal cell content, default value
SKIPDetect comments but don't return comment lines
READDetect comments and return comment lines (entire line as one field). The comment character itself will be removed

Example

js
reader.setCommentStrategy("NONE");

setSkipEmptyRows ​

Set whether to skip empty rows

js
reader.setSkipEmptyRows(f);
ParameterTypeDescription
fBooleanWhether to skip empty rows

Example

js
reader.setSkipEmptyRows(true);

setErrorOnDifferentFieldCount ​

Set whether to throw exception when encountering inconsistent field counts

js
reader.setErrorOnDifferentFieldCount(f);
ParameterTypeDescription
fBooleanWhether to throw exception

Example

js
reader.setErrorOnDifferentFieldCount(true);

setCharset ​

Set character set (Default: UTF-8)

js
reader.setCharset(charset);
ParameterTypeDescription
charsetStringCharacter set, e.g.: UTF-8, GBK

Example

js
reader.setCharset("UTF-8");

read ​

Read CSV file

js
reader.read(accept);
ParameterTypeDescription
acceptFunction(CsvRow)Processing function for each row

Example

js
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

js
csvRow.getOriginalLineNumber();

getField ​

Return field

js
csvRow.getField(index);
ParameterTypeDescription
indexIntegerIndex

getFields ​

Return field list

js
csvRow.getFields();

getFieldCount ​

Return field count

js
csvRow.getFieldCount();

isComment ​

Whether this line is a comment

js
csvRow.isComment();

isEmpty ​

Whether this line is empty

js
csvRow.isEmpty();

CsvWriter ​

writer ​

Get input object and write content to CSV file in local sandbox

javascript
informat.csv.writer(file);
ParameterTypeDescription
fileStringFile path in app's sandbox environment

Return Value

Type CsvWriter

Example

js
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

js
writer.setCharset(charset);

Tip

Default character set: UTF-8

ParameterTypeDescription
charsetStringCharacter set, e.g.: UTF-8, GBK

Example

js
writer.setCharset("UTF-8");

setFieldSeparator ​

Set field separator

js
writer.setFieldSeparator(c);

Tip

Default separator is comma ,

ParameterTypeDescription
ccharField separator

Example

js
writer.setFieldSeparator();

setQuoteCharacter ​

Set quote character, default is double quote (""

js
writer.setQuoteCharacter(c);

Tip

Default is double quote "

ParameterTypeDescription
ccharQuote character

Example

js
writer.setQuoteCharacter();

setQuoteStrategy ​

Set quote strategy

js
writer.setQuoteStrategy(s);
ParameterTypeDescription
sStringREQUIRED,EMPTY,ALWAYS

Strategy Description:

StrategyDescription
REQUIREDThis strategy requires all fields to use quotes, default value
EMPTYThis strategy requires quotes only when fields contain commas or line breaks
ALWAYSThis strategy requires all fields to always use quotes

Example

js
writer.setQuoteStrategy("REQUIRED");

setCommentCharacter ​

Set comment character, default comment character is (#)

js
writer.setCommentCharacter(c);
ParameterTypeDescription
ccharComment character

setLineDelimiter ​

Set line delimiter, default is (CRLF)

js
writer.setLineDelimiter(s);
ParameterTypeDescription
sStringLine delimiter

Line Delimiters

DelimiterDescription
CRLFCorresponds to \r\n, default value
CRCorresponds to \r
LFCorresponds to \n
PLATFORMSystem default

Example

js
writer.setLineDelimiter("CRLF");

writeRow ​

Write a row of data

js
writer.writeRow(row);
ParameterTypeDescription
rowArray<String>Content array

writeComment ​

Write comment line

js
writer.writeComment(comment);
ParameterTypeDescription
commentStringComment content

Example

js
writer.writeComment("Comment content");

close ​

Close input stream

js
writer.close();