11import * as XLSX from 'xlsx' ;
22import * as fs from 'node:fs' ;
33
4+ // Type definitions for data analysis
5+ interface TypeAnalysisInfo {
6+ types : string [ ] ;
7+ sampleCount : number ;
8+ totalCount : number ;
9+ samples : any [ ] ;
10+ }
11+
12+ type TypeAnalysisRecord = Record < string , TypeAnalysisInfo > ;
13+
414// check excel file
515const excelFile = '教育公益开放式数据库.xlsx' ;
616
717if ( ! fs . existsSync ( excelFile ) ) {
8- console . log ( 'Excel文件不存在:' , excelFile ) ;
918 console . log ( '当前目录:' , process . cwd ( ) ) ;
1019 console . log ( '目录内容:' ) ;
11- fs . readdirSync ( '.' ) . forEach ( ( file ) => {
20+ for ( const file of fs . readdirSync ( '.' ) ) {
1221 console . log ( ' ' , file ) ;
13- } ) ;
14- process . exit ( 1 ) ;
22+ }
23+ throw new Error ( `Excel文件不存在: ${ excelFile } ` ) ;
1524}
1625
1726try {
1827 // read excel file
19- console . log ( '正在分析Excel文件:' , excelFile ) ;
28+ console . log ( '正在分析Excel文件: ' , excelFile ) ;
2029 const workbook = XLSX . readFile ( excelFile ) ;
2130
22- console . log ( '\n=== sheet info ===' ) ;
23- console . log ( ' sheet count:' , workbook . SheetNames . length ) ;
24- console . log ( 'sheet names:' , workbook . SheetNames ) ;
31+ console . log (
32+ `\n=== sheet info ===\nsheet count: ${ workbook . SheetNames . length } \nsheet names: ${ workbook . SheetNames . join ( ', ' ) } ` ,
33+ ) ;
2534
2635 // analyze each sheet
27- workbook . SheetNames . forEach ( ( sheetName , index ) => {
36+ for ( let index = 0 ; index < workbook . SheetNames . length ; index ++ ) {
37+ const sheetName = workbook . SheetNames [ index ] ;
2838 console . log ( `\n=== sheet ${ index + 1 } : ${ sheetName } ===` ) ;
2939
3040 const worksheet = workbook . Sheets [ sheetName ] ;
@@ -35,34 +45,28 @@ try {
3545 if ( data . length > 0 ) {
3646 console . log ( '\ncolumn names (fields):' ) ;
3747 const columns = Object . keys ( data [ 0 ] ) ;
38- columns . forEach ( ( col , i ) => {
48+ for ( let i = 0 ; i < columns . length ; i ++ ) {
49+ const col = columns [ i ] ;
3950 console . log ( ` ${ i + 1 } . ${ col } ` ) ;
40- } ) ;
51+ }
4152
4253 console . log ( '\nfirst 3 rows data example:' ) ;
43- data . slice ( 0 , 3 ) . forEach ( ( row , i ) => {
54+ for ( let i = 0 ; i < Math . min ( 3 , data . length ) ; i ++ ) {
55+ const row = data [ i ] ;
4456 console . log ( `\nrow ${ i + 1 } :` ) ;
45- Object . entries ( row ) . forEach ( ( [ key , value ] ) => {
57+ for ( const [ key , value ] of Object . entries ( row ) ) {
4658 const displayValue =
4759 typeof value === 'string' && value . length > 50
4860 ? value . substring ( 0 , 50 ) + '...'
4961 : value ;
5062 console . log ( ` ${ key } : ${ displayValue } ` ) ;
51- } ) ;
52- } ) ;
63+ }
64+ }
5365
5466 // analyze data types
5567 console . log ( '\ndata types analysis:' ) ;
56- const typeAnalysis : Record <
57- string ,
58- {
59- types : string [ ] ;
60- sampleCount : number ;
61- totalCount : number ;
62- samples : any [ ] ;
63- }
64- > = { } ;
65- columns . forEach ( ( col ) => {
68+ const typeAnalysis : TypeAnalysisRecord = { } ;
69+ for ( const col of columns ) {
6670 const values = data
6771 . map ( ( row ) => row [ col ] )
6872 . filter ( ( v ) => v !== undefined && v !== null && v !== '' ) ;
@@ -74,16 +78,16 @@ try {
7478 totalCount : data . length ,
7579 samples : sampleValues ,
7680 } ;
77- } ) ;
81+ }
7882
79- Object . entries ( typeAnalysis ) . forEach ( ( [ col , info ] ) => {
83+ for ( const [ col , info ] of Object . entries ( typeAnalysis ) ) {
8084 console . log ( ` ${ col } :` ) ;
8185 console . log ( ` types: ${ info . types . join ( ', ' ) } ` ) ;
8286 console . log ( ` sample count: ${ info . sampleCount } /${ info . totalCount } ` ) ;
8387 console . log ( ` samples: ${ info . samples . slice ( 0 , 2 ) . join ( ', ' ) } ` ) ;
84- } ) ;
88+ }
8589 }
86- } ) ;
90+ }
8791} catch ( error ) {
8892 console . error ( 'analyze excel file failed:' , error . message ) ;
8993
0 commit comments