Stacked Alignment - Use Class Constant Rather than Literal (#1716)
* Stacked Alignment - Use Class Constant Rather than Literal PR #1580 defined constants for "stacked" alignment in cells. Using those constants outside of Style/Alignment was beyond the scope of the original PR, but I said I would get to it. This PR replaces all uses of literal -165, and appropriate uses of literal 255, with the named constants, and adds tests to make sure that the changed code is covered in the test suite.
This commit is contained in:
parent
30717fd35a
commit
2fac9ee2f7
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
|
||||
require __DIR__ . '/../Header.php';
|
||||
|
||||
$helper->log('Load from Xls template');
|
||||
$reader = IOFactory::createReader('Xls');
|
||||
$spreadsheet = $reader->load(__DIR__ . '/../templates/30templatebiff5.xls');
|
||||
|
||||
$helper->log('Add new data to the template');
|
||||
$data = [['title' => 'Excel for dummies',
|
||||
'price' => 17.99,
|
||||
'quantity' => 2,
|
||||
],
|
||||
['title' => 'PHP for dummies',
|
||||
'price' => 15.99,
|
||||
'quantity' => 1,
|
||||
],
|
||||
['title' => 'Inside OOP',
|
||||
'price' => 12.95,
|
||||
'quantity' => 1,
|
||||
],
|
||||
];
|
||||
|
||||
$spreadsheet->getActiveSheet()->setCellValue('D1', Date::PHPToExcel(time()));
|
||||
|
||||
$baseRow = 5;
|
||||
foreach ($data as $r => $dataRow) {
|
||||
$row = $baseRow + $r;
|
||||
$spreadsheet->getActiveSheet()->insertNewRowBefore($row, 1);
|
||||
|
||||
$spreadsheet->getActiveSheet()->setCellValue('A' . $row, $r + 1)
|
||||
->setCellValue('B' . $row, $dataRow['title'])
|
||||
->setCellValue('C' . $row, $dataRow['price'])
|
||||
->setCellValue('D' . $row, $dataRow['quantity'])
|
||||
->setCellValue('E' . $row, '=C' . $row . '*D' . $row);
|
||||
}
|
||||
$spreadsheet->getActiveSheet()->removeRow($baseRow - 1, 1);
|
||||
|
||||
// Save
|
||||
$helper->write($spreadsheet, __FILE__);
|
||||
Binary file not shown.
Binary file not shown.
|
|
@ -2286,8 +2286,8 @@ class Xls extends BaseReader
|
|||
$rotation = $angle;
|
||||
} elseif ($angle <= 180) {
|
||||
$rotation = 90 - $angle;
|
||||
} elseif ($angle == 255) {
|
||||
$rotation = -165;
|
||||
} elseif ($angle == Alignment::TEXTROTATION_STACK_EXCEL) {
|
||||
$rotation = Alignment::TEXTROTATION_STACK_PHPSPREADSHEET;
|
||||
}
|
||||
$objStyle->getAlignment()->setTextRotation($rotation);
|
||||
|
||||
|
|
@ -2389,7 +2389,7 @@ class Xls extends BaseReader
|
|||
|
||||
break;
|
||||
case 1:
|
||||
$objStyle->getAlignment()->setTextRotation(-165);
|
||||
$objStyle->getAlignment()->setTextRotation(Alignment::TEXTROTATION_STACK_PHPSPREADSHEET);
|
||||
|
||||
break;
|
||||
case 2:
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Shared;
|
|||
|
||||
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
|
||||
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
||||
|
||||
class Font
|
||||
{
|
||||
|
|
@ -342,7 +343,7 @@ class Font
|
|||
|
||||
// Calculate approximate rotated column width
|
||||
if ($rotation !== 0) {
|
||||
if ($rotation == -165) {
|
||||
if ($rotation == Alignment::TEXTROTATION_STACK_PHPSPREADSHEET) {
|
||||
// stacked text
|
||||
$columnWidth = 4; // approximation
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -165,9 +165,9 @@ class Xf
|
|||
self::mapBorderStyle($this->_style->getBorders()->getTop()->getBorderStyle()) ||
|
||||
self::mapBorderStyle($this->_style->getBorders()->getLeft()->getBorderStyle()) ||
|
||||
self::mapBorderStyle($this->_style->getBorders()->getRight()->getBorderStyle())) ? 1 : 0;
|
||||
$atr_pat = (($this->foregroundColor != 0x40) ||
|
||||
($this->backgroundColor != 0x41) ||
|
||||
self::mapFillType($this->_style->getFill()->getFillType())) ? 1 : 0;
|
||||
$atr_pat = ($this->foregroundColor != 0x40) ? 1 : 0;
|
||||
$atr_pat = ($this->backgroundColor != 0x41) ? 1 : $atr_pat;
|
||||
$atr_pat = self::mapFillType($this->_style->getFill()->getFillType()) ? 1 : $atr_pat;
|
||||
$atr_prot = self::mapLocked($this->_style->getProtection()->getLocked())
|
||||
| self::mapHidden($this->_style->getProtection()->getHidden());
|
||||
|
||||
|
|
@ -375,11 +375,7 @@ class Xf
|
|||
*/
|
||||
private static function mapBorderStyle($borderStyle)
|
||||
{
|
||||
if (isset(self::$mapBorderStyles[$borderStyle])) {
|
||||
return self::$mapBorderStyles[$borderStyle];
|
||||
}
|
||||
|
||||
return 0x00;
|
||||
return self::$mapBorderStyles[$borderStyle] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -420,11 +416,7 @@ class Xf
|
|||
*/
|
||||
private static function mapFillType($fillType)
|
||||
{
|
||||
if (isset(self::$mapFillTypes[$fillType])) {
|
||||
return self::$mapFillTypes[$fillType];
|
||||
}
|
||||
|
||||
return 0x00;
|
||||
return self::$mapFillTypes[$fillType] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -451,11 +443,7 @@ class Xf
|
|||
*/
|
||||
private function mapHAlign($hAlign)
|
||||
{
|
||||
if (isset(self::$mapHAlignments[$hAlign])) {
|
||||
return self::$mapHAlignments[$hAlign];
|
||||
}
|
||||
|
||||
return 0;
|
||||
return self::$mapHAlignments[$hAlign] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -479,11 +467,7 @@ class Xf
|
|||
*/
|
||||
private static function mapVAlign($vAlign)
|
||||
{
|
||||
if (isset(self::$mapVAlignments[$vAlign])) {
|
||||
return self::$mapVAlignments[$vAlign];
|
||||
}
|
||||
|
||||
return 2;
|
||||
return self::$mapVAlignments[$vAlign] ?? 2;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -497,15 +481,22 @@ class Xf
|
|||
{
|
||||
if ($textRotation >= 0) {
|
||||
return $textRotation;
|
||||
} elseif ($textRotation == -165) {
|
||||
return 255;
|
||||
} elseif ($textRotation < 0) {
|
||||
return 90 - $textRotation;
|
||||
}
|
||||
if ($textRotation == Alignment::TEXTROTATION_STACK_PHPSPREADSHEET) {
|
||||
return Alignment::TEXTROTATION_STACK_EXCEL;
|
||||
}
|
||||
|
||||
return 90 - $textRotation;
|
||||
}
|
||||
|
||||
private const LOCK_ARRAY = [
|
||||
Protection::PROTECTION_INHERIT => 1,
|
||||
Protection::PROTECTION_PROTECTED => 1,
|
||||
Protection::PROTECTION_UNPROTECTED => 0,
|
||||
];
|
||||
|
||||
/**
|
||||
* Map locked.
|
||||
* Map locked values.
|
||||
*
|
||||
* @param string $locked
|
||||
*
|
||||
|
|
@ -513,18 +504,15 @@ class Xf
|
|||
*/
|
||||
private static function mapLocked($locked)
|
||||
{
|
||||
switch ($locked) {
|
||||
case Protection::PROTECTION_INHERIT:
|
||||
return 1;
|
||||
case Protection::PROTECTION_PROTECTED:
|
||||
return 1;
|
||||
case Protection::PROTECTION_UNPROTECTED:
|
||||
return 0;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
return array_key_exists($locked, self::LOCK_ARRAY) ? self::LOCK_ARRAY[$locked] : 1;
|
||||
}
|
||||
|
||||
private const HIDDEN_ARRAY = [
|
||||
Protection::PROTECTION_INHERIT => 0,
|
||||
Protection::PROTECTION_PROTECTED => 1,
|
||||
Protection::PROTECTION_UNPROTECTED => 0,
|
||||
];
|
||||
|
||||
/**
|
||||
* Map hidden.
|
||||
*
|
||||
|
|
@ -534,15 +522,6 @@ class Xf
|
|||
*/
|
||||
private static function mapHidden($hidden)
|
||||
{
|
||||
switch ($hidden) {
|
||||
case Protection::PROTECTION_INHERIT:
|
||||
return 0;
|
||||
case Protection::PROTECTION_PROTECTED:
|
||||
return 1;
|
||||
case Protection::PROTECTION_UNPROTECTED:
|
||||
return 0;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
return array_key_exists($hidden, self::HIDDEN_ARRAY) ? self::HIDDEN_ARRAY[$hidden] : 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace PhpOffice\PhpSpreadsheetTests\Functional;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Protection;
|
||||
|
||||
class ActiveSheetTest extends AbstractFunctional
|
||||
{
|
||||
|
|
@ -46,6 +47,12 @@ class ActiveSheetTest extends AbstractFunctional
|
|||
|
||||
$spreadsheet->createSheet(2);
|
||||
|
||||
$spreadsheet->setActiveSheetIndex(2)
|
||||
->setCellValue('F1', 2)
|
||||
->getCell('F1')
|
||||
->getStyle()
|
||||
->getProtection()
|
||||
->setHidden(Protection::PROTECTION_PROTECTED);
|
||||
$spreadsheet->setActiveSheetIndex(2)
|
||||
->setTitle('Test3')
|
||||
->setCellValue('A1', 4)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace PhpOffice\PhpSpreadsheetTests\Shared;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Font;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Font as StyleFont;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FontTest extends TestCase
|
||||
|
|
@ -83,4 +84,16 @@ class FontTest extends TestCase
|
|||
{
|
||||
return require 'tests/data/Shared/CentimeterSizeToPixels.php';
|
||||
}
|
||||
|
||||
public function testVerdanaRotation(): void
|
||||
{
|
||||
$font = new StyleFont();
|
||||
$font->setName('Verdana')->setSize(10);
|
||||
$width = Font::getTextWidthPixelsApprox('n', $font, 0);
|
||||
self::assertEquals(8, $width);
|
||||
$width = Font::getTextWidthPixelsApprox('n', $font, 45);
|
||||
self::assertEquals(7, $width);
|
||||
$width = Font::getTextWidthPixelsApprox('n', $font, -165);
|
||||
self::assertEquals(4, $width);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue