sheetjs-vscode-extension/src/excelUtils.ts
Asad 21f4542e5f feat: implemented timestamp-based LRU caching for all workbooks
- add timestamp to cache keys to properly handle deleted/replaced files
- create modular LRUCache implementation to limit memory usage (20
  workbooks and 255 worksheet per workbook)
- added unit tets for LRUCache
2025-05-16 17:29:09 -04:00

51 lines
1.3 KiB
TypeScript

// parse range like A1:Z100 and return information about dimensions
export function parseRange(rangeStr: string) {
if (!rangeStr || !rangeStr.includes(':')) { return null; }
const parts = rangeStr.split(':');
const startCell = parts[0];
const endCell = parts[1];
// extract column letters and row numbers
const startCol = startCell.replace(/[0-9]/g, '');
const endCol = endCell.replace(/[0-9]/g, '');
const startRow = parseInt(startCell.replace(/[^0-9]/g, ''));
const endRow = parseInt(endCell.replace(/[^0-9]/g, ''));
// calc dimensions
const startColNum = colLetterToNum(startCol);
const endColNum = colLetterToNum(endCol);
const totalRows = endRow - startRow + 1;
const totalCols = endColNum - startColNum + 1;
return {
startRow,
endRow,
startCol,
endCol,
startColNum,
endColNum,
totalRows,
totalCols
};
}
// (A=1, B=2, etc.)
export function colLetterToNum(col: string): number {
let result = 0;
for (let i = 0; i < col.length; i++) {
result = result * 26 + (col.charCodeAt(i) - 64);
}
return result;
}
// (1=A, 2=B, etc.)
export function numToColLetter(num: number): string {
let result = '';
while (num > 0) {
const modulo = (num - 1) % 26;
result = String.fromCharCode(65 + modulo) + result;
num = Math.floor((num - modulo) / 26);
}
return result || 'A';
}