[英]Iterating through rangeToArray() in PhpSpreadsheet
I have Excel sheets that I need to perform calculations with.我有需要执行计算的 Excel 工作表。 I am using PHPSpreadsheet to read and write, as PHPExcel is now deprecated.我正在使用 PHPSpreadsheet 进行读写,因为 PHPExcel 现在已被弃用。
The problem I am experiencing is in getting values from a specific column to perform calculations.我遇到的问题是从特定列获取值以执行计算。
After going through the documentation, I found that I can use the rangeToArray() function to retrieve the values.通过文档后,我发现我可以使用 rangeToArray() 函数来检索值。 My code looks like this:我的代码如下所示:
$inputFiletype = 'Xlsx';
$inputFileName = './helloworld.xlsx';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
$find_val = $spreadsheet->getActiveSheet()->rangeToArray('K1:K5');
This, according to the documentation, creates an array ($find_vals).根据文档,这会创建一个数组($find_vals)。 However, when I attempt to loop through the array to view the values, I get an 'Array to string Conversion' notice, with the output of "Array".但是,当我尝试遍历数组以查看值时,会收到“数组到字符串转换”的通知,输出为“数组”。
If I use var_dump, I get the following:如果我使用 var_dump,我会得到以下信息:
array(5) { [0]=> array(1) { [0]=> float(1) } [1]=> array(1) { [0]=> float(2) } [2]=> array(1) { [0]=> float(3) } [3]=> array(1) { [0]=> float(4) } [4]=> array(1) { [0]=> float(5) } }
I have tried the following loop codes:我尝试了以下循环代码:
for($i=0; $i<=4; $i++) {
echo (string)$find_val[$i];
}
as well as也
foreach($find_val as $vals) {
echo $vals;
}
I have also tried the following which changed the result a little:我还尝试了以下稍微改变了结果的方法:
for($i=0; $i<=4; $i++) {
echo (string)$find_val[$i][$i];
}
This output one value, and gave me Undefined Offset errors for the other 4 iterations.此输出一个值,并在其他 4 次迭代中给了我未定义的偏移错误。
How would I be able to successfully iterate through a rangeToArray value using the PhpSpreadsheet API?如何使用 PhpSpreadsheet API 成功迭代 rangeToArray 值?
Thanks in advance.提前致谢。
Just saw your post now.现在才看到你的帖子。 Have you tried this?你试过这个吗?
You are almost there.你快到了。
foreach($sheetData as $vals) {
echo $vals[0];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.