# Struggling to add headers to CSV file

**URL:** https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607
**Category:** General
**Created:** [27 April 2020 09:24 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607 "2020-04-27T09:24:02Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)
#### Post date: [27 April 2020 09:24 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607/1 "2020-04-27T09:24:02Z")

</div>

So i have been struggling with this problem a few days now and actually started to ignore it but i want it to work correctly. Adding headers to CSV file and then add the payload.  
Here is the flow: (a plc payload goes into the function node)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e3aac74a35ab04d6a809972634a18fe290b075af.png)

inside the function node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/88f420dc7d5a0d2ae0d796d8e851abc542eabd67.png)

and inside the CSV node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2d6e5c94403825b60c6b320766b8d9bc7aa56501.png)

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [27 April 2020 10:04 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607/2 "2020-04-27T10:04:41Z")

</div>

Add a `debug` node (set to display the 'Complete msg object') to the output of the `function` node and see if the data coming out is in the format the `csv` node needs it to be in.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [27 April 2020 10:10 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607/3 "2020-04-27T10:10:23Z")

</div>

You want to **write** column names: "Object to CSV options".  
Look at the screenshot of the csv node.

---

<div class="post-metadata">

### Author: ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)
#### Post date: [27 April 2020 10:17 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607/4 "2020-04-27T10:17:50Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5ac2e03171332bd0548f03fda1bd45c7b50b8a58.png)

---

<div class="post-metadata">

### Author: ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)
#### Post date: [27 April 2020 10:19 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607/5 "2020-04-27T10:19:45Z")

</div>

Ticking that box result in this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/c/9c26f9c5fc776fe808c978bb04a1f2e9af6fd27d.png)

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [27 April 2020 10:29 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607/6 "2020-04-27T10:29:50Z")

</div>

So you are sending a row of data at a time but adding a title line in each row.

Why not send the title row, then send all the data rows?  
Tiy should also fix the warnings in your function node.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [27 April 2020 10:34 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607/7 "2020-04-27T10:34:43Z")

</div>

- send all rows at once and check the box or
- first set the first line with column names and just append the data as normal strings instead of using the csv node

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [27 April 2020 12:05 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607/8 "2020-04-27T12:05:04Z")

</div>

Note: - There is a Pull Request waiting for next release that will improve this, and try to handle it automatically (ie send header once then as many rows of data as you want until you send a reset command).

---

<div class="post-metadata">

### Author: ![dathor](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@dathor](https://discourse.nodered.org/u/dathor)
#### Post date: [27 April 2020 12:25 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607/9 "2020-04-27T12:25:48Z")

</div>

I used two csv nodes to solve this problem. One configured with col header and the other not. You can then switch between the two nodes dependant on whether it is the first row or not.

---

<div class="post-metadata">

### Author: ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)
#### Post date: [28 April 2020 06:22 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607/10 "2020-04-28T06:22:08Z")

</div>

Okay so how can i program it so that every night at 00:00 it will create a new csv with headers and then add values to it

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [28 April 2020 06:48 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607/11 "2020-04-28T06:48:34Z")

</div>

Try this example

```auto
[{"id":"27715663.ec571a","type":"inject","z":"53c7fdaa.e46f0c","name":"value input","topic":"","payload":"98","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":108,"y":288,"wires":[["9fddc311.389708"]]},{"id":"9fddc311.389708","type":"function","z":"53c7fdaa.e46f0c","name":"","func":"let value = msg.payload\nlet d = new Date().toISOString().split(\"T\")\nlet date = d[0];\nlet time = d[1].split(\".\")[0]\nlet filename = \"./data_\"+date+\".csv\"\n\nlet csv = date+\",\"+time+\",\"+value+\"\\r\"\n\nreturn {filename:filename,payload:csv}","outputs":1,"noerr":0,"x":266,"y":288,"wires":[["115a5f9e.20a688"]]},{"id":"115a5f9e.20a688","type":"file","z":"53c7fdaa.e46f0c","name":"","filename":"","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":482,"y":240,"wires":[["cba862c2.e9afa"]]},{"id":"9a89661b.1e6e3","type":"inject","z":"53c7fdaa.e46f0c","name":"Create new file at 00:00","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"x":158,"y":192,"wires":[["de60ef75.0fe2d8"]]},{"id":"de60ef75.0fe2d8","type":"function","z":"53c7fdaa.e46f0c","name":"","func":"\nlet d = new Date().toISOString().split(\"T\")\nlet date = d[0];\nlet filename = \"./data_\"+date+\".csv\"\nlet headers = \"Date,Time,Value\"\n\nreturn {filename:filename,payload:headers}","outputs":1,"noerr":0,"x":338,"y":192,"wires":[["115a5f9e.20a688"]]},{"id":"cba862c2.e9afa","type":"debug","z":"53c7fdaa.e46f0c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":626,"y":240,"wires":[]}]

```

This does not use the csv node (as csv is comma separated text).  
At 00:00 it creates a new file `data_<date>.csv` and the values are written that file.

File contents looks like:

```auto
Date,Time,Value
2020-04-28,06:47:23,98
2020-04-28,06:47:24,98
2020-04-28,06:47:24,98

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [27 June 2020 06:48 UTC](https://discourse.nodered.org/t/struggling-to-add-headers-to-csv-file/25607/12 "2020-06-27T06:48:42Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
