From 85eabc21aaa041a728ad60f3c76388939ec0513f Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Thu, 23 Aug 2012 00:16:40 +0100 Subject: [PATCH] Various bugfixes including Excel2007 Writer order of font style elements to conform with Excel2003 using compatibility pack --- Classes/PHPExcel/Shared/File.php | 6 +- Classes/PHPExcel/Writer/Excel2007/Style.php | 65 +++++++++++---------- Tests/08conditionalformatting.php | 12 ++-- Tests/runall.php | 2 + changelog.txt | 2 + 5 files changed, 48 insertions(+), 39 deletions(-) diff --git a/Classes/PHPExcel/Shared/File.php b/Classes/PHPExcel/Shared/File.php index 046bc49e..be0df9d4 100644 --- a/Classes/PHPExcel/Shared/File.php +++ b/Classes/PHPExcel/Shared/File.php @@ -110,13 +110,13 @@ class PHPExcel_Shared_File if ( !function_exists('sys_get_temp_dir')) { if ($temp = getenv('TMP') ) { - if (file_exists($temp)) { return realpath($temp); } + if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); } } if ($temp = getenv('TEMP') ) { - if (file_exists($temp)) { return realpath($temp); } + if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); } } if ($temp = getenv('TMPDIR') ) { - if (file_exists($temp)) { return realpath($temp); } + if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); } } // trick for creating a file in system's temporary dir diff --git a/Classes/PHPExcel/Writer/Excel2007/Style.php b/Classes/PHPExcel/Writer/Excel2007/Style.php index daf9f3ef..b830f9e9 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Style.php +++ b/Classes/PHPExcel/Writer/Excel2007/Style.php @@ -253,11 +253,13 @@ class PHPExcel_Writer_Excel2007_Style extends PHPExcel_Writer_Excel2007_WriterPa $objWriter->endElement(); } - // bgColor - if ($pFill->getEndColor()->getARGB()) { - $objWriter->startElement('bgColor'); - $objWriter->writeAttribute('rgb', $pFill->getEndColor()->getARGB()); - $objWriter->endElement(); + if ($pFill->getFillType() !== PHPExcel_Style_Fill::FILL_SOLID) { + // bgColor + if ($pFill->getEndColor()->getARGB()) { + $objWriter->startElement('bgColor'); + $objWriter->writeAttribute('rgb', $pFill->getEndColor()->getARGB()); + $objWriter->endElement(); + } } } @@ -277,20 +279,9 @@ class PHPExcel_Writer_Excel2007_Style extends PHPExcel_Writer_Excel2007_WriterPa { // font $objWriter->startElement('font'); - - // Name - if ($pFont->getName() !== NULL) { - $objWriter->startElement('name'); - $objWriter->writeAttribute('val', $pFont->getName()); - $objWriter->endElement(); - } - - // Size - if ($pFont->getSize() !== NULL) { - $objWriter->startElement('sz'); - $objWriter->writeAttribute('val', $pFont->getSize()); - $objWriter->endElement(); - } + // Weird! The order of these elements actually makes a difference when opening Excel2007 + // files in Excel2003 with the compatibility pack. It's not documented behaviour, + // and makes for a real WTF! // Bold. We explicitly write this element also when false (like MS Office Excel 2007 does // for conditional formatting). Otherwise it will apparently not be picked up in conditional @@ -308,6 +299,20 @@ class PHPExcel_Writer_Excel2007_Style extends PHPExcel_Writer_Excel2007_WriterPa $objWriter->endElement(); } + // Strikethrough + if ($pFont->getStrikethrough() !== NULL) { + $objWriter->startElement('strike'); + $objWriter->writeAttribute('val', $pFont->getStrikethrough() ? '1' : '0'); + $objWriter->endElement(); + } + + // Underline + if ($pFont->getUnderline() !== NULL) { + $objWriter->startElement('u'); + $objWriter->writeAttribute('val', $pFont->getUnderline()); + $objWriter->endElement(); + } + // Superscript / subscript if ($pFont->getSuperScript() === TRUE || $pFont->getSubScript() === TRUE) { $objWriter->startElement('vertAlign'); @@ -319,17 +324,10 @@ class PHPExcel_Writer_Excel2007_Style extends PHPExcel_Writer_Excel2007_WriterPa $objWriter->endElement(); } - // Underline - if ($pFont->getUnderline() !== NULL) { - $objWriter->startElement('u'); - $objWriter->writeAttribute('val', $pFont->getUnderline()); - $objWriter->endElement(); - } - - // Strikethrough - if ($pFont->getStrikethrough() !== NULL) { - $objWriter->startElement('strike'); - $objWriter->writeAttribute('val', $pFont->getStrikethrough() ? '1' : '0'); + // Size + if ($pFont->getSize() !== NULL) { + $objWriter->startElement('sz'); + $objWriter->writeAttribute('val', $pFont->getSize()); $objWriter->endElement(); } @@ -340,6 +338,13 @@ class PHPExcel_Writer_Excel2007_Style extends PHPExcel_Writer_Excel2007_WriterPa $objWriter->endElement(); } + // Name + if ($pFont->getName() !== NULL) { + $objWriter->startElement('name'); + $objWriter->writeAttribute('val', $pFont->getName()); + $objWriter->endElement(); + } + $objWriter->endElement(); } diff --git a/Tests/08conditionalformatting.php b/Tests/08conditionalformatting.php index 36f4ec8f..75716cc0 100644 --- a/Tests/08conditionalformatting.php +++ b/Tests/08conditionalformatting.php @@ -100,7 +100,7 @@ $objConditional2->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS) ->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_LESSTHAN) ->addCondition('0'); $objConditional2->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED); -$objConditional2->getStyle()->getFont()->setBold(true); +$objConditional2->getStyle()->getFont()->setItalic(true); $objConditional2->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE); $objConditional3 = new PHPExcel_Style_Conditional(); @@ -108,7 +108,7 @@ $objConditional3->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS) ->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_GREATERTHANOREQUAL) ->addCondition('0'); $objConditional3->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN); -$objConditional3->getStyle()->getFont()->setBold(true); +$objConditional3->getStyle()->getFont()->setItalic(true); $objConditional3->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE); $conditionalStyles = $objPHPExcel->getActiveSheet()->getStyle('B2')->getConditionalStyles(); @@ -128,10 +128,10 @@ $objPHPExcel->getActiveSheet()->duplicateConditionalStyle( // Set fonts echo date('H:i:s') , " Set fonts" , EOL; -$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); -$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); -$objPHPExcel->getActiveSheet()->getStyle('A7')->getFont()->setBold(true); -$objPHPExcel->getActiveSheet()->getStyle('B7')->getFont()->setBold(true); +$objPHPExcel->getActiveSheet()->getStyle('A1:B1')->getFont()->setBold(true); +//$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); +$objPHPExcel->getActiveSheet()->getStyle('A7:B7')->getFont()->setBold(true); +//$objPHPExcel->getActiveSheet()->getStyle('B7')->getFont()->setBold(true); // Set header and footer. When no different headers for odd/even are used, odd header is assumed. diff --git a/Tests/runall.php b/Tests/runall.php index d421dcdc..923395d2 100644 --- a/Tests/runall.php +++ b/Tests/runall.php @@ -66,6 +66,8 @@ $aTests = array( , '31docproperties_write.php' , '31docproperties_write-xls.php' , '32chartreadwrite.php' + , '33chartcreate.php' + , '34chartupdate.php' , 'OOCalcReader.php' , 'SylkReader.php' , 'Excel2003XMLReader.php' diff --git a/changelog.txt b/changelog.txt index 514a52db..193fba8b 100644 --- a/changelog.txt +++ b/changelog.txt @@ -106,6 +106,8 @@ Fixed in develop branch: - Bugfix: (MBaker) Work item 18145 - Autoshape being identified in twoCellAnchor when includeCharts is TRUE triggering load error - Bugfix: (MBaker) Work item 18325 - v-type texts for series labels now recognised and parsed correctly - Bugfix: (wolf5x) Work item 18492 - load file failed if the file has no extensionType +- Bugfix: (dverspui) Pattern fill colours in Excel2007 Style Writer +- Bugfix: (MBaker) Excel2007 Writer order of font style elements to conform with Excel2003 using compatibility pack 2012-05-19 (v1.7.7):