excel-reader.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import * as ExcelJS from 'exceljs';
  2. import { ReadSectionData, LayoutSectionField, SectionTypeEnum, FieldTypeEnum, ImportProgress } from './types';
  3. export class ExcelReaderService {
  4. async readExcelFile(
  5. fileBuffer: Buffer,
  6. layoutConfig: any,
  7. onProgress: (progress: ImportProgress) => void
  8. ): Promise<ReadSectionData[]> {
  9. const workbook = new ExcelJS.Workbook();
  10. await workbook.xlsx.load(fileBuffer);
  11. const results: ReadSectionData[] = [];
  12. const totalSections = layoutConfig.sections?.length || 0;
  13. // Initialize progress
  14. onProgress({
  15. importId: 0, // Will be set by caller
  16. status: 'processing',
  17. currentSection: '',
  18. currentRow: 0,
  19. totalRows: 0,
  20. errors: [],
  21. processedSections: 0,
  22. totalSections
  23. });
  24. for (let sectionIndex = 0; sectionIndex < totalSections; sectionIndex++) {
  25. const section = layoutConfig.sections[sectionIndex];
  26. const worksheet = workbook.getWorksheet(section.sheetName);
  27. if (!worksheet) {
  28. onProgress({
  29. importId: 0,
  30. status: 'processing',
  31. currentSection: section.name,
  32. currentRow: 0,
  33. totalRows: 0,
  34. errors: [`Worksheet '${section.sheetName}' not found`],
  35. processedSections: sectionIndex + 1,
  36. totalSections
  37. });
  38. continue;
  39. }
  40. const sectionData = await this.processSection(worksheet, section, sectionIndex, totalSections, onProgress);
  41. results.push(sectionData);
  42. }
  43. return results;
  44. }
  45. private async processSection(
  46. worksheet: ExcelJS.Worksheet,
  47. section: any,
  48. sectionIndex: number,
  49. totalSections: number,
  50. onProgress: (progress: ImportProgress) => void
  51. ): Promise<ReadSectionData> {
  52. const startingRow = section.startingRow || 1;
  53. const endingRow = section.endingRow || worksheet.rowCount;
  54. // Get headers from the first row (assuming row 1 has headers)
  55. const headers: string[] = [];
  56. const headerRow = worksheet.getRow(1);
  57. headerRow.eachCell((cell) => {
  58. headers.push(cell.text || '');
  59. });
  60. // Process data rows
  61. const data: Record<string, any>[] = [];
  62. const totalRows = endingRow - startingRow + 1;
  63. for (let rowNum = startingRow; rowNum <= endingRow; rowNum++) {
  64. const row = worksheet.getRow(rowNum);
  65. if (!row.hasValues) continue;
  66. const rowData: Record<string, any> = {};
  67. // Map cell values based on field configuration
  68. for (const field of section.fields || []) {
  69. const cellAddress = this.parseCellAddress(field.cellPosition);
  70. const cell = row.getCell(cellAddress.col);
  71. if (cell && cell.value !== null && cell.value !== undefined) {
  72. rowData[field.importTableColumnName] = this.convertCellValue(
  73. cell.value,
  74. field.parsedType || FieldTypeEnum.String
  75. );
  76. }
  77. }
  78. data.push(rowData);
  79. // Update progress every 100 rows
  80. if (rowNum % 100 === 0 || rowNum === endingRow) {
  81. onProgress({
  82. importId: 0,
  83. status: 'processing',
  84. currentSection: section.name,
  85. currentRow: rowNum - startingRow + 1,
  86. totalRows,
  87. errors: [],
  88. processedSections: sectionIndex,
  89. totalSections
  90. });
  91. }
  92. }
  93. return {
  94. id: section.id || 0,
  95. name: section.name || '',
  96. tableName: section.tableName || '',
  97. sheet: section.sheetName || '',
  98. type: section.type || '',
  99. startingRow,
  100. endingRow,
  101. parsedType: this.mapSectionType(section.type),
  102. fields: this.mapFields(section.fields || []),
  103. data
  104. };
  105. }
  106. private parseCellAddress(cellPosition: string): { row: number; col: number } {
  107. const match = cellPosition.match(/([A-Z]+)(\d+)/);
  108. if (!match) return { row: 1, col: 1 };
  109. const col = match[1].charCodeAt(0) - 'A'.charCodeAt(0) + 1;
  110. const row = parseInt(match[2]);
  111. return { row, col };
  112. }
  113. private mapSectionType(type: string): SectionTypeEnum {
  114. switch (type?.toLowerCase()) {
  115. case 'grid':
  116. return SectionTypeEnum.Grid;
  117. case 'properties':
  118. return SectionTypeEnum.Properties;
  119. default:
  120. return SectionTypeEnum.Unknown;
  121. }
  122. }
  123. private mapFields(fields: any[]): LayoutSectionField[] {
  124. return fields.map((field, index) => ({
  125. id: field.id || index,
  126. cellPosition: field.cellPosition || '',
  127. name: field.name || '',
  128. dataType: field.dataType || 'string',
  129. dataTypeFormat: field.dataTypeFormat,
  130. importTableColumnName: field.importTableColumnName || field.name || `column_${index}`,
  131. importColumnOrderNumber: field.importColumnOrderNumber || index,
  132. parsedType: this.mapFieldType(field.dataType)
  133. }));
  134. }
  135. private mapFieldType(dataType: string): FieldTypeEnum {
  136. const type = dataType?.toLowerCase();
  137. switch (type) {
  138. case 'time':
  139. return FieldTypeEnum.Time;
  140. case 'decimal':
  141. case 'number':
  142. case 'float':
  143. return FieldTypeEnum.Decimal;
  144. case 'date':
  145. return FieldTypeEnum.Date;
  146. case 'int':
  147. case 'integer':
  148. case 'numeric':
  149. return FieldTypeEnum.Numeric;
  150. default:
  151. return FieldTypeEnum.String;
  152. }
  153. }
  154. private convertCellValue(value: any, fieldType: FieldTypeEnum): any {
  155. if (value === null || value === undefined) return null;
  156. switch (fieldType) {
  157. case FieldTypeEnum.Time:
  158. if (typeof value === 'number') {
  159. // Excel time is fraction of a day
  160. return value * 24 * 60 * 60 * 1000; // Convert to milliseconds
  161. }
  162. return value;
  163. case FieldTypeEnum.Decimal:
  164. return parseFloat(value.toString()) || 0;
  165. case FieldTypeEnum.Date:
  166. if (typeof value === 'number') {
  167. // Excel date is days since 1900-01-01
  168. const excelEpoch = new Date(1900, 0, 1);
  169. return new Date(excelEpoch.getTime() + (value - 1) * 24 * 60 * 60 * 1000);
  170. }
  171. return new Date(value);
  172. case FieldTypeEnum.Numeric:
  173. return parseInt(value.toString()) || 0;
  174. case FieldTypeEnum.String:
  175. default:
  176. return value.toString();
  177. }
  178. }
  179. }