phpexcelで数式をExcelのように相対的にセル指定を変えてコピーする方法

https://stackoverflow.com/questions/43583220/duplicate-formula-dynamically-in-phpexcel
PHPExcel_ReferenceHelper->updateFormulaReferences関数を利用して、数式を変換した上でコピー先に値を設定する。

function CopyFormula($sheet, $from_cell, $to_cell) {
	$from     = PHPExcel_Cell::rangeBoundaries($from_cell);
	$from_col = (int)$from[0][0];
	$from_row = (int)$from[0][1];
	
	$to       = PHPExcel_Cell::rangeBoundaries($to_cell);
	$to_col   = (int)$to[0][0];
	$to_row   = (int)$to[0][1];
	
	$move_col = $to_col - $from_col;
	$move_row = $to_row - $from_row;
	
	$value = $sheet->getCell($from_cell)->getValue();
	if ($sheet->getCell($from_cell)->getDataType() === PHPExcel_Cell_DataType::TYPE_FORMULA) {
		$reference_helper = PHPExcel_ReferenceHelper::getInstance();
		$value = $reference_helper->updateFormulaReferences($value, "A1", $move_col, $move_row);
	}
	$sheet->setCellValue($to_cell, $value);
}