Skip to content

Commit 24725c8

Browse files
authored
Merge branch 'master' into Issue-3511_NumberFormat-colour-indexed-palette
2 parents 44c74f8 + e5ed8e9 commit 24725c8

File tree

10 files changed

+193
-5
lines changed

10 files changed

+193
-5
lines changed

samples/Header.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@
2626
<link rel="stylesheet" href="/bootstrap/css/phpspreadsheet.css"/>
2727
<script src="/bootstrap/js/jquery.min.js"></script>
2828
<script src="/bootstrap/js/bootstrap.min.js"></script>
29+
30+
<style>
31+
body { padding-top: 200px; }
32+
</style>
2933
</head>
3034
<body>
3135
<div class="container">
32-
<div class="navbar navbar-default" role="navigation">
36+
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
3337
<div class="container-fluid">
3438
<div class="navbar-header">
3539
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
5+
6+
require __DIR__ . '/../Header.php';
7+
8+
// Create new Spreadsheet object
9+
$helper->log('Create new Spreadsheet object');
10+
$spreadsheet = new Spreadsheet();
11+
12+
// Set document properties
13+
$helper->log('Set document properties');
14+
$spreadsheet->getProperties()->setCreator('aswinkumar863')
15+
->setLastModifiedBy('aswinkumar863')
16+
->setTitle('PhpSpreadsheet Table Test Document')
17+
->setSubject('PhpSpreadsheet Table Test Document')
18+
->setDescription('Test document for PhpSpreadsheet, generated using PHP classes.')
19+
->setKeywords('office PhpSpreadsheet php')
20+
->setCategory('Table');
21+
22+
// Create the worksheet
23+
$helper->log('Add data');
24+
25+
$spreadsheet->setActiveSheetIndex(0);
26+
27+
$columnFormula = '=SUM(Sales_Data[[#This Row],[Q1]:[Q4]])';
28+
29+
$dataArray = [
30+
['Year', 'Country', 'Q1', 'Q2', 'Q3', 'Q4', 'Sales'],
31+
[2010, 'Belgium', 380, 390, 420, 460, $columnFormula],
32+
[2010, 'France', 510, 490, 460, 590, $columnFormula],
33+
[2010, 'Germany', 720, 680, 640, 660, $columnFormula],
34+
[2010, 'Italy', 440, 410, 420, 450, $columnFormula],
35+
[2010, 'Spain', 510, 490, 470, 420, $columnFormula],
36+
[2010, 'UK', 690, 610, 620, 600, $columnFormula],
37+
[2010, 'United States', 790, 730, 860, 850, $columnFormula],
38+
[2011, 'Belgium', 400, 350, 450, 500, $columnFormula],
39+
[2011, 'France', 620, 650, 415, 570, $columnFormula],
40+
[2011, 'Germany', 680, 620, 710, 690, $columnFormula],
41+
[2011, 'Italy', 430, 370, 350, 335, $columnFormula],
42+
[2011, 'Spain', 460, 390, 430, 415, $columnFormula],
43+
[2011, 'UK', 720, 650, 580, 510, $columnFormula],
44+
[2011, 'United States', 800, 700, 900, 950, $columnFormula],
45+
];
46+
47+
$spreadsheet->getActiveSheet()->fromArray($dataArray, null, 'A1');
48+
49+
// Create Table
50+
$helper->log('Create Table');
51+
$table = new Table('A1:G16', 'Sales_Data'); // +1 row for totalsRow
52+
$table->setShowTotalsRow(true);
53+
$table->setRange('A1:G16'); // +1 row for totalsRow
54+
55+
// Set Column Formula
56+
$table->getColumn('G')->setColumnFormula($columnFormula);
57+
58+
$helper->log('Add Totals Row');
59+
// Table column label not implemented yet,
60+
$table->getColumn('A')->setTotalsRowLabel('Total');
61+
// So set the label directly to the cell
62+
$spreadsheet->getActiveSheet()->getCell('A16')->setValue('Total');
63+
64+
// Table column function not implemented yet,
65+
$table->getColumn('G')->setTotalsRowFunction('sum');
66+
// So set the formula directly to the cell
67+
$spreadsheet->getActiveSheet()->getCell('G16')->setValue('=SUBTOTAL(109,Sales_Data[Sales])');
68+
69+
// Table column function not implemented yet,
70+
$table->getColumn('C')->setTotalsRowFunction('sum');
71+
$table->getColumn('D')->setTotalsRowFunction('sum');
72+
$table->getColumn('E')->setTotalsRowFunction('sum');
73+
$table->getColumn('F')->setTotalsRowFunction('sum');
74+
// So set the formulae directly to the cells
75+
$spreadsheet->getActiveSheet()->getCell('C16')->setValue('=SUBTOTAL(109,Sales_Data[Q1])');
76+
$spreadsheet->getActiveSheet()->getCell('D16')->setValue('=SUBTOTAL(109,Sales_Data[Q2])');
77+
$spreadsheet->getActiveSheet()->getCell('E16')->setValue('=SUBTOTAL(109,Sales_Data[Q3])');
78+
$spreadsheet->getActiveSheet()->getCell('F16')->setValue('=SUBTOTAL(109,Sales_Data[Q4])');
79+
80+
// Add Table to Worksheet
81+
$helper->log('Add Table to Worksheet');
82+
$spreadsheet->getActiveSheet()->addTable($table);
83+
84+
$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, false, true, true));
85+
86+
$helper->log('Calculate Structured References');
87+
88+
$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, true, true, true));
89+
90+
// Save
91+
$helper->write($spreadsheet, __FILE__, ['Xlsx']);

