@@ -70,61 +70,61 @@ <h5>Standalone JS (IIFE)</h5>
7070 < script src ="https://cdn.jsdelivr.net/npm/excel-builder-vanilla@3.0.1/dist/excel-builder.iife.js "> </ script >
7171
7272 < script >
73- function downloader ( options ) {
74- // when using IE/Edge, then use different download call
75- if ( typeof navigator . msSaveOrOpenBlob === 'function' ) {
76- navigator . msSaveOrOpenBlob ( options . blob , options . filename ) ;
77- } else {
78- // this trick will generate a temp <a /> tag
79- // the code will then trigger a hidden click for it to start downloading
80- const link = document . createElement ( 'a' ) ;
81- const url = URL . createObjectURL ( options . blob ) ;
73+ function downloader ( options ) {
74+ // when using IE/Edge, then use different download call
75+ if ( typeof navigator . msSaveOrOpenBlob === 'function' ) {
76+ navigator . msSaveOrOpenBlob ( options . blob , options . filename ) ;
77+ } else {
78+ // this trick will generate a temp <a /> tag
79+ // the code will then trigger a hidden click for it to start downloading
80+ const link = document . createElement ( 'a' ) ;
81+ const url = URL . createObjectURL ( options . blob ) ;
8282
83- if ( link && document ) {
84- link . textContent = 'download' ;
85- link . href = url ;
86- link . setAttribute ( 'download' , options . filename ) ;
83+ if ( link && document ) {
84+ link . textContent = 'download' ;
85+ link . href = url ;
86+ link . setAttribute ( 'download' , options . filename ) ;
8787
88- // set the visibility to hidden so there is no effect on your web-layout
89- link . style . visibility = 'hidden' ;
88+ // set the visibility to hidden so there is no effect on your web-layout
89+ link . style . visibility = 'hidden' ;
9090
91- // this part will append the anchor tag, trigger a click (for download to start) and finally remove the tag once completed
92- document . body . appendChild ( link ) ;
93- link . click ( ) ;
94- document . body . removeChild ( link ) ;
95- }
91+ // this part will append the anchor tag, trigger a click (for download to start) and finally remove the tag once completed
92+ document . body . appendChild ( link ) ;
93+ link . click ( ) ;
94+ document . body . removeChild ( link ) ;
9695 }
9796 }
97+ }
9898
99- function exportExcel ( ) {
100- const originalData = [
101- [ 'Artist' , 'Album' , 'Price' ] ,
102- [ 'Buckethead' , 'Albino Slug' , 8.99 ] ,
103- [ 'Buckethead' , 'Electric Tears' , 13.99 ] ,
104- [ 'Buckethead' , 'Colma' , 11.34 ] ,
105- [ 'Crystal Method' , 'Vegas' , 10.54 ] ,
106- [ 'Crystal Method' , 'Tweekend' , 10.64 ] ,
107- [ 'Crystal Method' , 'Divided By Night' , 8.99 ] ,
108- ] ;
109- const artistWorkbook = new window . ExcelBuilder . Workbook ( ) ;
110- const albumList = artistWorkbook . createWorksheet ( { name : 'Artists' } ) ;
111- albumList . setData ( originalData ) ;
112- artistWorkbook . addWorksheet ( albumList ) ;
99+ function exportExcel ( ) {
100+ const originalData = [
101+ [ 'Artist' , 'Album' , 'Price' ] ,
102+ [ 'Buckethead' , 'Albino Slug' , 8.99 ] ,
103+ [ 'Buckethead' , 'Electric Tears' , 13.99 ] ,
104+ [ 'Buckethead' , 'Colma' , 11.34 ] ,
105+ [ 'Crystal Method' , 'Vegas' , 10.54 ] ,
106+ [ 'Crystal Method' , 'Tweekend' , 10.64 ] ,
107+ [ 'Crystal Method' , 'Divided By Night' , 8.99 ] ,
108+ ] ;
109+ const artistWorkbook = new window . ExcelBuilder . Workbook ( ) ;
110+ const albumList = artistWorkbook . createWorksheet ( { name : 'Artists' } ) ;
111+ albumList . setData ( originalData ) ;
112+ artistWorkbook . addWorksheet ( albumList ) ;
113113
114- new window . ExcelBuilder . createExcelFile ( artistWorkbook , { type : 'blob' } ) . then ( ( excelBlob ) => {
115- const downloadOptions = {
116- filename : 'Artist WB.xlsx' ,
117- format : 'xlsx' ,
118- } ;
114+ new window . ExcelBuilder . createExcelFile ( artistWorkbook , { type : 'blob' } ) . then ( excelBlob => {
115+ const downloadOptions = {
116+ filename : 'Artist WB.xlsx' ,
117+ format : 'xlsx' ,
118+ } ;
119119
120- // start downloading but add the Blob property only on the start download not on the event itself
121- downloader ( { ...downloadOptions , blob : excelBlob , data : albumList . data } ) ;
122- } ) ;
123- }
124-
125- document . querySelector ( 'button#export' ) . addEventListener ( 'click' , ( ) => {
126- exportExcel ( ) ;
120+ // start downloading but add the Blob property only on the start download not on the event itself
121+ downloader ( { ...downloadOptions , blob : excelBlob , data : albumList . data } ) ;
127122 } ) ;
123+ }
124+
125+ document . querySelector ( 'button#export' ) . addEventListener ( 'click' , ( ) => {
126+ exportExcel ( ) ;
127+ } ) ;
128128 </ script >
129129 </ body >
130130</ html >
0 commit comments