Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find title #893

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org).

## [Unreleased] -

### Fixed
- Fix getSheetByName function in XLSX Worksheet - [#739](https://github.com/PHPOffice/PhpSpreadsheet/issues/739)

### Added

- Added support for inline styles in Html reader (borders, alignment, width, height)
Expand Down
3 changes: 2 additions & 1 deletion src/PhpSpreadsheet/Spreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,8 @@ public function getSheetByName($pName)
{
$worksheetCount = count($this->workSheetCollection);
for ($i = 0; $i < $worksheetCount; ++$i) {
if ($this->workSheetCollection[$i]->getTitle() === $pName) {
if (($this->workSheetCollection[$i]->getTitle() === $pName)
|| $this->workSheetCollection[$i]->getTitle() === trim($pName, "'")) {
return $this->workSheetCollection[$i];
}
}
Expand Down
57 changes: 57 additions & 0 deletions tests/PhpSpreadsheetTests/SpreadsheetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;

class SpreadsheetTest extends TestCase
{
/** @var Spreadsheet */
private $object;

public function setUp()
{
parent::setUp();
$this->object = new Spreadsheet();
$sheet = $this->object->getActiveSheet();

$sheet->setTitle('someSheet1');
$sheet = new Worksheet();
$sheet->setTitle('someSheet2');
$this->object->addSheet($sheet);
$sheet = new Worksheet();
$sheet->setTitle('someSheet 3');
$this->object->addSheet($sheet);
}

/**
* @return array
*/
public function dataProviderForSheetNames()
{
$array = [
[0, 'someSheet1'],
[0, "'someSheet1'"],
[1, 'someSheet2'],
[1, "'someSheet2'"],
[2, 'someSheet 3'],
[2, "'someSheet 3'"],
];

return $array;
}

/**
* @param $index
* @param $sheetname
*
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @dataProvider dataProviderForSheetNames
*/
public function testGetSheetByName($index, $sheetname)
{
$this->assertEquals($this->object->getSheet($index), $this->object->getSheetByName($sheetname));
}
}
4 changes: 4 additions & 0 deletions tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public function extractSheetTitleProvider()
['B2', '', '', 'B2'],
['testTitle!B2', 'testTitle', 'B2', 'B2'],
['test!Title!B2', 'test!Title', 'B2', 'B2'],
['test!Title!B2', 'test!Title', 'B2', 'B2'],
["'testSheet 1'!A3", "'testSheet 1'", 'A3', 'A3'],
["'testSheet1'!A2", "'testSheet1'", 'A2', 'A2'],
["'testSheet 2'!A1", "'testSheet 2'", 'A1', 'A1'],
];
}

Expand Down