java 脚本中 for 循环中的迭代器错误(在 filereader 中使用 onload 函数)
Error with iterator in for loop in java script(With onload function in filereader)
代码:
var workbook = XLSX.read(data, {type: 'binary'});
var first_sheet_name = workbook.SheetNames[0];
var worksheet = workbook.Sheets[first_sheet_name];
var k=1
var desired_cell = worksheet['A'+k].w;
以上代码运行良好
但是:
for (var i = 1; i <= 50; i++) {
var obj = {};
var cell = (worksheet[('A'+i)]).w;
obj.PickUpDate = cell;
obj.PickUpTimeSlot = cell;
obj.PickUpAddress = cell;
obj.DeliveryAddress = cell;
obj.BoxType = cell;
array.push(obj);
}
这段代码报错:
Uncaught TypeError: Cannot read property 'w' of undefined
at FileReader.reader.onload
行:
var cell = (worksheet[('A'+i)]).w;
很好,因为我可以将 'cell' 打印到控制台。
两个片段都在 FileReader.reader.onload
函数中。
我正在使用 xlsx 包 https://github.com/SheetJS/js-xlsx
该代码有什么问题?
来自 SheetJS/js-xlsx 文档:
Built-in export utilities (such as the CSV exporter) will use the w text if it is available. To change a value, be sure to delete cell.w (or set it to undefined) before attempting to export. The utilities will regenerate the w text from the number format (cell.z) and the raw value if possible.
您无法确定 (worksheet[('A'+i)]).w
是否已定义。
我不知道你的工作表数据是什么,但也许可以完成这项工作:
var cell = (typeof (worksheet[('A'+i)]).w !== 'undefined') ? worksheet[('A'+i)]).w : worksheet[('A'+i)]).v;
希望对您有所帮助。
代码:
var workbook = XLSX.read(data, {type: 'binary'});
var first_sheet_name = workbook.SheetNames[0];
var worksheet = workbook.Sheets[first_sheet_name];
var k=1
var desired_cell = worksheet['A'+k].w;
以上代码运行良好 但是:
for (var i = 1; i <= 50; i++) {
var obj = {};
var cell = (worksheet[('A'+i)]).w;
obj.PickUpDate = cell;
obj.PickUpTimeSlot = cell;
obj.PickUpAddress = cell;
obj.DeliveryAddress = cell;
obj.BoxType = cell;
array.push(obj);
}
这段代码报错:
Uncaught TypeError: Cannot read property 'w' of undefined
at FileReader.reader.onload
行:
var cell = (worksheet[('A'+i)]).w;
很好,因为我可以将 'cell' 打印到控制台。
两个片段都在 FileReader.reader.onload
函数中。
我正在使用 xlsx 包 https://github.com/SheetJS/js-xlsx
该代码有什么问题?
来自 SheetJS/js-xlsx 文档:
Built-in export utilities (such as the CSV exporter) will use the w text if it is available. To change a value, be sure to delete cell.w (or set it to undefined) before attempting to export. The utilities will regenerate the w text from the number format (cell.z) and the raw value if possible.
您无法确定 (worksheet[('A'+i)]).w
是否已定义。
我不知道你的工作表数据是什么,但也许可以完成这项工作:
var cell = (typeof (worksheet[('A'+i)]).w !== 'undefined') ? worksheet[('A'+i)]).w : worksheet[('A'+i)]).v;
希望对您有所帮助。