Skip to content

Commit f52ae20

Browse files
authored
Merge pull request #4412 from mikkokoo/master
Conditional and table formatting support for html
2 parents 8e3417d + 584c866 commit f52ae20

31 files changed

+1215
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1313
- Add FormulaRange to IgnoredErrors. [PR #4393](https://github.com/PHPOffice/PhpSpreadsheet/pull/4393)
1414
- TextGrid improvements. [PR #4418](https://github.com/PHPOffice/PhpSpreadsheet/pull/4418)
1515
- Permit read to class which extends Spreadsheet. [Discussion #4402](https://github.com/PHPOffice/PhpSpreadsheet/discussions/4402) [PR #4404](https://github.com/PHPOffice/PhpSpreadsheet/pull/4404)
16+
- Conditional and table formatting support for html writer [PR #4412](https://github.com/PHPOffice/PhpSpreadsheet/pull/4412)
1617

1718
### Removed
1819

docs/topics/conditional-formatting.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,28 @@ Currently, the following Conditional Types are supported for the following Reade
143143

144144
MS Excel | Conditional Type | Readers | Writers
145145
---|---|---|---
146-
| Cell Value | Conditional::CONDITION_CELLIS | Xlsx | Xlsx, Xls
147-
Specific Text | Conditional::CONDITION_CONTAINSTEXT | Xlsx | Xlsx
148-
| Conditional::CONDITION_NOTCONTAINSTEXT | Xlsx | Xlsx
149-
| Conditional::CONDITION_BEGINSWITH | Xlsx | Xlsx
150-
| Conditional::CONDITION_ENDSWITH | Xlsx | Xlsx
151-
Dates Occurring | Conditional::CONDITION_TIMEPERIOD | Xlsx | Xlsx
152-
Blanks | Conditional::CONDITION_CONTAINSBLANKS | Xlsx | Xlsx
153-
No Blanks | Conditional::CONDITION_NOTCONTAINSBLANKS | Xlsx | Xlsx
154-
Errors | Conditional::CONDITION_CONTAINSERRORS | Xlsx | Xlsx
155-
No Errors | Conditional::CONDITION_NOTCONTAINSERRORS | Xlsx | Xlsx
156-
Duplicates/Unique | Conditional::CONDITION_DUPLICATES | Xlsx | Xlsx
157-
| Conditional::CONDITION_UNIQUE | Xlsx | Xlsx
158-
Use a formula | Conditional::CONDITION_EXPRESSION | Xlsx | Xlsx, Xls
159-
Data Bars | Conditional::CONDITION_DATABAR | Xlsx | Xlsx
146+
| Cell Value | Conditional::CONDITION_CELLIS | Xlsx | Xlsx, Xls, Html
147+
Specific Text | Conditional::CONDITION_CONTAINSTEXT | Xlsx | Xlsx, Html
148+
| Conditional::CONDITION_NOTCONTAINSTEXT | Xlsx | Xlsx, Html
149+
| Conditional::CONDITION_BEGINSWITH | Xlsx | Xlsx, Html
150+
| Conditional::CONDITION_ENDSWITH | Xlsx | Xlsx, Html
151+
Dates Occurring | Conditional::CONDITION_TIMEPERIOD | Xlsx | Xlsx, Html
152+
Blanks | Conditional::CONDITION_CONTAINSBLANKS | Xlsx | Xlsx, Html
153+
No Blanks | Conditional::CONDITION_NOTCONTAINSBLANKS | Xlsx | Xlsx, Html
154+
Errors | Conditional::CONDITION_CONTAINSERRORS | Xlsx | Xlsx, Html
155+
No Errors | Conditional::CONDITION_NOTCONTAINSERRORS | Xlsx | Xlsx, Html
156+
Duplicates/Unique | Conditional::CONDITION_DUPLICATES | Xlsx | Xlsx, Html
157+
| Conditional::CONDITION_UNIQUE | Xlsx | Xlsx, Html
158+
Use a formula | Conditional::CONDITION_EXPRESSION | Xlsx | Xlsx, Xls, Html
159+
Data Bars | Conditional::CONDITION_DATABAR | Xlsx | Xlsx, Html
160+
Colour Scales | Conditional::COLORSCALE | Xlsx | Html
161+
162+
To enable conditional formatting for Html writer, use:
163+
164+
```php
165+
$writer = new HtmlWriter($spreadsheet);
166+
$writer->setConditionalFormatting(true);
167+
```
160168

161169
The following Conditional Types are currently not supported by any Readers or Writers:
162170

@@ -165,7 +173,6 @@ MS Excel | Conditional Type
165173
Above/Below Average | ?
166174
Top/Bottom Items | ?
167175
Top/Bottom %age | ?
168-
Colour Scales |?
169176
Icon Sets | ?
170177

171178
Unsupported types will by ignored by the Readers, and cannot be created through PHPSpreadsheet.

docs/topics/tables.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Tables
2+
3+
## Introduction
4+
5+
To make managing and analyzing a group of related data easier, you can turn a range of cells into an Excel table (previously known as an Excel list).
6+
7+
## Support
8+
9+
Currently tables are supported in Xlsx reader and Html Writer
10+
11+
To enable table formatting for Html writer, use:
12+
13+
```php
14+
$writer = new HtmlWriter($spreadsheet);
15+
$writer->setConditionalFormatting(true);
16+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\IOFactory;
4+
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
5+
6+
require __DIR__ . '/../Header.php';
7+
8+
$inputFileName = 'BasicConditionalFormatting.xlsx';
9+
$inputFilePath = __DIR__ . '/../templates/' . $inputFileName;
10+
11+
$codePath = $helper->isCli() ? ('samples/templates/' . $inputFileName) : ('<code>' . 'samples/templates/' . $inputFileName . '</code>');
12+
$helper->log('Read ' . $codePath . ' with conditional formatting');
13+
$reader = IOFactory::createReader('Xlsx');
14+
$reader->setReadDataOnly(false);
15+
$spreadsheet = $reader->load($inputFilePath);
16+
$helper->log('Enable conditional formatting output');
17+
18+
function writerCallback(HtmlWriter $writer): void
19+
{
20+
$writer->setPreCalculateFormulas(true);
21+
$writer->setConditionalFormatting(true);
22+
}
23+
24+
// Save
25+
$helper->write($spreadsheet, __FILE__, ['Html'], false, writerCallback: writerCallback(...));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\IOFactory;
4+
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
5+
6+
require __DIR__ . '/../Header.php';
7+
8+
$inputFileName = 'ConditionalFormattingConditions.xlsx';
9+
$inputFilePath = __DIR__ . '/../templates/' . $inputFileName;
10+
11+
$codePath = $helper->isCli() ? ('samples/templates/' . $inputFileName) : ('<code>' . 'samples/templates/' . $inputFileName . '</code>');
12+
$helper->log('Read ' . $codePath . ' with conditional formatting');
13+
$reader = IOFactory::createReader('Xlsx');
14+
$reader->setReadDataOnly(false);
15+
$spreadsheet = $reader->load($inputFilePath);
16+
$helper->log('Enable conditional formatting output');
17+
18+
function writerCallback(HtmlWriter $writer): void
19+
{
20+
$writer->setPreCalculateFormulas(true);
21+
$writer->setConditionalFormatting(true);
22+
}
23+
24+
// Save
25+
$helper->write($spreadsheet, __FILE__, ['Html'], false, writerCallback: writerCallback(...));

samples/Html/html_03_Color_Scale.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\IOFactory;
4+
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
5+
6+
require __DIR__ . '/../Header.php';
7+
8+
$inputFileName = 'ColourScale.xlsx';
9+
$inputFilePath = __DIR__ . '/../templates/' . $inputFileName;
10+
11+
$codePath = $helper->isCli() ? ('samples/templates/' . $inputFileName) : ('<code>' . 'samples/templates/' . $inputFileName . '</code>');
12+
$helper->log('Read ' . $codePath . ' with color scale');
13+
$reader = IOFactory::createReader('Xlsx');
14+
$reader->setReadDataOnly(false);
15+
$spreadsheet = $reader->load($inputFilePath);
16+
$helper->log('Enable conditional formatting output');
17+
18+
function writerCallback(HtmlWriter $writer): void
19+
{
20+
$writer->setPreCalculateFormulas(true);
21+
$writer->setConditionalFormatting(true);
22+
}
23+
24+
// Save
25+
$helper->write($spreadsheet, __FILE__, ['Html'], false, writerCallback: writerCallback(...));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\IOFactory;
4+
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
5+
6+
require __DIR__ . '/../Header.php';
7+
8+
$inputFileName = 'TableFormat.xlsx';
9+
$inputFilePath = __DIR__ . '/../templates/' . $inputFileName;
10+
11+
$codePath = $helper->isCli() ? ('samples/templates/' . $inputFileName) : ('<code>' . 'samples/templates/' . $inputFileName . '</code>');
12+
$helper->log('Read ' . $codePath);
13+
$reader = IOFactory::createReader('Xlsx');
14+
$reader->setReadDataOnly(false);
15+
$spreadsheet = $reader->load($inputFilePath);
16+
$helper->log('Enable table formatting output');
17+
18+
function writerCallback(HtmlWriter $writer): void
19+
{
20+
$writer->setPreCalculateFormulas(true);
21+
$writer->setTableFormats(true);
22+
}
23+
24+
// Save
25+
$helper->write($spreadsheet, __FILE__, ['Html'], false, writerCallback: writerCallback(...));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\IOFactory;
4+
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
5+
6+
require __DIR__ . '/../Header.php';
7+
8+
$inputFileName = 'TableFormat.xlsx';
9+
$inputFilePath = __DIR__ . '/../templates/' . $inputFileName;
10+
11+
$codePath = $helper->isCli() ? ('samples/templates/' . $inputFileName) : ('<code>' . 'samples/templates/' . $inputFileName . '</code>');
12+
$helper->log('Read ' . $codePath);
13+
$reader = IOFactory::createReader('Xlsx');
14+
$reader->setReadDataOnly(false);
15+
$spreadsheet = $reader->load($inputFilePath);
16+
$helper->log('Enable table formatting output');
17+
$helper->log('Enable conditional formatting output');
18+
19+
function writerCallback(HtmlWriter $writer): void
20+
{
21+
$writer->setPreCalculateFormulas(true);
22+
$writer->setTableFormats(true);
23+
$writer->setConditionalFormatting(true);
24+
}
25+
26+
// Save
27+
$helper->write($spreadsheet, __FILE__, ['Html'], false, writerCallback: writerCallback(...));
Binary file not shown.

samples/templates/ColourScale.xlsx

8.59 KB
Binary file not shown.

0 commit comments

Comments
 (0)