src/PhpSpreadsheet/Reader/Xls.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ public function __construct()
430430
*/
431431
public function canRead(string $filename): bool
432432
{
433-
if (!File::testFileNoThrow($filename)) {
433+
if (File::testFileNoThrow($filename) === false) {
434434
return false;
435435
}
436436

@@ -440,6 +440,9 @@ public function canRead(string $filename): bool
440440

441441
// get excel data
442442
$ole->read($filename);
443+
if ($ole->wrkbook === null) {
444+
throw new Exception('The filename ' . $filename . ' is not recognised as a Spreadsheet file');
445+
}
443446

444447
return true;
445448
} catch (PhpSpreadsheetException $e) {
@@ -449,7 +452,7 @@ public function canRead(string $filename): bool
449452

450453
public function setCodepage(string $codepage): void
451454
{
452-
if (!CodePage::validate($codepage)) {
455+
if (CodePage::validate($codepage) === false) {
453456
throw new PhpSpreadsheetException('Unknown codepage: ' . $codepage);
454457
}
455458

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,10 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
945945
// no style index means 0, it seems
946946
$cell->setXfIndex(isset($styles[(int) ($cAttr['s'])]) ?
947947
(int) ($cAttr['s']) : 0);
948+
// issue 3495
949+
if ($cell->getDataType() === DataType::TYPE_FORMULA) {
950+
$cell->getStyle()->setQuotePrefix(false);
951+
}
948952
}
949953
}
950954
++$rowIndex;

src/PhpSpreadsheet/Shared/OLERead.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function read(string $filename): void
134134

135135
$bbdBlocks = $this->numBigBlockDepotBlocks;
136136

137-
if ($this->numExtensionBlocks != 0) {
137+
if ($this->numExtensionBlocks !== 0) {
138138
$bbdBlocks = (self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS) / 4;
139139
}
140140

tests/PhpSpreadsheetTests/IOFactoryTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,33 @@ public function providerIdentify(): array
108108
];
109109
}
110110

111+
public function testIdentifyInvalid(): void
112+
{
113+
$file = __DIR__ . '/../data/Reader/NotASpreadsheetFile.doc';
114+
115+
$this->expectException(ReaderException::class);
116+
$this->expectExceptionMessage('Unable to identify a reader for this file');
117+
IOFactory::identify($file);
118+
}
119+
120+
public function testCreateInvalid(): void
121+
{
122+
$file = __DIR__ . '/../data/Reader/NotASpreadsheetFile.doc';
123+
124+
$this->expectException(ReaderException::class);
125+
$this->expectExceptionMessage('Unable to identify a reader for this file');
126+
IOFactory::createReaderForFile($file);
127+
}
128+
129+
public function testLoadInvalid(): void
130+
{
131+
$file = __DIR__ . '/../data/Reader/NotASpreadsheetFile.doc';
132+
133+
$this->expectException(ReaderException::class);
134+
$this->expectExceptionMessage('Unable to identify a reader for this file');
135+
IOFactory::load($file);
136+
}
137+
111138
public function testFormatAsExpected(): void
112139
{
113140
$fileName = 'samples/templates/30template.xls';

tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3464Test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public function testReadFontColor(): void
1818
$objReader = IOFactory::createReader($inputFileType);
1919
$objReader->setReadEmptyCells(false);
2020

21-
$sheet = $objReader->load(self::$testbook)->getActiveSheet();
21+
$spreadsheet = $objReader->load(self::$testbook);
22+
$sheet = $spreadsheet->getActiveSheet();
2223
$rickText = $sheet->getCell([1, 1])->getValue();
2324
self::assertInstanceOf(RichText::class, $rickText);
2425

@@ -34,5 +35,6 @@ public function testReadFontColor(): void
3435
$font = $elements[1]->getFont();
3536
self::assertNotNull($font);
3637
self::assertEquals('ff2600', $font->getColor()->getRGB());
38+
$spreadsheet->disconnectWorksheets();
3739
}
3840
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
6+
7+
class Issue3495Test extends \PHPUnit\Framework\TestCase
8+
{
9+
/**
10+
* @var string
11+
*/
12+
private static $testbook = 'tests/data/Reader/XLSX/issue.3495d.xlsx';
13+
14+
public function testPreliminaries(): void
15+
{
16+
$file = 'zip://';
17+
$file .= self::$testbook;
18+
$file2 = $file;
19+
$file .= '#xl/styles.xml';
20+
$file2 .= '#xl/worksheets/sheet1.xml';
21+
$data = file_get_contents($file);
22+
if ($data === false) {
23+
self::fail('Unable to read file');
24+
} else {
25+
// default style plus one other style
26+
self::assertStringContainsString(
27+
'<cellXfs count="2">'
28+
. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>'
29+
. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" quotePrefix="1"/>',
30+
$data
31+
);
32+
}
33+
$data = file_get_contents($file2);
34+
if ($data === false) {
35+
self::fail('Unable to read file');
36+
} else {
37+
// cells B1, C1, D1 all nominally use quotePrefix s="1"
38+
self::assertStringContainsString('<c r="B1" s="1">', $data);
39+
self::assertStringContainsString('<c r="C1" s="1" t="s">', $data);
40+
self::assertStringContainsString('<c r="D1" s="1" t="s">', $data);
41+
}
42+
}
43+
44+
public function testFormulaDespiteQuotePrefix(): void
45+
{
46+
$reader = new Xlsx();
47+
$spreadsheet = $reader->load(self::$testbook);
48+
$sheet = $spreadsheet->getActiveSheet();
49+
self::assertSame('=2+3', $sheet->getCell('B1')->getValue());
50+
self::assertSame('=1+2', $sheet->getCell('C1')->getValue());
51+
self::assertSame('3', $sheet->getCell('D1')->getValue());
52+
self::assertSame(5, $sheet->getCell('B1')->getCalculatedValue());
53+
self::assertSame('=1+2', $sheet->getCell('C1')->getCalculatedValue());
54+
self::assertSame('3', $sheet->getCell('D1')->getCalculatedValue());
55+
$spreadsheet->disconnectWorksheets();
56+
}
57+
}
27.5 KB
Binary file not shown.
10.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)