Need a special offer?Find out if your project fits.
+

is it possible to add custom fields in the CSV export?

Answered
Lekhraj asked on October 7, 2019

Hello Team,

I have added custom fields in the Excel export and I need the same for the CSV export.
is it possible to add custom fields in the CSV export?
please provide an example for it.
Thank you in advance.
 
Regards,
Lekhraj

5 answers

Public
Vera Didenko Vera Didenko Flexmonster October 7, 2019

Hello, Lekhraj,
 
Thank you for writing to us.
 
 
At the moment adding custom fields is supported for Excel, HTML, and PDF export.
As a workaround, we kindly suggest adding your custom data via the callbackHandler: https://www.flexmonster.com/api/exportto/.
If the destinationType parameter is set to plain, then you can modify the data as needed and then write the result to a file.
 
We have prepared a JSFiddle example for illustration: https://jsfiddle.net/flexmonster/jm76ban2/.
 
 
Please let us know if this works fine for you.
 
 
Best Regards,
Vera

Public
Lekhraj October 9, 2019

Hi Vera,
thanks for the example.
getting error on savingAs(blob, "myCsv.csv");
"zone.js:199 Uncaught TypeError: s2.apply is not a function"
 
we have some input fields according to that selection, reports are generated.
for example,
inputFieldsObject = {
state:'ABC',
cities: [we can select MULTIPLE cities],
};
and this object we have to append in the Header of CSV file.
same we have to implement in export to HTML property. 

Public
Vera Didenko Vera Didenko Flexmonster October 10, 2019

Hello, Lekhraj,
 
Thank you for writing to us.
 
Please see our answers below:
 
1) About custom fields:

It is possible to define your custom data (to be included in the export) in the customFields report property, for example:

public pivotReport = {
customFields: [
{
'name': 'State',
'value': 'ABC'
},
{
'name': 'Cities',
'value': 'A, B, C'
}
],
dataSource: {
filename: 'https://cdn.flexmonster.com/data/data.csv'
},
slice: {
rows: [
{ uniqueName: 'Destination' },
{ uniqueName: 'Color' },
{ uniqueName: '[Measures]' }
],
columns: [
{ uniqueName: 'Category' },
{ uniqueName: 'Country' }
],
measures: [
{ uniqueName: 'Price', aggregation: 'sum' },
{ uniqueName: 'Quantity', aggregation: 'sum' }
]
}
};

Then you can get and parse accordingly the customFields and append them to your data (in the exportTo() API call handler) via flexmonster.getReport().customFields.
 
2) About the error with savingAs():

It seems you are using Angular.
In such a case, we kindly recommend using FileSaver.js: https://github.com/eligrey/FileSaver.js/

For example:

import { saveAs } from 'file-saver';

.....

csv() {
this.pivot.flexmonster.exportTo('csv', { destinationType: 'plain' }, (data) => {
// Prepare data for export:
let csv = data['data'];
csv += '\n"Custom data","19191"';
// Save to csv file:
const blob = new Blob([csv]);
saveAs(blob, 'myCsv.csv');
});
}

 
3) About HTML export:

You can set custom data via the header parameter, for example:

this.pivot.flexmonster.exportTo('html', { destinationType: 'file', header: 'My Custom Data' });

 
Please let us know if this helps to get everything working.
 
Best Regards,
Vera

Public
Lekhraj October 23, 2019

Hello Vera,
thanks for the detailed solutions.
we have successfully implemented this solution for the flat layouts.
we have some charts reports, and we have added custom fields and it's working fine for the exportTo Excel. we have to implement the same for the CSV and PDF export.
 
the error I am getting for the chart type reports in CSV and PDF export is

TypeError: Cannot read property 'getReport' of undefined

please find the attached pivot object for the chart type 'LINE'.
 
please provide the sample example for this functionality.
 
Thanks and regards,
Lekhraj

Public
Vera Didenko Vera Didenko Flexmonster October 25, 2019

Hello, Lekhraj,
 
Thank you for writing to us. 
 
Could you please let us know which company you represent? 
We need this for our records.
 
Regarding the issue, it seems that the Flexmonster instance that getReport() is called from is not defined.
We kindly suggest making sure that the Flexmonster object is defined and accessible where getReport() is being called.
 
Please let us know if you have any questions.
 
Best Regards,
Vera

Please login or Register to Submit Answer