From ae63b58ff4c03a044660180afd22290ba6e29343 Mon Sep 17 00:00:00 2001 From: toolsmythe Date: Thu, 20 Jul 2017 11:15:19 -0700 Subject: [PATCH 1/2] Update csv.service.ts New Functionality: Suppress automatic generation of column headings in csv output. Perhaps you don't want any headings or perhaps you want to name them something different than the object property names. Now you have that option. Look for J. Pagakis 20170601 comments. Bug fix: At least for me, the download in IE was failing exactly 50% of the time (it would fail on odd attempts and succeed on even attempts - just like Star Trek movies). This change seems to have corrected that. Look for J.Pagakis 20170719 comments. --- src/csv.service.ts | 102 +++++++++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 40 deletions(-) diff --git a/src/csv.service.ts b/src/csv.service.ts index f5252ac..1b02732 100755 --- a/src/csv.service.ts +++ b/src/csv.service.ts @@ -2,62 +2,84 @@ import {Injectable} from '@angular/core'; @Injectable() - -export class CsvService { - - constructor() { - // Blank Constructor for Demo Purpose - } - +export class  CsvService { + constructor() { } + + /////////////////////////////////////////////////////////////////    + // J. Pagakis 20170601    + // Mods to make the header suppressable    + /////////////////////////////////////////////////////////////////    + // J. Pagakis 20170719    + // Mods to fix download failures in IE + // Download CSV - download(data:any, filename:string){ - var csvData = this.ConvertToCSV(data); - var a: any = document.createElement("a"); - a.setAttribute('style', 'display:none;'); + download( data:any, filename:string, suppressHeader:boolean = false /* J. Pagakis 20170601 */ ) + { + var csvData = this.ConvertToCSV( data, suppressHeader ); //J. Pagakis 20170601 + var a: any = document.createElement( "a" ); + a.setAttribute( 'style', 'display:none;' ); document.body.appendChild(a); - var blob = new Blob([csvData], { type: 'text/csv' }); - var url= window.URL.createObjectURL(blob); + var blob = new Blob( [csvData], { type: 'text/csv' } ); + var url= window.URL.createObjectURL( blob ); a.href = url; - var isIE = /*@cc_on!@*/false || !!( document).documentMode; + var isIE = /*@cc_on!@*/false || !!( document ).documentMode; + var retVal; - if (isIE) - { - var retVal = navigator.msSaveBlob(blob, filename+'.csv'); + if ( isIE ) + { + retVal = navigator.msSaveOrOpenBlob( blob, filename+'.csv' );  //J.Pagakis 20170719 - changed to msSaveOrOpenBlob instead on msSaveBlob } - else{ + else + { a.download = filename+'.csv'; } - // If you will any error in a.download then dont worry about this. - a.click(); - } - - - // convert Json to CSV data - ConvertToCSV(objArray:any) { - var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray; + + // If you will any error in a.download then dont worry about this. + if ( !isIE )  //J.Pagakis 20170719 - a.click( ) was failing 50% of the time in IE; apparently not needed. + { + a.click(); + } + + + // convert Json to CSV data + ConvertToCSV( objArray:any, suppressHeader:boolean = false /* J. Pagakis 20170601 */ ) { + var array = typeof objArray != 'object' ? JSON.parse( objArray ) : objArray; var str = ''; var row = ""; - - for (var index in objArray[0]) { - //Now convert each value to string and comma-seprated + + for ( var index in objArray[ 0 ] ) + { + //Convert each value to string and comma-seprated row += index + ','; } - row = row.slice(0, -1); + + row = row.slice( 0, -1 ); + //append Label row with line break - str += row + '\r\n'; - - for (var i = 0; i < array.length; i++) { + + if ( ! suppressHeader )  // J. Pagakis 20170601 + { + str += row + '\r\n'; + } + + for ( var i = 0; i < array.length; i++ ) + { var line = ''; - for (var index in array[i]) { - if (line != '') line += ',' - - line += '"'+array[i][index]+'"'; + for ( var index in array[ i ] ) + { + if ( line != '' ) line += ','; + + line += '"' + array[ i ][ index ] + '"'; } - + str += line + '\r\n'; } - + return str; } -} + } + + + + From c139daf50187dba7a57e5eaa88c8d56d140843c7 Mon Sep 17 00:00:00 2001 From: toolsmythe Date: Thu, 20 Jul 2017 11:20:13 -0700 Subject: [PATCH 2/2] Update csv.service.ts Missed a closing brace on the download function. --- src/csv.service.ts | 75 ++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/src/csv.service.ts b/src/csv.service.ts index 1b02732..aa089b7 100755 --- a/src/csv.service.ts +++ b/src/csv.service.ts @@ -40,46 +40,43 @@ export class  CsvService { { a.click(); } - - - // convert Json to CSV data - ConvertToCSV( objArray:any, suppressHeader:boolean = false /* J. Pagakis 20170601 */ ) { - var array = typeof objArray != 'object' ? JSON.parse( objArray ) : objArray; - var str = ''; - var row = ""; - - for ( var index in objArray[ 0 ] ) - { - //Convert each value to string and comma-seprated - row += index + ','; - } - - row = row.slice( 0, -1 ); - - //append Label row with line break - - if ( ! suppressHeader )  // J. Pagakis 20170601 - { - str += row + '\r\n'; - } - - for ( var i = 0; i < array.length; i++ ) + } + + + // convert Json to CSV data + ConvertToCSV( objArray:any, suppressHeader:boolean = false /* J. Pagakis 20170601 */ ) { + var array = typeof objArray != 'object' ? JSON.parse( objArray ) : objArray; + var str = ''; + var row = ""; + + for ( var index in objArray[ 0 ] ) + { + //Convert each value to string and comma-seprated + row += index + ','; + } + + row = row.slice( 0, -1 ); + + //append Label row with line break + + if ( ! suppressHeader )  // J. Pagakis 20170601 + { + str += row + '\r\n'; + } + + for ( var i = 0; i < array.length; i++ ) + { + var line = ''; + for ( var index in array[ i ] ) { - var line = ''; - for ( var index in array[ i ] ) - { - if ( line != '' ) line += ','; - - line += '"' + array[ i ][ index ] + '"'; - } - - str += line + '\r\n'; + if ( line != '' ) line += ','; + + line += '"' + array[ i ][ index ] + '"'; } - - return str; + + str += line + '\r\n'; } + + return str; } - - - - +}