UNPKG

bankcode-bic

Version:

Convert bank codes from IBAN to BICs, Name of bank. Currently supports only some selected EU countries.

83 lines (82 loc) 3.05 kB
//#region src/libs/xlsx-worksheet.ts /** * Parses an Excel Worksheet XML string and returns the data of each row as an array of arrays. * Each inner array represents a row, with cell values as strings (empty string for missing cells). * No external dependencies are used. * @param xml - The worksheet XML string * @returns Array of rows, each row is an array of cell values */ function xlsxWorksheetData(xml, sharedStrings) { const rowRegex = /<row[^>]*>([\s\S]*?)<\/row>/g; const cellRegex = /<c\s+([^>]*?)r="([A-Z]+)\d+"([^>]*)>([\s\S]*?)<\/c>/g; const vRegex = /<v>([\s\S]*?)<\/v>/; const rows = []; let match; while (match = rowRegex.exec(xml)) { const rowXml = match[1]; const cells = []; let cellMatch; while (cellMatch = cellRegex.exec(rowXml)) { const attrs = (cellMatch[1] + " " + cellMatch[3]).trim(); const colRef = cellMatch[2]; const tMatch = /t="([^"]+)"/.exec(attrs); const cellType = tMatch ? tMatch[1] : void 0; let colIdx = 0; for (let i = 0; i < colRef.length; i++) colIdx = colIdx * 26 + (colRef.charCodeAt(i) - 64); colIdx -= 1; const vMatch = vRegex.exec(cellMatch[4]); let value = vMatch ? vMatch[1] : ""; if (cellType === "s" && value !== "" && sharedStrings) { const idx = Number.parseInt(value, 10); value = sharedStrings.get(idx) ?? ""; } cells.push({ col: colIdx, value }); } const rowArr = []; if (cells.length) { const maxCol = Math.max(...cells.map((c) => c.col)); for (let i = 0; i <= maxCol; i++) rowArr[i] = ""; for (const cell of cells) rowArr[cell.col] = cell.value; } rows.push(rowArr); } return rows; } function xlsxWorksheetXML(zipEntries, sheetIndex) { const sheetPath = `xl/worksheets/sheet${sheetIndex}.xml`; if (!zipEntries[sheetPath]) throw new Error(`Sheet1 not found in the XLSX file at path: ${sheetPath}`); const sheet1Data = zipEntries[sheetPath]; const xmlText = new TextDecoder("utf-8").decode(sheet1Data); return xmlText; } function xlsxSharedStrings(zipEntries) { const sharedStringsPath = `xl/sharedStrings.xml`; if (!zipEntries[sharedStringsPath]) throw new Error(`Shared strings not found in the XLSX file at path: ${sharedStringsPath}`); const sharedStringsData = zipEntries[sharedStringsPath]; const xmlText = new TextDecoder("utf-8").decode(sharedStringsData); return parseSharedStringsXml(xmlText); } /** * Parses a sharedStrings.xml string from an Excel file and returns a Map of index to string value. * @param xml - The sharedStrings.xml content as a string * @returns Map<number, string> where key is the index and value is the shared string */ function parseSharedStringsXml(xml) { const map = /* @__PURE__ */ new Map(); const siRegex = /<si>([\s\S]*?)<\/si>/g; let match; let idx = 0; while (match = siRegex.exec(xml)) { const tRegex = /<t[^>]*>([\s\S]*?)<\/t>/g; let tMatch; let value = ""; while (tMatch = tRegex.exec(match[1])) value += tMatch[1]; map.set(idx++, value); } return map; } //#endregion export { xlsxSharedStrings, xlsxWorksheetData, xlsxWorksheetXML };