Skip to content

CSVWriter ​

Writes rows to a CSV file, one array of values per row.

js
const writer = datasource.csv("exports/actuals.csv");

writer.write(["year", "month", "product", "amount"]);
writer.write(["2026", "Jan", "Widgets", 1200.5]);
writer.write(["2026", "Jan", "Gadgets", 830]);

writer.close();
csv
"year","month","product","amount"
"2026","Jan","Widgets",1200.5
"2026","Jan","Gadgets",830

Constructor ​

datasource.csv datasource.csv(fileName) CSVWriter | null ​

Creates a CSV file in the filesystem and returns a CSVWriter, or null when the path is invalid. If the file already exists, it's replaced.

ParameterTypeDescription
fileNamestringThe path of the file, for example "exports/actuals.csv". Missing folders are created.
js
// A file in the exports folder
const writer = datasource.csv("exports/actuals.csv");

// A file in a folder that doesn't exist yet; the folder is created
const writer = datasource.csv("exports/2026/actuals.csv");

Overview ​

  • One row per call: pass write() an array of values, one per column.
  • How values are written: numbers and booleans are written bare, null is written as NULL, and everything else is written as quoted text.
  • Format: rows are comma-delimited, text is wrapped in double quotes, and rows are separated by a Windows-style line break (\r\n). Change these with the setter methods before writing any rows.
  • Always close: call close() when you've finished, so everything is written to the file.

Methods ​

write write(values) boolean ​

Writes one row to the file. Returns true on success, or false when the file is closed or values isn't an array.

ParameterTypeDescription
valuesarrayThe values for one row, in column order.

close close() ​

Writes any buffered rows to the file and closes it. Always call it when you've finished writing: until then, the file may not be complete. Writes after close() are ignored and logged as errors.

setDelimiter setDelimiter(delimiter) ​

Sets the text written between values. The default is a comma. Call it before writing any rows.

ParameterTypeDescription
delimiterstringThe delimiter, for example ";", or "\t" for tab.

getDelimiter getDelimiter() string ​

Returns the text written between values, "," unless changed with setDelimiter().

setQuoteCharacter setQuoteCharacter(quoteCharacter) ​

Sets the character wrapped around text values. The default is a double quote. Call it before writing any rows.

ParameterTypeDescription
quoteCharacterstringThe quote character. Pass "" to turn quoting off.

getQuoteCharacter getQuoteCharacter() string ​

Returns the character wrapped around text values, " unless changed with setQuoteCharacter().

setLineBreak setLineBreak(lineBreak) ​

Sets the text written between rows. The default is "\r\n". Call it before writing any rows.

ParameterTypeDescription
lineBreakstringThe line break, for example "\n" for Unix-style line endings.

getLineBreak getLineBreak() string ​

Returns the text written between rows, "\r\n" unless changed with setLineBreak().

Examples ​

A semicolon-delimited file ​

js
const writer = datasource.csv("exports/budget.csv");
writer.setDelimiter(";");

writer.write(["account", "month", "amount"]);
writer.write(["4000", "Jan", 1000]);
writer.write(["4000", "Feb", 1100]);

writer.close();
csv
"account";"month";"amount"
"4000";"Jan";1000
"4000";"Feb";1100

Values without quotes ​

js
const writer = datasource.csv("exports/products.csv");
writer.setQuoteCharacter("");

writer.write(["code", "name", "active"]);
writer.write(["W-01", "Widget", true]);
writer.write(["G-02", "Gadget", false]);

writer.close();
csv
code,name,active
W-01,Widget,true
G-02,Gadget,false