@@ -6,26 +6,15 @@ import type { SerializationOptions } from '@syncfusion/ej2-react-spreadsheet';
66function App ( ) : React . ReactElement {
77 const spreadsheetRef = React . useRef < SpreadsheetComponent | null > ( null ) ;
88
9+ const [ savedJson , setSavedJson ] = React . useState < string | null > ( null ) ;
10+
911 const handleSaveAsJson = React . useCallback ( async ( ) => {
1012 if ( ! spreadsheetRef . current ) return ;
11- try {
12- const options : SerializationOptions = { } ;
13- const result : any = await spreadsheetRef . current . saveAsJson ( options ) ;
14- const jsonObject : any = result && ( result as any ) . jsonObject ? ( result as any ) . jsonObject : result ;
15- const content : string = typeof jsonObject === 'string' ? jsonObject : JSON . stringify ( jsonObject , null , 2 ) ;
16- const blob : Blob = new Blob ( [ content ] , { type : 'application/json' } ) ;
17- const url : string = URL . createObjectURL ( blob ) ;
18- const a : HTMLAnchorElement = document . createElement ( 'a' ) ;
19- a . href = url ;
20- a . download = 'spreadsheet.json' ;
21- document . body . appendChild ( a ) ;
22- a . click ( ) ;
23- a . remove ( ) ;
24- URL . revokeObjectURL ( url ) ;
25- } catch ( e : unknown ) {
26- console . error ( 'Failed to save as JSON' , e ) ;
27- alert ( 'Failed to save as JSON.' ) ;
28- }
13+ const options : SerializationOptions = { } ;
14+ const result : any = await spreadsheetRef . current . saveAsJson ( options ) ;
15+ const jsonObject : any = result && ( result as any ) . jsonObject ? ( result as any ) . jsonObject : result ;
16+ const content : string = typeof jsonObject === 'string' ? jsonObject : JSON . stringify ( jsonObject , null , 2 ) ;
17+ setSavedJson ( content ) ;
2918 } , [ ] ) ;
3019
3120 return (
@@ -40,6 +29,15 @@ function App(): React.ReactElement {
4029 < div >
4130 < SpreadsheetComponent id = "spreadsheet" ref = { spreadsheetRef } />
4231 </ div >
32+
33+ { savedJson && (
34+ < div style = { { marginTop : 12 } } >
35+ < strong > Saved JSON</ strong >
36+ < pre style = { { maxHeight : 200 , overflow : 'auto' , background : '#f5f5f5' , padding : 12 } } >
37+ { savedJson }
38+ </ pre >
39+ </ div >
40+ ) }
4341 </ div >
4442 </ div >
4543 ) ;
0 commit comments