Skip to content

Patch fix TabularImportExport - #27

Merged
Bapham12 merged 1 commit into
BKDatabase:mainfrom
Bapham12:main
Aug 8, 2026
Merged

Bapham12 merged 1 commit into
BKDatabase:mainfrom
Bapham12:main

Conversation

@Bapham12

@Bapham12 Bapham12 commented Aug 8, 2026

Copy link
Copy Markdown
Member

Sửa lỗi jquery.js:3783 jQuery.Deferred exception: Cannot read properties of undefined (reading 'aoa_to_sheet') TypeError: Cannot read properties of undefined (reading 'aoa_to_sheet')const xlsxScript = mw.loader.getScript( 'https://cdn.jsdelivr.net/npm/js-xlsx@0.8.22/dist/xlsx.full.min.js' );
Cùng với thêm patch sau, nhằm sửa lỗi không tải được tập tin Excel:

convertToExcel( jsondata, pageData ) {
			const wb = new Workbook();
			const dataArray = [ [] ];
			const localizedIndices = [];
			const languageCodes = new Set();

			jsondata.schema.fields.forEach( ( field, idx ) => {
				dataArray[ 0 ].push( field.name );
				// Kiểm tra an toàn trước khi lấy keys
				if ( isNonNullObject( field.title ) ) {
					addKeysToSet( languageCodes, field.title );
				}
				if ( field && field.type === 'localized' ) {
					localizedIndices.push( idx );
				}
			} );

			jsondata.data.forEach( ( row ) => {
				const copyOfRow = deepCopyArray( row );
				localizedIndices.forEach( ( idx ) => {
					if ( isNonNullObject( copyOfRow[ idx ] ) ) {
						addKeysToSet( languageCodes, copyOfRow[ idx ] );
						copyOfRow[ idx ] = Object.values( copyOfRow[ idx ] )[ 0 ];
					}
				} );
				dataArray.push( copyOfRow );
			} );

			const ws = XLSX.utils.aoa_to_sheet( dataArray );
			const ws_name = mw.config.get( 'wgTitle' )
				.replace( /[/\\*'?[\]:]/g, ' ' )
				.toLowerCase()
				.slice( 0, 31 );
			ws[ '!merges' ] = [];
			wb.SheetNames.push( ws_name );
			wb.Sheets[ ws_name ] = ws;

			const copyDataWithReferences = ( dArray, dSheetName ) => {
				return dArray.map( ( row, i ) => {
					return row.map( ( cell, j ) => {
						const cellName = XLSX.utils.encode_cell( { r: i, c: j } );
						return { f: `'${ dSheetName }'!${ cellName }` };
					} );
				} );
			};

			// Hàm được sửa lỗi null-check an toàn tại đây
			function createTranslatedArrayOfArrays( tDataArray, lang ) {
				jsondata.schema.fields.forEach( ( field, idx ) => {
					// An toàn hóa field.title nếu nó là null, undefined hoặc string
					const titleObj = isNonNullObject( field.title ) ? field.title : {};
					const entries = Object.entries( titleObj );
					const firstEntry = entries.length > 0 ? entries[ 0 ] : [ '', field.name || '' ];

					const cellValue = titleObj[ lang ] || firstEntry[ 1 ] || field.name || '';
					const cellLanguage = titleObj[ lang ] ? lang : ( firstEntry[ 0 ] || '' );
					const cell = { v: cellValue, c: [ { a: 'localized', t: cellLanguage, hidden: true } ] };
					tDataArray[ 0 ][ idx ] = cell;
				} );

				jsondata.data.forEach( ( row, rowIndex ) => {
					localizedIndices.forEach( ( idx ) => {
						const cellObj = row[ idx ];
						if ( isNonNullObject( cellObj ) ) {
							const entries = Object.entries( cellObj );
							const firstEntry = entries.length > 0 ? entries[ 0 ] : [ '', '' ];

							const cellValue = cellObj[ lang ] || firstEntry[ 1 ] || null;
							const cellLanguage = cellObj[ lang ] ? lang : ( firstEntry[ 0 ] || '' );
							const cell = { v: cellValue, c: [ { a: 'localized', t: cellLanguage, hidden: true } ] };
							tDataArray[ rowIndex + 1 ][ idx ] = cell;
						}
					} );
				} );
				return tDataArray;
			}

			const dataArrayWithReferences = copyDataWithReferences( dataArray, ws_name );

			[ ...languageCodes ].sort().forEach( ( lang ) => {
				const langSheetName = lang;
				const langWs = XLSX.utils.aoa_to_sheet( createTranslatedArrayOfArrays( deepCopyArray( dataArrayWithReferences ), lang ) );
				wb.SheetNames.push( langSheetName );
				wb.Sheets[ langSheetName ] = langWs;
			} );

			const pageUrl = new URL( mw.config.get( 'wgArticlePath' ).replace( '$1', pageData.title ), window.location );
			const historyUrl = new URL( mw.config.get( 'wgScript' ), window.location );
			historyUrl.searchParams.set( 'title', pageData.title );
			historyUrl.searchParams.set( 'action', 'history' );

			wb.Props = {
				Title: pageData.title,
				Company: mw.config.get( 'wgSiteName' ),
				SheetNames: wb.SheetNames,
				Worksheets: wb.SheetNames.length
			};

			wb.Custprops = {
				Revision: pageData.revisions[ 0 ].revid,
				LastModified: pageData.revisions[ 0 ].timestamp,
				Url: pageUrl.toString(),
				Contributors: historyUrl.toString(),
				Sources: jsondata.sources || '',
				LicenseCode: jsondata.license,
				Software: 'Tabular Import/Export-gadget'
			};

			for ( const language in jsondata.description ) {
				wb.Custprops[ `Description.${ language }` ] = jsondata.description[ language ];
			}

			if ( jsondata.mediaWikiCategories && jsondata.mediaWikiCategories.length > 0 ) {
				wb.Custprops.MediaWikiCategories = jsondata.mediaWikiCategories.map( ( cat ) => {
					let str = 'Category:' + cat.name;
					if ( cat.sort ) {
						str += '|' + cat.sort;
					}
					return str;
				} ).join( ', ' );
			}

			return wb;
		}

@Bapham12

Bapham12 commented Aug 8, 2026

Copy link
Copy Markdown
Member Author

Đã test trên production

@Bapham12

Bapham12 commented Aug 8, 2026

Copy link
Copy Markdown
Member Author

Test thành công trên production, có thể merge ngay bây giờ

@Bapham12
Bapham12 merged commit 43fc48a into BKDatabase:main Aug 8, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant