gpt4 book ai didi

javascript - 使用 Sheetjs 访问单元格

转载 作者:行者123 更新时间:2023-11-29 18:57:16 24 4
gpt4 key购买 nike

我按照这里的演示操作: https://github.com/SheetJS/js-xlsx/tree/master/demos/electron

我可以将 excel 文件拖到我的 electron 应用程序中。

documentation 说,你可以访问每个单元格:

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};
/* if an A1-style address is needed, encode the address */
var cell_ref = XLSX.utils.encode_cell(cell_address);
}
}

我如何将它与下面的代码一起使用?我将文件的内容存储在我的测试变量中,但我无法访问它。那里的文档缺少信息。

var do_file = (function() {
return function do_file(files) {
var f = files[0];
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
data = new Uint8Array(data);
test = XLSX.read(data, {type: 'array'});
console.log(test);
};
reader.readAsArrayBuffer(f);
};
})();

我不知道如何开始,提前致谢

最佳答案

这里是:

var do_file = (function() {
return function do_file(files) {
var f = files[0];
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
data = new Uint8Array(data);
//process_wb(XLSX.read(data, {type: 'array'}));
/* read the file */
var workbook = XLSX.read(data, {type: 'array'}); // parse the file
var sheet = workbook.Sheets[workbook.SheetNames[0]]; // get the first worksheet

/* loop through every cell manually */
var range = XLSX.utils.decode_range(sheet['!ref']); // get the range
for(var R = range.s.r; R <= range.e.r; ++R) {
for(var C = range.s.c; C <= range.e.c; ++C) {
/* find the cell object */
console.log('Row : ' + R);
console.log('Column : ' + C);
var cellref = XLSX.utils.encode_cell({c:C, r:R}); // construct A1 reference for cell
if(!sheet[cellref]) continue; // if cell doesn't exist, move on
var cell = sheet[cellref];
console.log(cell.v);

};
reader.readAsArrayBuffer(f);
};
})();

关于javascript - 使用 Sheetjs 访问单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48816940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com