Files
oleibman 1e6668fe58 Copy Cell Adjusting Formula
Fix #1203. The issue actually complains about the documentation, but I think it wants documented functionality that doesn't yet exist. This PR adds a method for copying a formula from one cell to another, adjusting cell references in the formula as Excel would. For a non-formula, it copies the value without making any adjustments.
2025-08-10 13:45:03 -07:00

32 lines
1.0 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class Issue1203Test extends TestCase
{
public static function testCopyFormula(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 1);
$sheet->setCellValue('A5', 5);
$sheet->setCellValue('E5', '=A5+$A$1');
$sheet->insertNewRowBefore(5, 1);
$e5 = $sheet->getCell('E5')->getValue();
self::assertNull($e5);
self::assertSame('=A6+$A$1', $sheet->getCell('E6')->getValue());
$sheet->copyFormula('E6', 'E5');
self::assertSame('=A5+$A$1', $sheet->getCell('E5')->getValue());
$sheet->copyFormula('E6', 'H9');
self::assertSame('=D9+$A$1', $sheet->getCell('H9')->getValue());
$sheet->copyFormula('A6', 'Z9');
self::assertSame(5, $sheet->getCell('Z9')->getValue());
$spreadsheet->disconnectWorksheets();
}
}