Export to CSV file from layer

782
3
Jump to solution
12-04-2018 08:39 AM
MRReddy
Occasional Contributor

Below code works in chrome, but it getting syntax error in IE 11 because of expression =>

i used the => expression to filter data,

Any suggestions

Thanks

function customExportCSV(evt) {
 
 var data = propertylayer._graphicsVal;
 var csv = convertArrayOfObjectsToCSV({
 data: data
 });
if (!csv.match(/^data:text\/csv/i)) {
 csv = 'data:text/csv;charset=utf-8,' + csv;
 }
 //var blob = new Blob([csv], { type: "text/csv" });
 //if (navigator.msSaveBlob) { // IE 10+
 // //var blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
 // //navigator.msSaveBlob(blob, "Exportdata.csv");
 // navigator.msSaveBlob(blob, "csvname.csv")
 //}
 var encodedUri = encodeURI(csv);
 var link = document.createElement('a');
 link.setAttribute('href', encodedUri);
 link.setAttribute('download', "Exportdata.csv");
 link.click();
 
 
 
 }
 function convertArrayOfObjectsToCSV(value) {
 
 var result, ctr, keys, columnDelimiter, lineDelimiter, data;
data = Array.from(new Set(value.data)).filter(d => d).map(d => d.attributes) || null;
if (!data || !data.length) {
 return null;
 }

columnDelimiter = value.columnDelimiter || ',';
 lineDelimiter = value.lineDelimiter || '\n';
keys = Object.keys(data[1]);
 result = '';
 result += keys.join(columnDelimiter);
 result += lineDelimiter;
data.forEach(function (item) {
 ctr = 0;
 keys.forEach(function (key) {
 if (ctr > 0)
 result += columnDelimiter;
 result += item[key];
 ctr++;
 });
 result += lineDelimiter;
 });
 
 return result;
 }
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Malla,

   Once again a ES6 issue in IE array.from is not supported.

Array.from() - JavaScript | MDN 

You need to check which ES6 method are supported by IE11 before you attempt to use them.

The above link provides a polyfill for Array.from that you can added to your code.

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Malla,

   IE 11 is not capable of using some ES6 code (i.e. fat arrows/arrow functions). So you have to convert it to ES5 code:

data = Array.from(new Set(value.data)).filter(function (d) {return d;}).map(function (d){return d.attributes;}) || null;
0 Kudos
MRReddy
Occasional Contributor

Robert,

Tried with your code it got below alert in IE

and it works in chrome

Thanks

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Malla,

   Once again a ES6 issue in IE array.from is not supported.

Array.from() - JavaScript | MDN 

You need to check which ES6 method are supported by IE11 before you attempt to use them.

The above link provides a polyfill for Array.from that you can added to your code.

View solution in original post