RAS standard library
CSV (csv)
The RAS standard library module for reading and writing CSV. Tables are passed as 2-D arrays (rows × columns; the same shape as range.toArray() / range.fromArray()). It is emitted into the VBA module RAS_Csv.
import * as csv from "csv";
export function importSales(src: string) {
const rows = csv.read(src, cast: true); // detects the encoding; converts to numbers, dates and TRUE/FALSE
Sheet1.range("A1").fromArray(rows);
const data = Sheet1.range("A1").currentRegion.toArray();
csv.write(src + ".out.csv", data); // UTF-8 with BOM (no garbled text when opened in Excel)
csv.write(src + ".sjis.csv", data, "shift_jis");
const tsv = csv.stringify(data, delimiter: "\t"); // a tab-delimited string
const back = csv.parse(tsv, delimiter: "\t"); // any[][]
debug.print(back.rows, back.cols);
}
The default encoding for writing is UTF-8 with a BOM (no garbled text when Excel opens the file by double-click). To use Shift_JIS, the same as Excel's "CSV (Comma delimited)", specify "shift_jis". The encoding names are the same as in fs.
Reference
Function csv.castValue
function castValue(s: string): any
Converts one CSV value to a value of the type that fits its content (cast of parse). An empty value is null, TRUE / FALSE (any case) is a boolean,
a number (including digit grouping like 1,234) is an int (integers up to 9 digits) or a double, a date (2026/4/1, 2026-04-01 09:30) is a date, anything else stays a string.
Numbers with a leading 0 ("0123", codes) and numbers of 16 or more digits stay strings.
Example: csv.castValue("1,200") → 1200, csv.castValue("2026/4/1") → a date, csv.castValue("007") → "007"
Function csv.parse
function parse(text: string, delimiter: string = ",", cast: boolean = false, trim: boolean = false, skipEmptyLines: boolean = true, comment: string = ""): any[][]
Parses a CSV string into a 2-D array (rows × columns). Values are strings; with cast: true they are converted to numbers, dates and booleans by castValue (empty values become null).
A value enclosed in quotes (") can contain the delimiter, line breaks and "" (one quote). Line breaks can be CR LF, LF or CR.
Rows with fewer columns are padded with "" (null with cast: true).
delimiter is the delimiter (default ","; "\t" for tab-separated), trim removes surrounding whitespace from values, skipEmptyLines skips empty lines (default true),
comment skips lines starting with that string (such as "#"). A leading BOM is removed.
Example: const rows = csv.parse(text);, csv.parse(text, delimiter: "\t", cast: true)
Function csv.formatValue
function formatValue(v: any, delimiter: string = ",", quoteAll: boolean = false): string
Converts one value to a CSV value string (used by stringify). null is empty, booleans are TRUE / FALSE, dates are 2026/04/01 (2026/04/01 09:30:00 when they have a time),
cell error values are #N/A and so on. Values containing the delimiter, quotes or line breaks (every value with quoteAll: true) are enclosed in quotes, with quotes inside doubled to "".
Example: csv.formatValue("a,b") → "\"a,b\""
Function csv.stringify
function stringify<T>(rows: T[][], delimiter: string = ",", newline: string = "\r\n", quoteAll: boolean = false): string
Converts a 2-D array (rows × columns, such as range.toArray()) to a CSV string. Each row is followed by newline (default CR LF).
Values are written as by formatValue (null is empty, booleans are TRUE / FALSE, dates are 2026/04/01).
Example: const text = csv.stringify(Sheet1.usedRange.toArray());, csv.stringify(rows, delimiter: "\t")
Function csv.read
function read(p: string, encoding: string = "auto", delimiter: string = ",", cast: boolean = false, trim: boolean = false, skipEmptyLines: boolean = true, comment: string = ""): any[][]
Reads a CSV file into a 2-D array (rows × columns). encoding is as in fs.readText (default "auto": detects BOM, UTF-8 and Shift_JIS).
The other arguments are as in parse.
Example: const rows = csv.read("C:\\data\\sales.csv", cast: true);, csv.read(path, "shift_jis")
Function csv.write
function write<T>(p: string, rows: T[][], encoding: string = "utf8bom", delimiter: string = ",", newline: string = "\r\n", quoteAll: boolean = false, append: boolean = false): void
Writes a 2-D array (rows × columns) to a CSV file. encoding is as in fs.writeText; the default is "utf8bom" (UTF-8 with a BOM, which Excel opens without garbled text).
Use "shift_jis" for Shift_JIS. With append: true the text is appended. The other arguments are as in stringify.
Example: csv.write("out.csv", Sheet1.range("A1").currentRegion.toArray()), csv.write(path, rows, "shift_jis")