I am parsing Excel files and trying to get informations from it. All the files have the same named ranges. The purpose of my program is to parse all the files to aggregate the datas. I have tried the package xlsx. With the package, I am able to get the data from the excel files, I am able to get the defined names, however, I do not understand how to use the defined names to get their values. Maybe another package would be more helpfull?
Here is the code below:
var XLSX = require("xlsx");
var workbook = XLSX.readFile("myWorkbook.xlsx");
var sheet_name_list = workbook.SheetNames;
let SummarySheet = sheet_name_list.findIndex(elt => {
return elt === "Summary"; //the values I am looking for are in summary sheets
});
let worksheet = workbook.Sheets[sheet_name_list[SummarySheet]];
let datesIndex = workbook.Workbook.Names.findIndex(elt => {
return (elt.Name === "myDate") & (elt.Sheet !== 0);
});
let myDates = workbook.Workbook.Names[datesIndex].Ref; //this gives Summary!$11:$11
console.log(worksheet[myDates]); //this renders undefined
console.log(worksheet['A1:B1']); //this renders undefined
console.log(worksheet['A1']); //this renders the correct value of cell A1
I expect to have the values of the cells being in the range Summary!$11:$11
Thanks a lot for your help!
[EDIT] I wrongly deleted my comment but here it was: @Cat the XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]) loose the defined names unfortunately.
@A.M. I have tried with convert-excel_to_json but I loose the defined named cells here.
I can access a named range in an Excel worksheet using the code below, I have to parse the range ref and then I can loop over it:
var XLSX = require("xlsx");
var workbook = XLSX.readFile("myWorkbook.xlsx");
var sheet_name_list = workbook.SheetNames;
let SummarySheet = sheet_name_list.findIndex(elt => {
return elt === "Summary"; //the values I am looking for are in summary sheets
});
var worksheet = workbook.Sheets[sheet_name_list[SummarySheet]];
var datesIndex = workbook.Workbook.Names.findIndex(elt => {
return (elt.Name === "myDate") && (elt.Sheet !== 0);
});
var myDates = workbook.Workbook.Names[datesIndex].Ref;
// Get the sheet specific range..
myDates = myDates.split('!')[1];
console.log("myDates", myDates); console.log("myDates: Sheetref", myDates);
// Get the decoded range for the named range..
var range = XLSX.utils.decode_range(myDates);
// Get the range of the worksheet..
var worksheetRange = XLSX.utils.decode_range(worksheet['!ref']);
// If our named range overlaps the worksheet range set this for the columns.
if (!range.s.c || range.s.c <= 0) range.s.c = worksheetRange.s.c;
if (!range.e.c || range.e.c <= 0) range.e.c = worksheetRange.e.c;
// Loop over the referenced range..
for(var R = range.s.r; R <= range.e.r; ++R) {
for(var C = range.s.c; C <= range.e.c; ++C) {
var cell_address = {c:C, r:R};
console.log(cell_address);
console.log(worksheet[XLSX.utils.encode_cell(cell_address)]);
}
}