app.csv - CSV Parsing & Generation API
CSV text parsing, file reading, and generation.
Permission level: L0 (safe, always available)
Methods
app.csv.parse(text, options?)
Parses CSV text.
Parameters:
text(string) - CSV text contentoptions(table, optional):delimiter(string) - Delimiter (default,)header(boolean) - Use the first row as headers (default false)
Returns: array, error
-- Basic parsing (returns a 2D array)
local rows = app.csv.parse("name,age\nAlice,30\nBob,25")
-- {{"name", "age"}, {"Alice", "30"}, {"Bob", "25"}}
-- With headers (returns an array of dictionaries)
local rows = app.csv.parse("name,age\nAlice,30\nBob,25", {header = true})
-- {{name = "Alice", age = "30"}, {name = "Bob", age = "25"}}
-- TSV (tab-separated)
local rows = app.csv.parse(tsv_text, {delimiter = "\t"})
app.csv.parseFile(path, options?)
Reads and parses a CSV file.
Parameters:
path(string) - CSV file pathoptions(table, optional) - Same asparse()
Returns: array, error
local data, err = app.csv.parseFile("/path/to/data.csv", {header = true})
if data then
for _, row in ipairs(data) do
app.log.info(row.name .. ": " .. row.age)
end
end
app.csv.generate(rows, options?)
Generates CSV text from data.
Parameters:
rows(array) - 2D arrayoptions(table, optional):delimiter(string) - Delimiter (default,)
Returns: string, error
local csv = app.csv.generate({
{"name", "age", "city"},
{"Alice", "30", "New York"},
{"Bob", "25", "London"}
})
app.csv.generateFile(path, rows, options?)
Generates CSV and writes it to a file.
Parameters:
path(string) - Output file pathrows(array) - 2D arrayoptions(table, optional) - Same asgenerate()
Returns: boolean, error
local ok, err = app.csv.generateFile("/path/to/output.csv", {
{"name", "score"},
{"Alice", "95"},
{"Bob", "87"}
})
Examples
Export file list as CSV
function MyPlugin:handleExportCSV(context)
local rows = {{"File Name", "Size", "Type"}}
for _, file in ipairs(context.selectedFiles) do
local info = app.file.info(file)
if info then
table.insert(rows, {
app.path.basename(file),
tostring(info.size),
info.type or ""
})
end
end
local outPath = app.path.join(context.currentDirectory, "file_list.csv")
local ok, err = app.csv.generateFile(outPath, rows)
if ok then
app.notification.show("Done", "Exported " .. (#rows - 1) .. " records")
app.finder.reveal(outPath)
end
end