Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Worksheet/Table/ColumnTest.php
T
aswinkumar863 92a50d134f Format as Table for Xlsx
Initial implementation of Excel's tables feature (i.e. Select Home >
Format as Table in Excel App).

Tables are similar to AutoFilter but tables have other advantages like
named ranges, easy formatting, totals row and header row with filter.
Tables can also be converted to charts and pivot tables easily.

Usage:
$table = new Table();
$table->setName('Sales_Data');
$table->setRange('A1:D17');
$spreadsheet->getActiveSheet()->addTable($table);

In this Commit:
- Added Table API with initial support for header and totals row.
- Added complete styling options for Table.
- Added Xlsx Writer for Table.
- Added samples.
- Covered with unit tests.

To be done:
- Filter expressions similar to AutoFilter.
- Precalucate formulas for totals row (Check sample 2).
- Table named ranges in formulas and calculation.
2022-03-10 19:54:45 +05:30

87 lines
2.7 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
use PhpOffice\PhpSpreadsheet\Worksheet\Table\Column;
class ColumnTest extends SetupTeardown
{
protected function initTable(): Table
{
$sheet = $this->getSheet();
$sheet->getCell('G1')->setValue('Heading');
$sheet->getCell('G2')->setValue(2);
$sheet->getCell('G3')->setValue(3);
$sheet->getCell('G4')->setValue(4);
$sheet->getCell('H1')->setValue('Heading2');
$sheet->getCell('H2')->setValue(1);
$sheet->getCell('H3')->setValue(2);
$sheet->getCell('H4')->setValue(3);
$this->maxRow = $maxRow = 4;
$table = new Table();
$table->setRange("G1:H$maxRow");
return $table;
}
public function testVariousGets(): void
{
$table = $this->initTable();
$column = $table->getColumn('H');
$result = $column->getColumnIndex();
self::assertEquals('H', $result);
}
public function testGetBadColumnIndex(): void
{
$this->expectException(PhpSpreadsheetException::class);
$this->expectExceptionMessage('Column is outside of current table range.');
$table = $this->initTable();
$table->getColumn('B');
}
public function testSetColumnIndex(): void
{
$table = $this->initTable();
$column = $table->getColumn('H');
$column->setShowFilterButton(false);
$expectedResult = 'G';
$result = $column->setColumnIndex($expectedResult);
self::assertInstanceOf(Column::class, $result);
$result = $result->getColumnIndex();
self::assertEquals($expectedResult, $result);
}
public function testVariousSets(): void
{
$table = $this->initTable();
$column = $table->getColumn('H');
$result = $column->setShowFilterButton(false);
self::assertInstanceOf(Column::class, $result);
self::assertFalse($column->getShowFilterButton());
$label = 'Total';
$result = $column->setTotalsRowLabel($label);
self::assertInstanceOf(Column::class, $result);
self::assertEquals($label, $column->getTotalsRowLabel());
$function = 'sum';
$result = $column->setTotalsRowFunction($function);
self::assertInstanceOf(Column::class, $result);
self::assertEquals($function, $column->getTotalsRowFunction());
}
public function testTable(): void
{
$table = $this->initTable();
$column = new Column('H');
$column->setTable($table);
self::assertEquals($table, $column->getTable());
}
}