woocommerce/plugins/woocommerce-admin/packages/csv-export
Darren Ethier dc5304823b Add @woocommerce/eslint-plugin (https://github.com/woocommerce/woocommerce-admin/pull/4714)
* add new woocommerce/eslint-plugin

* implement eslint-plugin (switch scripts to use wp-scripts etc)

* various linting fixes

* more lint fixes

* improve fix script

* more eslint fixes

* more eslint fixes

* temporary convert rules to warnings until they are all handled

* linting fixes

* update package-lock.json

* remove duplicates

* remove unnecessary `@wordpress/dependency-group` rule config

* add docs and adjust rules

* prettier fix

* fix merge artifact

* convert more rules to warnings

* change order to reflect importance

* install babel-preset-default

* remove unnecessary configuration for babel

* fix dependency group rule and test

* fix test

* handle collapsible whitespace

* add react testing library eslint plugin

* linter errors

* Add back in prettierrc

* package-lock update

* js lint fix with --fix

* disable whitespace in translation

* package-lock update

* make eslint actual dependency, not peer dependency

* pin eslint to v7

* I don't know why, but its working

* fix lint js

Co-authored-by: Paul Sealock <psealock@gmail.com>
2020-07-28 14:32:58 +12:00
..
src Add @woocommerce/eslint-plugin (https://github.com/woocommerce/woocommerce-admin/pull/4714) 2020-07-28 14:32:58 +12:00
.npmrc Build: Move `lib/csv` to packages (https://github.com/woocommerce/woocommerce-admin/pull/756) 2018-11-06 16:53:22 -05:00
CHANGELOG.md Packages: Prep for release. (https://github.com/woocommerce/woocommerce-admin/pull/3325) 2019-12-02 08:08:35 -05:00
README.md Update text domain. (https://github.com/woocommerce/woocommerce-admin/pull/1795) 2019-03-13 11:14:02 -06:00
package.json Update dependency moment to v2.27.0 (https://github.com/woocommerce/woocommerce-admin/pull/4814) 2020-07-17 14:48:02 +10:00

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