|
26 | 26 | * [`quoteColumns`](#examples-quote-columns)
|
27 | 27 | * [`quoteHeaders`](#examples-quote-headers)
|
28 | 28 | * [Transforming Rows](#examples-transforming)
|
| 29 | + * [Appending To A CSV](#examples-appending) |
29 | 30 |
|
30 | 31 | <a name="options"></a>
|
31 | 32 | ## Options
|
|
52 | 53 | * If there is not a headers row and you want to provide one then set to a `string[]`
|
53 | 54 | * **NOTE** If the row is an object the headers must match fields in the object, otherwise you will end up with empty fields
|
54 | 55 | * **NOTE** If there are more headers than columns then additional empty columns will be added
|
| 56 | +* `alwaysWriteHeaders: {boolean} = false`: Set to true if you always want headers written, even if no rows are written. |
| 57 | + * **NOTE** This will throw an error if headers are not specified as an array. |
55 | 58 | * `quoteColumns: {boolean|boolean[]|{[string]: boolean} = false`
|
56 | 59 | * If `true` then columns and headers will be quoted (unless `quoteHeaders` is specified).
|
57 | 60 | * If it is an object then each key that has a true value will be quoted ((unless `quoteHeaders` is specified)
|
@@ -836,4 +839,88 @@ VALUE1A,VALUE2A
|
836 | 839 | VALUE1A,VALUE2A
|
837 | 840 | VALUE1A,VALUE2A
|
838 | 841 | VALUE1A,VALUE2A
|
| 842 | +``` |
| 843 | + |
| 844 | +<a name="examples-appending"></a> |
| 845 | +### Appending To A CSV |
| 846 | + |
| 847 | +[`examples/formatting/append.example.js`](../examples/formatting/append.example.js) |
| 848 | + |
| 849 | +In this example a new csv is created then appended to. |
| 850 | + |
| 851 | +```javascript |
| 852 | +const path = require('path'); |
| 853 | +const fs = require('fs'); |
| 854 | + |
| 855 | +const write = (filestream, rows, options) => { |
| 856 | + return new Promise((res, rej) => { |
| 857 | + csv.writeToStream(filestream, rows, options) |
| 858 | + .on('error', err => rej(err)) |
| 859 | + .on('finish', () => res()); |
| 860 | + }); |
| 861 | +}; |
| 862 | + |
| 863 | +// create a new csv |
| 864 | +const createCsv = (filePath, rows) => { |
| 865 | + const csvFile = fs.createWriteStream(filePath); |
| 866 | + return write(csvFile, rows, { headers: true, includeEndRowDelimiter: true }); |
| 867 | +}; |
| 868 | + |
| 869 | +// append the rows to the csv |
| 870 | +const appendToCsv = (filePath, rows = []) => { |
| 871 | + const csvFile = fs.createWriteStream(filePath, { flags: 'a' }); |
| 872 | + // notice how headers are set to false |
| 873 | + return write(csvFile, rows, { headers: false }); |
| 874 | +}; |
| 875 | + |
| 876 | +// read the file |
| 877 | +const readFile = filePath => { |
| 878 | + return new Promise((res, rej) => { |
| 879 | + fs.readFile(filePath, (err, contents) => { |
| 880 | + if (err) { |
| 881 | + return rej(err); |
| 882 | + } |
| 883 | + return res(contents); |
| 884 | + }); |
| 885 | + }); |
| 886 | +}; |
| 887 | + |
| 888 | +const csvFilePath = path.resolve(__dirname, 'tmp', 'append.csv'); |
| 889 | + |
| 890 | +// 1. create the csv |
| 891 | +createCsv(csvFilePath, [ |
| 892 | + { a: 'a1', b: 'b1', c: 'c1' }, |
| 893 | + { a: 'a2', b: 'b2', c: 'c2' }, |
| 894 | + { a: 'a3', b: 'b3', c: 'c3' }, |
| 895 | +]) |
| 896 | + .then(() => { |
| 897 | + // 2. append to the csv |
| 898 | + return appendToCsv(csvFilePath, [ |
| 899 | + { a: 'a4', b: 'b4', c: 'c4' }, |
| 900 | + { a: 'a5', b: 'b5', c: 'c5' }, |
| 901 | + { a: 'a6', b: 'b6', c: 'c6' }, |
| 902 | + ]); |
| 903 | + }) |
| 904 | + .then(() => readFile(csvFilePath)) |
| 905 | + .then(contents => { |
| 906 | + console.log(`${contents}`); |
| 907 | + }) |
| 908 | + .catch(err => { |
| 909 | + console.error(err.stack); |
| 910 | + process.exit(1); |
| 911 | + }); |
| 912 | + |
| 913 | + |
| 914 | +``` |
| 915 | + |
| 916 | +Expected output |
| 917 | + |
| 918 | +``` |
| 919 | +a,b,c |
| 920 | +a1,b1,c1 |
| 921 | +a2,b2,c2 |
| 922 | +a3,b3,c3 |
| 923 | +a4,b4,c4 |
| 924 | +a5,b5,c5 |
| 925 | +a6,b6,c6 |
839 | 926 | ```
|
0 commit comments