1ac8577fc2
* Add "exportable" report interface for defining CSV export values. * Define export values for Orders Report. * Define export values for Products Report. * Define export values for Categories Report. * Define export values for Coupons Report. * Allow commas and double quotes in CSV exported values. * Fix in-browser export formatting of orders report products. * Align server-side orders report export formatting with in-browser. * Cover comma and double quote escaping in CSV export package tests. * Define export values for Customers Report. * Embed response links when requesting data for CSV exports. * Define export values for Downloads Report. * Move reusable report export functions to a trait. * Define export values for Stock Report. * Define export values for Taxes Report. * Define export values for Variations Report. * Define export values for Revenue Report. * Always pass export row data through the filter. * Fix formatting in test case for CSV coupon export. * Quote escape CSV headers in client-side export. Escape values with spaces as well. * Check if inventory is managed at the product level before using the stock status/quantity. * Prevent CSV injection in csv-export package. |
||
---|---|---|
.. | ||
src | ||
.npmrc | ||
CHANGELOG.md | ||
README.md | ||
package.json |
README.md
CSV Export
A set of functions to convert data into CSV values, and enable a browser download of the CSV data.
Installation
Install the module
npm install @woocommerce/csv-export --save
Usage
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 top
// of the file. `rows` 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.
// For header format, see https://woocommerce.github.io/woocommerce-admin/#/components/table?id=headers-2
// For rows format, see https://woocommerce.github.io/woocommerce-admin/#/components/table?id=rows-1
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 );
}
generateCSVDataFromTable(headers, rows) ⇒ String
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 |
rows | Array.Array.<Object> |
Object with table rows information |
generateCSVFileName([name], [params]) ⇒ String
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 |
[params] | Object |
{} |
Object of key-values to append to the file name |
downloadCSVFile(fileName, content)
Downloads a CSV file with the given file name and contents
Param | Type | Description |
---|---|---|
fileName | String |
Name of the file to download |
content | String |
Contents of the file to download |