1+ "use strict" ;
2+
3+ var _interopRequireDefault = require ( "@babel/runtime/helpers/interopRequireDefault" ) ;
4+
5+ var _interopRequireWildcard = require ( "@babel/runtime/helpers/interopRequireWildcard" ) ;
6+
7+ Object . defineProperty ( exports , "__esModule" , {
8+ value : true
9+ } ) ;
10+ exports . $toExcel = exports . $readExcel = void 0 ;
11+
12+ var _nodeFetch = _interopRequireWildcard ( require ( "node-fetch" ) ) ;
13+
14+ var _index = require ( "../index" ) ;
15+
16+ var _xlsx = _interopRequireDefault ( require ( "xlsx" ) ) ;
17+
18+ /**
19+ * @license
20+ * Copyright 2021, JsData. All rights reserved.
21+ *
22+ * This source code is licensed under the MIT license found in the
23+ * LICENSE file in the root directory of this source tree.
24+
25+ * Unless required by applicable law or agreed to in writing, software
26+ * distributed under the License is distributed on an "AS IS" BASIS,
27+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+ * See the License for the specific language governing permissions and
29+ * limitations under the License.
30+ * ==========================================================================
31+ */
32+ const $readExcel = async ( filePath , options ) => {
33+ const {
34+ sheet,
35+ method,
36+ headers
37+ } = {
38+ sheet : 0 ,
39+ method : "GET" ,
40+ headers : { } ,
41+ ...options
42+ } ;
43+
44+ if ( filePath . startsWith ( "http" ) || filePath . startsWith ( "https" ) ) {
45+ return new Promise ( resolve => {
46+ ( 0 , _nodeFetch . default ) ( filePath , {
47+ method,
48+ headers
49+ } ) . then ( response => {
50+ if ( response . status !== 200 ) {
51+ throw new Error ( `Failed to load ${ filePath } ` ) ;
52+ }
53+
54+ response . arrayBuffer ( ) . then ( arrBuf => {
55+ const arrBufInt8 = new Uint8Array ( arrBuf ) ;
56+
57+ const workbook = _xlsx . default . read ( arrBufInt8 , {
58+ type : "array"
59+ } ) ;
60+
61+ const worksheet = workbook . Sheets [ workbook . SheetNames [ sheet ] ] ;
62+
63+ const data = _xlsx . default . utils . sheet_to_json ( worksheet ) ;
64+
65+ const df = new _index . DataFrame ( data ) ;
66+ resolve ( df ) ;
67+ } ) ;
68+ } ) . catch ( err => {
69+ throw new Error ( err ) ;
70+ } ) ;
71+ } ) ;
72+ } else {
73+ return new Promise ( resolve => {
74+ const workbook = _xlsx . default . readFile ( filePath ) ;
75+
76+ const worksheet = workbook . Sheets [ workbook . SheetNames [ sheet ] ] ;
77+
78+ const data = _xlsx . default . utils . sheet_to_json ( worksheet ) ;
79+
80+ const df = new _index . DataFrame ( data ) ;
81+ resolve ( df ) ;
82+ } ) ;
83+ }
84+ } ;
85+
86+ exports . $readExcel = $readExcel ;
87+
88+ const $toExcel = ( df , options ) => {
89+ let {
90+ filePath,
91+ sheetName
92+ } = {
93+ filePath : "./output.xlsx" ,
94+ sheetName : "Sheet1" ,
95+ ...options
96+ } ;
97+
98+ if ( ! filePath . endsWith ( ".xlsx" ) ) {
99+ filePath = filePath + ".xlsx" ;
100+ }
101+
102+ let data ;
103+
104+ if ( df . $isSeries ) {
105+ const row = df . values ;
106+ const col = df . columns ;
107+ data = [ col , ...row . map ( x => [ x ] ) ] ;
108+ } else {
109+ const row = df . values ;
110+ const cols = df . columns ;
111+ data = [ cols , ...row ] ;
112+ }
113+
114+ const worksheet = _xlsx . default . utils . aoa_to_sheet ( data ) ;
115+
116+ const wb = _xlsx . default . utils . book_new ( ) ;
117+
118+ _xlsx . default . utils . book_append_sheet ( wb , worksheet , sheetName ) ;
119+
120+ _xlsx . default . writeFile ( wb , `${ filePath } ` ) ;
121+ } ;
122+
123+ exports . $toExcel = $toExcel ;
0 commit comments