我正在处理一个项目,我需要导出电子表格,手动填充它,然后再将其导入系统。我正在使用 PHPExcel 来完成所有艰苦的工作。
电子表格基本上是一组需要回答的问题。 (You can download an example of the spreadsheet here)。每个问题的答案都在数据验证列表中,该列表来自另一个使用命名范围的工作表(答案)。问题和答案都带有唯一的ID,因此我可以轻松地将结果读回数据库。
questionID存储在B列中,并在创建电子表格时插入。然而,answerID是通过查找(在C列中)完成的,因为answerID将根据所选的答案而改变。每组答案都有两个命名范围1,用于列表中的答案,另一组用于查找。这些命名范围标记为answers_questionID和answers_lookup_questionID。用于获取查找值的C列中使用的公式为:
=INDEX(answers_lookup_1, MATCH(E6, answers_1, 0),2)
我使用INDEX和MATCH代替VLOOKUP,因为我不想按字母顺序对数据进行排序。
这一切都可以在电子表格中正常工作,当我尝试阅读它时,我遇到了问题。当我尝试重读这些值时,我要么 0 (它们都应该是正整数)或 #REF!
以下是我在阅读数据时所做的事情:
$objPHPExcel = new PHPExcel();
// Set a read filter so that we can restrict what we're loading in
class MyReadFilter implements PHPExcel_Reader_IReadFilter {
private $_iStartRow = 0;
private $_iEndRow = 0;
private $_aColumns = array();
public function __construct($iStartRow, $iEndRow, $aColumns) {
$this->_iStartRow = $iStartRow;
$this->_iEndRow = $iEndRow;
$this->_aColumns = $aColumns;
}
public function readCell($iCol, $iRow, $sWorksheetName = '') {
if ($iRow >= $this->_iStartRow && $iRow <= $this->_iEndRow) {
if (in_array($iCol, $this->_aColumns)) {
return true;
}
}
return false;
}
}
$objFilterSubset = new MyReadFilter(5, 500, range('B', 'L'));
// Which type of reader do we need.
if (!extension_loaded('zip')) dl('php_zip.dll');
$objReader = new PHPExcel_Reader_Excel2007();
// Set the read filter so we don't get EVERYTHING
$objReader->setReadFilter($objFilterSubset);
// Load the actual data
$objPHPExcel = $objReader->load($sFilename);
// Store the data in an array
$aSheetNames = $objPHPExcel->getSheetNames();
foreach ($aSheetNames as $iSheetNum => $sSheetName) {
$objPHPExcel->setActiveSheetIndex($iSheetNum);
$aSheetData[$sSheetName] = $objPHPExcel->getActiveSheet()->toArray(null,true,true,false);
}
所以我得到了一个包含所有数据的漂亮大数组,只有C列错误。
Mark Baker让我运行一个公式测试,可以找到here,如果有人能理解它,可以进行以下调试。
Formula Value is =INDEX(answers_lookup_1, MATCH(E6, answers_1, 0),2)
Expected Value is 7 Parser Stack :-
Array (
[0] => Array
(
[type] => Value
[value] => answers_lookup_1
[reference] =>
)
[1] => Array
(
[type] => Cell Reference
[value] => E6
[reference] => E6
)
[2] => Array
(
[type] => Value
[value] => answers_1
[reference] =>
)
[3] => Array
(
[type] => Value
[value] => 0
[reference] =>
)
[4] => Array
(
[type] => Operand Count for Function MATCH()
[value] => 3
[reference] =>
)
[5] => Array
(
[type] => Function
[value] => MATCH(
[reference] =>
)
[6] => Array
(
[type] => Value
[value] => 2
[reference] =>
)
[7] => Array
(
[type] => Operand Count for Function INDEX()
[value] => 3
[reference] =>
)
[8] => Array
(
[type] => Function
[value] => INDEX(
[reference] =>
)
)
Calculated Value is 0
Evaluation Log:
Array
(
[0] => Questions!C6 -> Evaluating Named Range answers_lookup_1
[1] => Questions!C6 -> Evaluation Result for named range A4:B6 is a matrix with a value of { , ; , 7; , 8 }
[2] => Questions!C6 -> Evaluating Cell E6 in current worksheet
[3] => Questions!C6 -> Evaluation Result for cell Questions!E6 is a string with a value of Yes
[4] => Questions!C6 -> Evaluating Named Range answers_1
[5] => Questions!C6 -> Evaluation Result for named range A4:A6 is a matrix with a value of { ; ; }
[6] => Questions!C6 -> Evaluating Function MATCH() with 3 arguments
[7] => Questions!C6 -> Evaluating MATCH( Yes, { ; ; }, 0 )
[8] => Questions!C6 -> Evaluation Result for MATCH() function call is a #N/A error
[9] => Questions!C6 -> Evaluating Function INDEX() with 3 arguments
[10] => Questions!C6 -> Evaluating INDEX( { , ; , 7; , 8 }, #N/A, 2 )
[11] => Questions!C6 -> Evaluation Result for INDEX() function call is a null value
)
我尝试了多种公式和读取数据的方法。我已经循环使用了数据并使用了getCalculatedValue()而不是toArray(),但仍然得到了相同的结果。
我只能认为这可能是 Functions.php 中 INDEX 函数中的错误,并且在使用named读取时未正确解释公式范围。如果我将传递的数组打印到INDEX(),结果是:
INDEX: Array
(
[12] => Array
(
[A] =>
[B] => 6
)
[13] => Array
(
[A] =>
[B] => 7
)
[14] => Array
(
[A] =>
[B] => 8
)
)
这是正确的值(虽然我不确定A列是否应该填充在我的电子表格中),即是,否和N / A ...
如果你已经走到这一步,感谢并希望你可以帮助或指出我正确的方向。
非常感谢,
亚当。
答案 0 :(得分:-1)
函数INDEX不能将单元格引用作为第二个参数处理
@@ -604,10 +604,13 @@ class PHPExcel_Calculation_LookupRef
}
if (!is_array($arrayValues)) {
return PHPExcel_Calculation_Functions::REF();
}
+
+ $lrow = PHPExcel_Calculation_Functions::flattenSingleValue($rowNum);
+ $rowNum = $lrow;
$rowKeys = array_keys($arrayValues);
$columnKeys = @array_keys($arrayValues[$rowKeys[0]]);
if ($columnNum > count($columnKeys)) {