From bf97d2a50fcab0d3b29d0c6a683919aa552ebe65 Mon Sep 17 00:00:00 2001 From: quentin Date: Thu, 8 Oct 2020 10:26:30 +0200 Subject: [PATCH 1/4] NEW helper functions for export with phpspreadsheet --- .../export/export_excel2007new.modules.php | 182 ++++++++++++++++++ 1 file changed, 182 insertions(+) diff --git a/htdocs/core/modules/export/export_excel2007new.modules.php b/htdocs/core/modules/export/export_excel2007new.modules.php index 6d7cbb5d05f..0567e4b21f2 100644 --- a/htdocs/core/modules/export/export_excel2007new.modules.php +++ b/htdocs/core/modules/export/export_excel2007new.modules.php @@ -27,6 +27,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; +use PhpOffice\PhpSpreadsheet\Cell\Coordinate; /** * Class to build export files with Excel format @@ -436,4 +437,185 @@ class ExportExcel2007new extends ModeleExports return $letter; } + + /** + * Set cell value and automatically merge if we give an endcell + * + * @param string $val cell value + * @param string $startCell + * @param string $endCell + * @return int 1 if success -1 if failed + */ + public function setCellValue($val, $startCell, $endCell = '') { + try { + $this->workbook->getActiveSheet()->setCellValue($startCell, $val); + + if(! empty($endCell)) { + $cellRange = $startCell.':'.$endCell; + $this->workbook->getActiveSheet()->mergeCells($startCell.':'.$endCell); + } + else $cellRange = $startCell; + if(! empty($this->styleArray)) $this->workbook->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->styleArray); + } + catch(Exception $e) { + $this->error = $e->getMessage(); + return -1; + } + return 1; + } + + /** + * Set border style + * + * @param string $thickness style \PhpOffice\PhpSpreadsheet\Style\Border + * @param string $color color \PhpOffice\PhpSpreadsheet\Style\Color + */ + public function setBorderStyle($thickness, $color) { + $this->styleArray['borders'] = array( + 'outline' => array( + 'borderStyle' => $thickness, + 'color' => array('argb' => $color) + ) + ); + } + + /** + * Set font style + * + * @param bool $bold + * @param string $color color \PhpOffice\PhpSpreadsheet\Style\Color + */ + public function setFontStyle($bold, $color) { + $this->styleArray['font'] = array( + 'color' => array('argb' => $color), + 'bold' => $bold + ); + } + + /** + * Set alignment style (horizontal, left, right, ...) + * + * @param string $horizontal PhpOffice\PhpSpreadsheet\Style\Alignment + */ + public function setAlignmentStyle($horizontal) { + $this->styleArray['alignment'] = array('horizontal' => $horizontal); + } + + /** + * Reset Style + */ + public function resetStyle() { + $this->styleArray = array(); + } + + /** + * Make a NxN Block in sheet + * + * @param string $startCell + * @param array $TDatas array(ColumnName=>array(Row value 1, row value 2, etc ...)) + * @param bool $boldTitle + * @return int 1 if OK, -1 if KO + */ + public function setBlock($startCell, $TDatas = array(), $boldTitle = false) { + try { + if(! empty($TDatas)) { + $startCell = $this->workbook->getActiveSheet()->getCell($startCell); + $startColumn = Coordinate::columnIndexFromString($startCell->getColumn()); + $startRow = $startCell->getRow(); + foreach($TDatas as $column => $TRows) { + if($boldTitle) $this->setFontStyle(true, $this->styleArray['font']['color']['argb']); + $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow); + $this->setCellValue($column, $cell->getCoordinate()); + $rowPos = $startRow; + if($boldTitle) $this->setFontStyle(false, $this->styleArray['font']['color']['argb']); + foreach($TRows as $row) { + $rowPos++; + $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $rowPos); + $this->setCellValue($row, $cell->getCoordinate()); + } + $startColumn++; + } + } + } + catch(Exception $e) { + $this->error = $e->getMessage(); + return -1; + } + return 1; + } + + /** + * Make a 2xN Tab in Sheet + * + * @param string $startCell A1 + * @param array $TDatas array(Title=>val) + * @param bool $boldTitle + * @return int 1 if OK, -1 if KO + */ + public function setBlock2Columns($startCell, $TDatas = array(), $boldTitle = false) { + try { + if(! empty($TDatas)) { + $startCell = $this->workbook->getActiveSheet()->getCell($startCell); + $startColumn = Coordinate::columnIndexFromString($startCell->getColumn()); + $startRow = $startCell->getRow(); + foreach($TDatas as $title => $val) { + $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow); + if($boldTitle) $this->setFontStyle(true, $this->styleArray['font']['color']['argb']); + $this->setCellValue($title, $cell->getCoordinate()); + if($boldTitle) $this->setFontStyle(false, $this->styleArray['font']['color']['argb']); + $cell2 = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + 1, $startRow); + $this->setCellValue($val, $cell2->getCoordinate()); + $startRow++; + } + } + } + catch(Exception $e) { + $this->error = $e->getMessage(); + return -1; + } + return 1; + } + + /** + * Enable auto sizing for column range + * + * @param string $firstColumn + * @param string $lastColumn + */ + public function enableAutosize($firstColumn, $lastColumn) { + foreach(range($firstColumn, $lastColumn) as $columnID) { + $this->workbook->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true); + } + } + + /** + * Set a value cell and merging it by giving a starting cell and a length + * + * @param string $val Cell value + * @param string $startCell Starting cell + * @param int $length Length + * @param int $offset Starting offset + * @return string Coordinate or -1 if KO + */ + public function setMergeCellValueByLength($val, $startCell, $length, $offset = 0) { + try { + $startCell = $this->workbook->getActiveSheet()->getCell($startCell); + $startColumn = Coordinate::columnIndexFromString($startCell->getColumn()); + if(! empty($offset)) $startColumn += $offset; + + $startRow = $startCell->getRow(); + $startCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow); + $startCoordinate = $startCell->getCoordinate(); + $this->setCellValue($val, $startCell->getCoordinate()); + + $endCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + ($length - 1), $startRow); + $endCoordinate = $endCell->getCoordinate(); + $this->workbook->getActiveSheet()->mergeCells($startCoordinate.':'.$endCoordinate); + } + catch(Exception $e) { + $this->error = $e->getMessage(); + return -1; + } + return $endCoordinate; + } } From fe8d7f7f1b6a071e4628b328e73e5a2c81292421 Mon Sep 17 00:00:00 2001 From: quentin Date: Thu, 8 Oct 2020 10:32:16 +0200 Subject: [PATCH 2/4] missing public parameter --- htdocs/core/modules/export/export_excel2007new.modules.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/htdocs/core/modules/export/export_excel2007new.modules.php b/htdocs/core/modules/export/export_excel2007new.modules.php index 0567e4b21f2..31b3fcfdd0b 100644 --- a/htdocs/core/modules/export/export_excel2007new.modules.php +++ b/htdocs/core/modules/export/export_excel2007new.modules.php @@ -60,6 +60,8 @@ class ExportExcel2007new extends ModeleExports public $worksheet; // Handle sheet + public $styleArray; + public $row; public $col; From f5b34370b6b2029eb1dc2b164e0726f0810679d5 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 8 Oct 2020 08:34:40 +0000 Subject: [PATCH 3/4] Fixing style errors. --- .../export/export_excel2007new.modules.php | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/htdocs/core/modules/export/export_excel2007new.modules.php b/htdocs/core/modules/export/export_excel2007new.modules.php index 31b3fcfdd0b..6ba508192ab 100644 --- a/htdocs/core/modules/export/export_excel2007new.modules.php +++ b/htdocs/core/modules/export/export_excel2007new.modules.php @@ -448,18 +448,19 @@ class ExportExcel2007new extends ModeleExports * @param string $endCell * @return int 1 if success -1 if failed */ - public function setCellValue($val, $startCell, $endCell = '') { + public function setCellValue($val, $startCell, $endCell = '') + { try { $this->workbook->getActiveSheet()->setCellValue($startCell, $val); - if(! empty($endCell)) { + if (! empty($endCell)) { $cellRange = $startCell.':'.$endCell; $this->workbook->getActiveSheet()->mergeCells($startCell.':'.$endCell); } else $cellRange = $startCell; - if(! empty($this->styleArray)) $this->workbook->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->styleArray); + if (! empty($this->styleArray)) $this->workbook->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->styleArray); } - catch(Exception $e) { + catch (Exception $e) { $this->error = $e->getMessage(); return -1; } @@ -472,7 +473,8 @@ class ExportExcel2007new extends ModeleExports * @param string $thickness style \PhpOffice\PhpSpreadsheet\Style\Border * @param string $color color \PhpOffice\PhpSpreadsheet\Style\Color */ - public function setBorderStyle($thickness, $color) { + public function setBorderStyle($thickness, $color) + { $this->styleArray['borders'] = array( 'outline' => array( 'borderStyle' => $thickness, @@ -487,7 +489,8 @@ class ExportExcel2007new extends ModeleExports * @param bool $bold * @param string $color color \PhpOffice\PhpSpreadsheet\Style\Color */ - public function setFontStyle($bold, $color) { + public function setFontStyle($bold, $color) + { $this->styleArray['font'] = array( 'color' => array('argb' => $color), 'bold' => $bold @@ -499,14 +502,16 @@ class ExportExcel2007new extends ModeleExports * * @param string $horizontal PhpOffice\PhpSpreadsheet\Style\Alignment */ - public function setAlignmentStyle($horizontal) { + public function setAlignmentStyle($horizontal) + { $this->styleArray['alignment'] = array('horizontal' => $horizontal); } /** * Reset Style */ - public function resetStyle() { + public function resetStyle() + { $this->styleArray = array(); } @@ -518,19 +523,20 @@ class ExportExcel2007new extends ModeleExports * @param bool $boldTitle * @return int 1 if OK, -1 if KO */ - public function setBlock($startCell, $TDatas = array(), $boldTitle = false) { + public function setBlock($startCell, $TDatas = array(), $boldTitle = false) + { try { - if(! empty($TDatas)) { + if (! empty($TDatas)) { $startCell = $this->workbook->getActiveSheet()->getCell($startCell); $startColumn = Coordinate::columnIndexFromString($startCell->getColumn()); $startRow = $startCell->getRow(); - foreach($TDatas as $column => $TRows) { - if($boldTitle) $this->setFontStyle(true, $this->styleArray['font']['color']['argb']); + foreach ($TDatas as $column => $TRows) { + if ($boldTitle) $this->setFontStyle(true, $this->styleArray['font']['color']['argb']); $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow); $this->setCellValue($column, $cell->getCoordinate()); $rowPos = $startRow; - if($boldTitle) $this->setFontStyle(false, $this->styleArray['font']['color']['argb']); - foreach($TRows as $row) { + if ($boldTitle) $this->setFontStyle(false, $this->styleArray['font']['color']['argb']); + foreach ($TRows as $row) { $rowPos++; $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $rowPos); $this->setCellValue($row, $cell->getCoordinate()); @@ -539,7 +545,7 @@ class ExportExcel2007new extends ModeleExports } } } - catch(Exception $e) { + catch (Exception $e) { $this->error = $e->getMessage(); return -1; } @@ -554,24 +560,25 @@ class ExportExcel2007new extends ModeleExports * @param bool $boldTitle * @return int 1 if OK, -1 if KO */ - public function setBlock2Columns($startCell, $TDatas = array(), $boldTitle = false) { + public function setBlock2Columns($startCell, $TDatas = array(), $boldTitle = false) + { try { - if(! empty($TDatas)) { + if (! empty($TDatas)) { $startCell = $this->workbook->getActiveSheet()->getCell($startCell); $startColumn = Coordinate::columnIndexFromString($startCell->getColumn()); $startRow = $startCell->getRow(); - foreach($TDatas as $title => $val) { + foreach ($TDatas as $title => $val) { $cell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow); - if($boldTitle) $this->setFontStyle(true, $this->styleArray['font']['color']['argb']); + if ($boldTitle) $this->setFontStyle(true, $this->styleArray['font']['color']['argb']); $this->setCellValue($title, $cell->getCoordinate()); - if($boldTitle) $this->setFontStyle(false, $this->styleArray['font']['color']['argb']); + if ($boldTitle) $this->setFontStyle(false, $this->styleArray['font']['color']['argb']); $cell2 = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn + 1, $startRow); $this->setCellValue($val, $cell2->getCoordinate()); $startRow++; } } } - catch(Exception $e) { + catch (Exception $e) { $this->error = $e->getMessage(); return -1; } @@ -584,8 +591,9 @@ class ExportExcel2007new extends ModeleExports * @param string $firstColumn * @param string $lastColumn */ - public function enableAutosize($firstColumn, $lastColumn) { - foreach(range($firstColumn, $lastColumn) as $columnID) { + public function enableAutosize($firstColumn, $lastColumn) + { + foreach (range($firstColumn, $lastColumn) as $columnID) { $this->workbook->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true); } } @@ -599,11 +607,12 @@ class ExportExcel2007new extends ModeleExports * @param int $offset Starting offset * @return string Coordinate or -1 if KO */ - public function setMergeCellValueByLength($val, $startCell, $length, $offset = 0) { + public function setMergeCellValueByLength($val, $startCell, $length, $offset = 0) + { try { $startCell = $this->workbook->getActiveSheet()->getCell($startCell); $startColumn = Coordinate::columnIndexFromString($startCell->getColumn()); - if(! empty($offset)) $startColumn += $offset; + if (! empty($offset)) $startColumn += $offset; $startRow = $startCell->getRow(); $startCell = $this->workbook->getActiveSheet()->getCellByColumnAndRow($startColumn, $startRow); @@ -614,7 +623,7 @@ class ExportExcel2007new extends ModeleExports $endCoordinate = $endCell->getCoordinate(); $this->workbook->getActiveSheet()->mergeCells($startCoordinate.':'.$endCoordinate); } - catch(Exception $e) { + catch (Exception $e) { $this->error = $e->getMessage(); return -1; } From 7591ec6cf69838924685602323a2d66eb89c3dad Mon Sep 17 00:00:00 2001 From: quentin Date: Thu, 8 Oct 2020 10:49:29 +0200 Subject: [PATCH 4/4] FIX style --- .../export/export_excel2007new.modules.php | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/htdocs/core/modules/export/export_excel2007new.modules.php b/htdocs/core/modules/export/export_excel2007new.modules.php index 31b3fcfdd0b..92b576ed57e 100644 --- a/htdocs/core/modules/export/export_excel2007new.modules.php +++ b/htdocs/core/modules/export/export_excel2007new.modules.php @@ -444,8 +444,8 @@ class ExportExcel2007new extends ModeleExports * Set cell value and automatically merge if we give an endcell * * @param string $val cell value - * @param string $startCell - * @param string $endCell + * @param string $startCell starting cell + * @param string $endCell ending cell * @return int 1 if success -1 if failed */ public function setCellValue($val, $startCell, $endCell = '') { @@ -471,6 +471,7 @@ class ExportExcel2007new extends ModeleExports * * @param string $thickness style \PhpOffice\PhpSpreadsheet\Style\Border * @param string $color color \PhpOffice\PhpSpreadsheet\Style\Color + * @return int 1 if ok */ public function setBorderStyle($thickness, $color) { $this->styleArray['borders'] = array( @@ -479,43 +480,50 @@ class ExportExcel2007new extends ModeleExports 'color' => array('argb' => $color) ) ); + return 1; } /** * Set font style * - * @param bool $bold + * @param bool $bold true if bold * @param string $color color \PhpOffice\PhpSpreadsheet\Style\Color + * @return int 1 */ public function setFontStyle($bold, $color) { $this->styleArray['font'] = array( 'color' => array('argb' => $color), 'bold' => $bold ); + return 1; } /** * Set alignment style (horizontal, left, right, ...) * * @param string $horizontal PhpOffice\PhpSpreadsheet\Style\Alignment + * @return int 1 */ public function setAlignmentStyle($horizontal) { $this->styleArray['alignment'] = array('horizontal' => $horizontal); + return 1; } /** * Reset Style + * @return int 1 */ public function resetStyle() { $this->styleArray = array(); + return 1; } /** * Make a NxN Block in sheet * - * @param string $startCell + * @param string $startCell starting cell * @param array $TDatas array(ColumnName=>array(Row value 1, row value 2, etc ...)) - * @param bool $boldTitle + * @param bool $boldTitle true if bold headers * @return int 1 if OK, -1 if KO */ public function setBlock($startCell, $TDatas = array(), $boldTitle = false) { @@ -551,7 +559,7 @@ class ExportExcel2007new extends ModeleExports * * @param string $startCell A1 * @param array $TDatas array(Title=>val) - * @param bool $boldTitle + * @param bool $boldTitle true if bold titles * @return int 1 if OK, -1 if KO */ public function setBlock2Columns($startCell, $TDatas = array(), $boldTitle = false) { @@ -581,13 +589,15 @@ class ExportExcel2007new extends ModeleExports /** * Enable auto sizing for column range * - * @param string $firstColumn - * @param string $lastColumn + * @param string $firstColumn first column to autosize + * @param string $lastColumn to last column to autosize + * @return int 1 */ public function enableAutosize($firstColumn, $lastColumn) { foreach(range($firstColumn, $lastColumn) as $columnID) { $this->workbook->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true); } + return 1; } /**