Last active
October 8, 2023 02:03
-
-
Save davidhq/0afed70985842cac6fc4e00f88a71bd2 to your computer and use it in GitHub Desktop.
Execute excel workbooks through node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FormulaParser = require('hot-formula-parser').Parser; | |
const parser = new FormulaParser(); | |
const Excel = require('exceljs'); | |
const workbook = new Excel.Workbook(); | |
function getCellResult(worksheet, cellLabel) { | |
if (worksheet.getCell(cellLabel).formula) { | |
return parser.parse(worksheet.getCell(cellLabel).formula).result; | |
} else { | |
return worksheet.getCell(cellCoord.label).value; | |
} | |
} | |
workbook.xlsx.readFile('./doc.xlsx').then(() => { | |
var worksheet = workbook.getWorksheet(1); | |
parser.on('callCellValue', function(cellCoord, done) { | |
if (worksheet.getCell(cellCoord.label).formula) { | |
done(parser.parse(worksheet.getCell(cellCoord.label).formula).result); | |
} else { | |
done(worksheet.getCell(cellCoord.label).value); | |
} | |
}); | |
parser.on('callRangeValue', function(startCellCoord, endCellCoord, done) { | |
var fragment = []; | |
for (var row = startCellCoord.row.index; row <= endCellCoord.row.index; row++) { | |
var colFragment = []; | |
for (var col = startCellCoord.column.index; col <= endCellCoord.column.index; col++) { | |
colFragment.push(worksheet.getRow(row + 1).getCell(col + 1).value); | |
} | |
fragment.push(colFragment); | |
} | |
if (fragment) { | |
done(fragment); | |
} | |
}); | |
worksheet.getCell('C12').value = 500; | |
console.log(getCellResult(worksheet, 'C13')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It worked. Thanks a ton!