# Csv Header only once

**URL:** <https://discourse.nodered.org/t/csv-header-only-once/21907>\
**Category:** General\
**Created:** [18 February 2020 09:36 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907 "2020-02-18T09:36:20Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [18 February 2020 16:03 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/21 "2020-02-18T16:03:50Z")

</div>

You are aware that in CSV a header is not any different from a normal row? It's just used as first row describing the columns, but follows the exact same format as a row would do.

---

<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:** [18 February 2020 16:55 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/22 "2020-02-18T16:55:51Z")

</div>

@zenofmud is on the right lines, but you would only need that once... so you would need to send that header line each time it created a new file - ie whenever the day changed - so you need a small bit of logic there to save the old day to context - and compare to the new day value - and if changed then prepend the header line to the existing payload, and then update context, etc

---

<div class="post-metadata">

**Author:** ![mustio](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mustio](https://discourse.nodered.org/u/mustio)\
**Post date:** [18 February 2020 17:20 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/23 "2020-02-18T17:20:28Z")

</div>

Yes, I think it is the way to do it. Could you help me with the code? I'm sorry but I'm newbie on node-red... 😔

I'm PLC programmer and JS is all new to me...

---

<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:** [18 February 2020 18:09 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/24 "2020-02-18T18:09:17Z")

</div>

Hopefully something like this for your function

```auto
context.day = context.day || ""; // restore or create blank variable to hold "old" day

var d = new Date();
var t = d.getTime();
var year = d.getFullYear();
var month = d.getMonth()+1; 
if (month.toString().length == 1) {
    month = '0'+month;
}
var day = d.getDate();
if (day.toString().length == 1) {
    day = '0'+day;
}
if (day != context.day) { // If day has changed then insert header line first and update context
    msg.payload = "Time,Prueba_1,Prueba_2\n" + msg.payload;
    context.day = day;
}
var hour = d.getHours();
msg.date = t;
msg.filename = "D:/Clientes/Node Red/Pruebas/Logs/"+year+month+day+"_Registro.csv";
return msg;

```

---

<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:** [3 March 2020 18:09 UTC](https://discourse.nodered.org/t/csv-header-only-once/21907/25 "2020-03-03T18:09:22Z")

</div>

Closed on 3 March 2020 18:09 UTC

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

[Previous page](https://discourse.nodered.org/t/csv-header-only-once/21907.md?page=1)
