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

Stacked Alignment - Use Class Constant Rather than Literal #1716

Merged
merged 5 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
43 changes: 43 additions & 0 deletions samples/Basic/30_Templatebiff5.php
Original file line number Diff line number Diff line change
@@ -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 modified samples/templates/30template.xls
Binary file not shown.
Binary file added samples/templates/30templatebiff5.xls
Binary file not shown.
6 changes: 3 additions & 3 deletions src/PhpSpreadsheet/Reader/Xls.php
Original file line number Diff line number Diff line change
Expand Up @@ -2286,8 +2286,8 @@ private function readXf(): void
$rotation = $angle;
} elseif ($angle <= 180) {
$rotation = 90 - $angle;
} elseif ($angle == 255) {
$rotation = -165;
} elseif ($angle == Alignment::TEXTROTATION_STACK_EXCEL) {
$rotation = Alignment::TEXTROTATION_STACK_PHPSPREADSHEET;
oleibman marked this conversation as resolved.
Show resolved Hide resolved
}
$objStyle->getAlignment()->setTextRotation($rotation);

Expand Down Expand Up @@ -2389,7 +2389,7 @@ private function readXf(): void

break;
case 1:
$objStyle->getAlignment()->setTextRotation(-165);
$objStyle->getAlignment()->setTextRotation(Alignment::TEXTROTATION_STACK_PHPSPREADSHEET);

break;
case 2:
Expand Down
3 changes: 2 additions & 1 deletion src/PhpSpreadsheet/Shared/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

class Font
{
Expand Down Expand Up @@ -342,7 +343,7 @@ public static function getTextWidthPixelsApprox($columnText, \PhpOffice\PhpSprea

// Calculate approximate rotated column width
if ($rotation !== 0) {
if ($rotation == -165) {
if ($rotation == Alignment::TEXTROTATION_STACK_PHPSPREADSHEET) {
// stacked text
$columnWidth = 4; // approximation
} else {
Expand Down
55 changes: 16 additions & 39 deletions src/PhpSpreadsheet/Writer/Xls/Xf.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ public function writeXf()
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());

Expand Down Expand Up @@ -375,11 +375,7 @@ public function setFontIndex($value): void
*/
private static function mapBorderStyle($borderStyle)
{
if (isset(self::$mapBorderStyles[$borderStyle])) {
return self::$mapBorderStyles[$borderStyle];
}

return 0x00;
return self::$mapBorderStyles[$borderStyle] ?? 0;
}

/**
Expand Down Expand Up @@ -420,11 +416,7 @@ private static function mapBorderStyle($borderStyle)
*/
private static function mapFillType($fillType)
{
if (isset(self::$mapFillTypes[$fillType])) {
return self::$mapFillTypes[$fillType];
}

return 0x00;
return self::$mapFillTypes[$fillType] ?? 0;
}

/**
Expand All @@ -451,11 +443,7 @@ private static function mapFillType($fillType)
*/
private function mapHAlign($hAlign)
{
if (isset(self::$mapHAlignments[$hAlign])) {
return self::$mapHAlignments[$hAlign];
}

return 0;
return self::$mapHAlignments[$hAlign] ?? 0;
}

/**
Expand All @@ -479,11 +467,7 @@ private function mapHAlign($hAlign)
*/
private static function mapVAlign($vAlign)
{
if (isset(self::$mapVAlignments[$vAlign])) {
return self::$mapVAlignments[$vAlign];
}

return 2;
return self::$mapVAlignments[$vAlign] ?? 2;
}

/**
Expand All @@ -497,11 +481,12 @@ private static function mapTextRotation($textRotation)
{
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;
}

/**
Expand All @@ -514,15 +499,11 @@ private static function mapTextRotation($textRotation)
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 1;
oleibman marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -535,14 +516,10 @@ private static function mapLocked($locked)
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 0;
oleibman marked this conversation as resolved.
Show resolved Hide resolved
}
oleibman marked this conversation as resolved.
Show resolved Hide resolved
}
7 changes: 7 additions & 0 deletions tests/PhpSpreadsheetTests/Functional/ActiveSheetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Functional;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Protection;

class ActiveSheetTest extends AbstractFunctional
{
Expand Down Expand Up @@ -46,6 +47,12 @@ public function testActiveSheet($format): void

$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)
Expand Down
13 changes: 13 additions & 0 deletions tests/PhpSpreadsheetTests/Shared/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -83,4 +84,16 @@ public function providerCentimeterSizeToPixels()
{
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);
}
}