Back to Lowdefy

@lowdefy/plugin-csv

code-docs/plugins/plugins/csv.md

5.3.02.5 KB
Original Source

@lowdefy/plugin-csv

CSV parsing utilities for Lowdefy.

Overview

Provides operators for CSV parsing and generation.

Operators

OperatorPurpose
_csv_parseParse CSV string to array
_csv_stringifyConvert array to CSV string

_csv_parse

Parse CSV string to array of objects:

yaml
data:
  _csv_parse:
    csv:
      _state: csvInput
    options:
      header: true # First row is headers
      skipEmptyLines: true
      delimiter: ','

Options

OptionTypeDescription
headerbooleanUse first row as headers
skipEmptyLinesbooleanSkip empty lines
delimiterstringField delimiter (default: ,)
columnsarraySpecify column names

Output

With header: true:

javascript
[
  { name: 'John', email: '[email protected]' },
  { name: 'Jane', email: '[email protected]' },
];

Without header:

javascript
[
  ['name', 'email'],
  ['John', '[email protected]'],
  ['Jane', '[email protected]'],
];

_csv_stringify

Convert array to CSV:

yaml
csvOutput:
  _csv_stringify:
    data:
      _request: getData
    options:
      header: true
      columns:
        - key: name
          header: Name
        - key: email
          header: Email Address

Options

OptionTypeDescription
headerbooleanInclude header row
columnsarrayColumn definitions
delimiterstringField delimiter

Use Cases

Import CSV Data

yaml
events:
  onFileUpload:
    - id: parse
      type: SetState
      params:
        importedData:
          _csv_parse:
            csv:
              _event: content
            options:
              header: true

Export to CSV

yaml
events:
  onExport:
    - id: generate
      type: SetState
      params:
        csvDownload:
          _csv_stringify:
            data:
              _request: getExportData
            options:
              header: true

CSV File Download

Combine with Fetch action for download:

yaml
events:
  onClick:
    - id: downloadCsv
      type: Fetch
      params:
        url:
          _string:
            - 'data:text/csv;charset=utf-8,'
            - _csv_stringify:
                data:
                  _request: getData
        download: export.csv