WooCommerce utility library to convert data to CSV files.
npm install @woocommerce/csv-exportA set of functions to convert data into CSV values, and enable a browser download of the CSV data.
Install the module
``bash`
pnpm install @woocommerce/csv-export --save
`js
onClick = () => {
// Create a file name based on a title and optional query. Will return a timestamped
// name, for example: revenue-2018-11-01-interval-month.csv
const name = generateCSVFileName( 'revenue', { interval: 'month' } );
// Create a string of CSV data, headers is an array of row headers, put at the toprows
// of the file. is a 2 dimensional array. Each array is a line in the file,
// separated by newlines. The second-level arrays are the data points in each row.
const data = generateCSVDataFromTable( headers, rows );
// Triggers a browser UI to save a file, named the first argument, with the contents of
// the second argument.
downloadCSVFile( name, data );
}
`
Generates a CSV string from table contents
Returns: String - Table contents in a CSV format
| Param | Type | Description |
| --- | --- | --- |
| headers | Array.<Object> | Object with table header information |Array.Array.<Object>
| rows | | Object with table rows information |
Generates a file name for CSV files based on the provided name, the current date
and the provided params, which are all appended with hyphens.
Returns: String - Formatted file name
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [name] | String | '' | Name of the file |Object
| [params] | | {} | Object of key-values to append to the file name |
Downloads a CSV file with the given file name and contents
| Param | Type | Description |
| --- | --- | --- |
| fileName | String | Name of the file to download |String` | Contents of the file to download |
| content |