|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +// Function to convert column letter to index (A=0, B=1, C=2, etc.) |
| 5 | +function columnLetterToIndex(letter) { |
| 6 | + return letter.toUpperCase().charCodeAt(0) - 'A'.charCodeAt(0); |
| 7 | +} |
| 8 | + |
| 9 | +// Function to parse cell reference (e.g., "C1" -> {column: 2, row: 0}) |
| 10 | +function parseCellReference(cellRef) { |
| 11 | + const match = cellRef.match(/^([A-Z]+)(\d+)$/i); |
| 12 | + if (!match) { |
| 13 | + throw new Error(`Invalid cell reference: ${cellRef}`); |
| 14 | + } |
| 15 | + |
| 16 | + const columnLetter = match[1].toUpperCase(); |
| 17 | + const rowNumber = parseInt(match[2]); |
| 18 | + |
| 19 | + // Convert to 0-based indices |
| 20 | + const columnIndex = columnLetterToIndex(columnLetter); |
| 21 | + const rowIndex = rowNumber - 1; // Convert 1-based to 0-based |
| 22 | + |
| 23 | + return { column: columnIndex, row: rowIndex, original: cellRef }; |
| 24 | +} |
| 25 | + |
| 26 | +// Function to get cell value from spreadsheet data |
| 27 | +function getCellValue(sheetData, columnIndex, rowIndex) { |
| 28 | + if (!sheetData.rows || rowIndex >= sheetData.rows.length || rowIndex < 0) { |
| 29 | + return { exists: false, reason: 'Row out of bounds' }; |
| 30 | + } |
| 31 | + |
| 32 | + const row = sheetData.rows[rowIndex]; |
| 33 | + if (!row || columnIndex >= row.length || columnIndex < 0) { |
| 34 | + return { exists: false, reason: 'Column out of bounds' }; |
| 35 | + } |
| 36 | + |
| 37 | + const cellValue = row[columnIndex]; |
| 38 | + return { |
| 39 | + exists: true, |
| 40 | + value: cellValue, |
| 41 | + type: typeof cellValue === 'object' ? 'formula' : typeof cellValue, |
| 42 | + isEmpty: cellValue === null || cellValue === undefined || cellValue === '' |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +// Function to format cell details for output |
| 47 | +function formatCellDetails(cellRef, cellInfo, sheetName) { |
| 48 | + const { original } = cellRef; |
| 49 | + |
| 50 | + if (!cellInfo.exists) { |
| 51 | + return `${original}: [ERROR] ${cellInfo.reason}`; |
| 52 | + } |
| 53 | + |
| 54 | + if (cellInfo.isEmpty) { |
| 55 | + return `${original}: <empty>`; |
| 56 | + } |
| 57 | + |
| 58 | + if (cellInfo.type === 'formula' && cellInfo.value.formula) { |
| 59 | + return `${original}: ${cellInfo.value.formula} (evaluates to: ${cellInfo.value.value})`; |
| 60 | + } |
| 61 | + |
| 62 | + return `${original}: ${cellInfo.value}`; |
| 63 | +} |
| 64 | + |
| 65 | +// Main function |
| 66 | +function extractCells() { |
| 67 | + const args = process.argv.slice(2); |
| 68 | + |
| 69 | + if (args.length < 2) { |
| 70 | + console.error('Usage: node extract_cells.js <spreadsheet.json> <cell1> [cell2] [cell3] ...'); |
| 71 | + console.error('Example: node extract_cells.js spreadsheet.json C1 C20 E40'); |
| 72 | + process.exit(1); |
| 73 | + } |
| 74 | + |
| 75 | + const jsonFilePath = args[0]; |
| 76 | + const cellReferences = args.slice(1); |
| 77 | + |
| 78 | + // Check if file exists |
| 79 | + if (!fs.existsSync(jsonFilePath)) { |
| 80 | + console.error(`❌ File not found: ${jsonFilePath}`); |
| 81 | + process.exit(1); |
| 82 | + } |
| 83 | + |
| 84 | + try { |
| 85 | + // Read and parse JSON file |
| 86 | + const jsonData = JSON.parse(fs.readFileSync(jsonFilePath, 'utf-8')); |
| 87 | + |
| 88 | + // Get the first sheet (or we could make this configurable) |
| 89 | + const sheetNames = Object.keys(jsonData); |
| 90 | + if (sheetNames.length === 0) { |
| 91 | + console.error('❌ No sheets found in the spreadsheet data'); |
| 92 | + process.exit(1); |
| 93 | + } |
| 94 | + |
| 95 | + const sheetName = sheetNames[0]; // Use first sheet by default |
| 96 | + const sheetData = jsonData[sheetName]; |
| 97 | + |
| 98 | + if (sheetNames.length > 1) { |
| 99 | + console.log(`📋 Using sheet: "${sheetName}" (${sheetNames.length} sheets available)`); |
| 100 | + console.log(''); |
| 101 | + } |
| 102 | + |
| 103 | + // Process each cell reference |
| 104 | + cellReferences.forEach(cellRefString => { |
| 105 | + try { |
| 106 | + const cellRef = parseCellReference(cellRefString); |
| 107 | + const cellInfo = getCellValue(sheetData, cellRef.column, cellRef.row); |
| 108 | + const output = formatCellDetails(cellRef, cellInfo, sheetName); |
| 109 | + console.log(output); |
| 110 | + } catch (error) { |
| 111 | + console.log(`${cellRefString}: [ERROR] ${error.message}`); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + } catch (error) { |
| 116 | + console.error(`❌ Error reading JSON file: ${error.message}`); |
| 117 | + process.exit(1); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// Run the script |
| 122 | +extractCells(); |
| 123 | + |
| 124 | +module.exports = { parseCellReference, getCellValue, formatCellDetails }; |
0 commit comments