Files
PhpSpreadsheet/samples/Financial3/ISPMT.php
T
oleibman b3de003aa5 Reorganize Samples
The samples have become unwieldy when running them from a browser. In particular, the drop-down lists are fixed size with no scrolling, and many of them are now just too large. I have moved all the Calculation samples up a level, and broken several categories (Basic, Chart, DateTime, Engineering, Financial, and Reader) into several pieces.

Convert-Online (now found in the Engineering category) had a number of different problems which are now resolved. It is the only member with any significant code change.
2024-02-03 07:25:29 -08:00

37 lines
1.2 KiB
PHP

<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
require __DIR__ . '/../../Header.php';
$helper->log('Returns the interest paid during a specific period of a loan or investment.');
// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
// Add some data
$arguments = [
['Interest Rate', 0.05],
['Number of Years', 5],
['Present Value', 50000.00],
];
// Some basic formatting for the data
$worksheet->fromArray($arguments, null, 'A1');
$worksheet->getStyle('B1')->getNumberFormat()->setFormatCode('0.00%');
$worksheet->getStyle('B3')->getNumberFormat()->setFormatCode('$#,##0.00');
// Now the formula
$baseRow = 6;
for ($month = 1; $month <= 12; ++$month) {
$row = (string) ($baseRow + $month);
$worksheet->setCellValue("A{$row}", "Payment for Mth {$month}");
$worksheet->setCellValue("B{$row}", "=ISPMT(\$B\$1/12, {$month}, \$B\$2*12, \$B\$3)");
$worksheet->getStyle("B{$row}")->getNumberFormat()->setFormatCode('$#,##0.00;-$#,##0.00');
$helper->log($worksheet->getCell("B{$row}")->getValue());
$helper->log("ISPMT() Month {$month} Result is " . $worksheet->getCell("B{$row}")->getFormattedValue());
}