CSV

◷ Reading Time: 4 minutes

toCsv

Creates a CSV document from a source input. It returns a CSV document that CSV monads can be operated.

source |toCsv (options)
  • source: null to create a new CSV document, otherwise a list of objects
  • options: The monad options is a set of key: value separated by,
    • header: If header should be part of the content when serialized, then true, otherwise false. Default is true.
    • separator: Specifies the field value separator in each row of CSV. Default is,
    • columns: List of columns in string or key/value pair of {name, property}
      • string: becomes the name of the property
      • key/value pair: reads name of the header and creates property with provided value. Defines mapping between the actual header name and resulted in property name.
Example: null |toCsv (separator:'\t', header: false)
Result: Returns a new CSV document that field values are separated by Tab (\t) and header will not be written.
Example: 'eg1.csv'|toCsv(separator:',', trim:true, columns:[{name:'St Day', property:'Date'}, 'Site_Name'])
Result: List of objects from file eg1.csv and only reads headers of St Date and Site_Name. In the result list all objects have properties of Date and Site_Name

csvWrite

Writes a collection of objects or a CSV document to a file.

source |csvWrite (path, options)
  • source: can be either a collection of objects or an existing CSV document (Mandatory)
  • path: File path to write the CSV into
  • options: The monad options is a set of key:value separated by ,
    • header: If header should be part of the content when serialized, then true, otherwise false. Default is true.
    • separator: Specifies the field value separator in each row of CSV. Default is ,
    • columns: List of columns in string or key/value pair of {name, property}
      • string: becomes name of the property
      • key/value pair: reads name of the header and creates property with provided value. Defines mapping between actual header name and resulted property name.
Example: list |csvWrite ('test-file-csv', separator:'\t', columns: ['Age', 'Name'])
Result: Writes to a CSV file
Example: [{'Age': 3, 'Name': 'Alex'}] |toCsv()|csvWrite (pathCombine(pathCurrent(),'test-file.csv'), separator:'\t', columns: ['Age', 'Name'])
Result: Writes to a CSV file

csvColumn

Creates or gets column on a CSV document

source |csvColumn (name, options)
  • source: can be an existing column or CSV document (Mandatory)
  • name: Property
  • options: The monad options is a set of key:value separated by ,
    • formatter: Allows introducing a function that can format the values when Csv is serialized.
    • trim: True when values should be trimmed. Otherwise false.
    • headerName: name of the header, otherwise the property name is used
  • return: CSV document

Formatter function

The function should be in the following format:

FunctionName(ArgName)=Expression
  • FunctionName: A name for the formatter function
  • ArgName: The argument of the function which will be a place holder for the column value to be formatted. This function must have only one argument.
  • Expression: The behaviour using Expressions to define the formatting logic

Example

Define NameDate and Country columns on doc. Also set the formatter property for the Date column called fnDate which accepts the value of date as an argument named dt.

doc 
 |csvColumn('Name')
 |csvColumn('Date', 
    formatter: fnDate (dt)= 
            if dt!=null 
            then dt|formatDate(dd/MM/yyyy) 
            else null
    )
 |csvColumn('Country')

csvAdd

Adds a record or object to a CSV document

doc |csvAdd (record)
  • doc: CSV document (Mandatory)
  • record: A single or a collection of objects to be added to the CSV document (Mandatory)
Example: "C:\Users\Alex\AppData\Local\Programs\Pliant Framework\FlexRule Designer\8.2.19\Bin\test-file.csv" |toCsv()|csvAdd ([{'Age': 45, 'Name': 'Sarah'}])
Return: Adds a redord to a CSV document.
Updated on November 29, 2022

Was this article helpful?

Related Articles