From bf97d2a50fcab0d3b29d0c6a683919aa552ebe65 Mon Sep 17 00:00:00 2001 From: quentin Date: Thu, 8 Oct 2020 10:26:30 +0200 Subject: [PATCH] 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; + } }