최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday

티스토리 뷰




PHPExcel 계산 엔진 사용하기


수식 계산 값 가져오기


PHPExcel은 스프레드 시트안의 메모리들을 나타내는데. 수식 계산 기능을 제공합니다.


무슨말이냐하면 특정 셀에서 숫자나 텍스트가 포함된 값의 형식, 또는 수식에 대한 연산결과를 가져올 수 있다는 뜻이다.


예를 들어, 수식 "=SUM(A1:A10)" 은 A1,A2, ..., A10 값의 합계인데


이런 수식에 대한 계산을 하려면 셀이 수식을 포함하고 있을 때 getCalculatedValue() 메서드를 호출하면 된다.


$objPHPExcel->getActiveSheet()->getCell('E11')->getCalculatedValue();


다음의 PHPExcel에 포함된 데모에서 코드를 작성하는 경우 값은 "64"를 확인할 수 있다.








PHPExcel의 수식 해석의 또다른 뛰어난 기능은 행/열에 대한 삽입/제거시 자동으로 수식이 조정된다는 것입니다.


다음은 예제입니다


셀 E11에 포함 된 수식이 "SUM(E4:E9)" 인 것을 알 수 있습니다. 


이제 내가 다음 코드에서 두개의 새로운 라인이 추가 됩니다.


$objPHPExcel->getActiveSheet()->insertNewRowBefore(7, 2);


그림 : 09-formula-in-cell-2.png

주의 깊게 보셨습니까?


이전 수식의 셀 E11은 (이제 E13이고 2개의 새로운 행이 삽입되었습니다.) "SUM(E4:E11)" 로 체인지 되었습니다.


또한 삽입된 셀은 당순히 Excel의 행동 처럼 이전 셀의 스타일 정보를 복사합니다.


당신은 행과 열을 모두 삽입 할 수 있습니다.


알려진 제한 사항


PHPExcel 계산 엔진으로 알려진 몇 가지 제한 사항이 있습니다.


대부분은 Excel수식이 실행되기 전에 PHP코드로 변환된다는 것입니다. 이 뜻은 엑셀 수식 계산은 PHP언어 특성에 따른 다는 것입니다.


연산자 우선 순위


Excel에서는 보통의 대수학 처럼 '+'는 "&'보다 우선하며, '*'는 '+'보다 우선합니다. 


Excel에서 연산자 우선 순위에 대한 참조 : http://support.microsoft.com/kb/25189


PHP의 연산자 우선 순위에 대한 참조 : http://www.php.net/operators


숫자와 텍스트를 포함하는 수식


숫자와 텍스트를 포함하는 수식이 있을경우 예기치 않은 결과 또는 판독할 수 없는 내용의 파일을 만들어 냅니다.


예를 들어 수식 '=3+"Hello"' 는 Excel에서 오류(#VALUE!)가 예상됩니다.


때문에 PHP는 "Hello"를 숫자값 0로 변환하며, '=3+"Hello"의 수식의 경우 에러대신 3으로 평가됩니다.


이것은 또한 엑셀 문서를 읽을 수 없는 내용을 포함하고 있는 컨텐츠로 생성되는 원인이 됩니다.


이 문제에 대한 PHP참조 : http://be.php.net/manual/en/language.types.string.php#language.types.string.conversion











영어원문

Using the PHPExcel calculation engine

Performing formula calculations

As PHPExcel represents an in-memory spreadsheet, it also offers formula calculation capabilities. A cell can be of a value type (containing a number or text), or a formula type (containing a formula which can be evaluated). For example, the formula "=SUM(A1:A10)" evaluates to the sum of values in A1, A2, ..., A10.

To calculate a formula, you can call the cell containing the formula’s method getCalculatedValue(), for example:

$objPHPExcel->getActiveSheet()->getCell('E11')->getCalculatedValue();

If you write the following line of code in the invoice demo included with PHPExcel, it evaluates to the value "64":

09-command-line-calculation.png

Another nice feature of PHPExcel's formula parser, is that it can automatically adjust a formula when inserting/removing rows/columns. Here's an example:

09-formula-in-cell-1.png

You see that the formula contained in cell E11 is "SUM(E4:E9)". Now, when I write the following line of code, two new product lines are added:

$objPHPExcel->getActiveSheet()->insertNewRowBefore(7, 2);

09-formula-in-cell-2.png

Did you notice? The formula in the former cell E11 (now E13, as I inserted 2 new rows), changed to "SUM(E4:E11)". Also, the inserted cells duplicate style information of the previous cell, just like Excel's behaviour. Note that you can both insert rows and columns.

Known limitations

There are some known limitations to the PHPExcel calculation engine. Most of them are due to the fact that an Excel formula is converted into PHP code before being executed. This means that Excel formula calculation is subject to PHP's language characteristics.

Operator precedence

In Excel '+' wins over '&', just like '*' wins over '+' in ordinary algebra. The former rule is not what one finds using the calculation engine shipped with PHPExcel.

Reference for operator precedence in Excel: http://support.microsoft.com/kb/25189

Reference for operator precedence in PHP: http://www.php.net/operators

Formulas involving numbers and text

Formulas involving numbers and text may produce unexpected results or even unreadable file contents. For example, the formula '=3+"Hello "' is expected to produce an error in Excel (#VALUE!). Due to the fact that PHP converts “Hello” to a numeric value (zero), the result of this formula is evaluated as 3 instead of evaluating as an error. This also causes the Excel document being generated as containing unreadable content.

Reference for this behaviour in PHP: http://be.php.net/manual/en/language.types.string.php#language.types.string.conversion


https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/09-Calculation-Engine.md

댓글