Skip to content

Commit 7fcbd13

Browse files
committed
1014442: Updated contents for open/save in json
1 parent c26abd7 commit 7fcbd13

4 files changed

Lines changed: 33 additions & 986 deletions

File tree

Document-Processing/code-snippet/spreadsheet/react/save-as-json-cs1/app/app.jsx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
import * as React from 'react';
2+
import { createRoot } from 'react-dom/client';
23
import { SpreadsheetComponent } from '@syncfusion/ej2-react-spreadsheet';
34

45
function App() {
56
const spreadsheetRef = React.useRef(null);
67

8+
const [savedJson, setSavedJson] = React.useState(null);
9+
710
const handleSaveAsJson = React.useCallback(async () => {
811
if (!spreadsheetRef.current) return;
9-
try {
10-
const options = {};
11-
const result = await spreadsheetRef.current.saveAsJson(options);
12-
const jsonObject = result && result.jsonObject ? result.jsonObject : result;
13-
const content = typeof jsonObject === 'string' ? jsonObject : JSON.stringify(jsonObject, null, 2);
14-
const blob = new Blob([content], { type: 'application/json' });
15-
const url = URL.createObjectURL(blob);
16-
const a = document.createElement('a');
17-
a.href = url;
18-
a.download = 'spreadsheet.json';
19-
document.body.appendChild(a);
20-
a.click();
21-
a.remove();
22-
URL.revokeObjectURL(url);
23-
} catch (e) {
24-
console.error('Failed to save as JSON', e);
25-
alert('Failed to save as JSON.');
26-
}
12+
const options = {};
13+
const result = await spreadsheetRef.current.saveAsJson(options);
14+
const jsonObject = result && (result).jsonObject ? (result).jsonObject : result;
15+
const content = typeof jsonObject === 'string' ? jsonObject : JSON.stringify(jsonObject, null, 2);
16+
setSavedJson(content);
2717
}, []);
2818

2919
return (
@@ -38,6 +28,15 @@ function App() {
3828
<div>
3929
<SpreadsheetComponent id="spreadsheet" ref={spreadsheetRef} />
4030
</div>
31+
32+
{savedJson && (
33+
<div style={{ marginTop: 12 }}>
34+
<strong>Saved JSON</strong>
35+
<pre style={{ maxHeight: 200, overflow: 'auto', background: '#f5f5f5', padding: 12 }}>
36+
{savedJson}
37+
</pre>
38+
</div>
39+
)}
4140
</div>
4241
</div>
4342
);

Document-Processing/code-snippet/spreadsheet/react/save-as-json-cs1/app/app.tsx

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,15 @@ import type { SerializationOptions } from '@syncfusion/ej2-react-spreadsheet';
66
function 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

Comments
 (0)