PapaParse icon indicating copy to clipboard operation
PapaParse copied to clipboard

transformHeader for unparse?

Open MihailsKuzmins opened this issue 1 year ago • 1 comments

According to the documentation (https://www.papaparse.com/docs#unparse-config-default) unparse does not provide "transformHeader" functionality, so it is not possible to customize the headers while preserving the original object. Are there any plans to add this functionality in the future?

For now the only solution is to add these original properties to the original object and specify them as "columns" of UnparseConfig.

const csvOptions: UnparseConfig = {
  columns: ['customHeader'],
  // ... more properties
};

for (const item of items) {
  item['customHeader'] = item['realProperty'];
}

const csvData = Papa.unparse(items, csvOptions);

MihailsKuzmins avatar Jan 24 '24 03:01 MihailsKuzmins

You can achieve this with https://github.com/juanjoDiaz/json2csv

    const data = [
      { "carModel": "Audi", "price": 0, "color": "blue" },
      { "carModel": "BMW", "price": 15000, "color": "red", "manual": true },
      { "carModel": "Mercedes", "price": 20000, "color": "yellow" },
      { "carModel": "Porsche", "price": 30000, "color": "green" }
    ];

    const opts = {
      fields: [
        {
          label: 'Car Model',
          value: 'carModel'
        },
        {
          label: 'Price',
          value: "price",
        },
        {
          label: 'Color',
          value: 'color'
        },
        {
          label: 'Manual',
          value: 'manual',
          default: false
        }
      ]
    };


    const json2csvParser = new Parser();
    const csv = json2csvParser.parse(data);

vadimcoder avatar Jun 16 '24 01:06 vadimcoder