2016-12-31 22 views
0

我正在使用以下代碼複製PHPSpreadsheet中的行。不過,我想更改複製行中的公式引用以反映新的行號。 (例如(A1-B1),(A2-B2),A3-B3等)有什麼建議嗎?更新PHPExcel中複製行的單元引用(PHPSpreadsheet)

function copyRowFull(&$ws_from, &$ws_to, $row_from, $row_to) { 
    $ws_to->getRowDimension($row_to)->setRowHeight($ws_from->getRowDimension($row_from)->getRowHeight()); 
    $lastColumn = $ws_from->getHighestColumn(); 
    ++$lastColumn; 
    for ($c = 'A'; $c != $lastColumn; ++$c) { 
    $cell_from = $ws_from->getCell($c.$row_from); 
    $cell_to = $ws_to->getCell($c.$row_to); 
    $cell_to->setXfIndex($cell_from->getXfIndex()); 
    $cell_to->setValue($cell_from->getValue()); 
    } 
} 

回答

0

您應該能夠使用PHPExcel_ReferenceHelper類的updateFormulaReferences()方法在公式串更新單元格引用

/** 
* Update references within formulas 
* 
* @param string $pFormula Formula to update 
* @param int  $pBefore Insert before this one 
* @param int  $pNumCols Number of columns to insert 
* @param int  $pNumRows Number of rows to insert 
* @param string $sheetName Worksheet name/title 
* @return string Updated formula 
* @throws PHPExcel_Exception 
*/