使用节点 js 从 excel 读取名为 cells/range

reading named cells/range from excel with node js

我正在解析 Excel 文件并尝试从中获取信息。所有文件都具有相同的命名范围。我的程序的目的是解析所有文件以聚合数据。 我试过 xlsx 包。有了这个包,我可以从 excel 文件中获取数据,我可以获取定义的名称,但是,我不明白如何使用定义的名称来获取它们的值。也许另一个包裹会更有帮助?

下面是代码:

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!: 

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

我希望单元格的值在 Summary!$11:$11

范围内

非常感谢您的帮助!

[编辑] 我错误地删除了我的评论,但它是: 不幸的是,@Cat XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]) 丢失了定义的名称。

@A.M。我试过 convert-excel_to_json 但我在这里丢失了定义的命名单元格。

我可以使用下面的代码访问 Excel 工作表中的命名范围,我必须解析范围引用然后循环遍历它:

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)]);
    }
}