<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx;
> use PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
> use PhpOffice\PhpSpreadsheet\Calculation\Information\Value;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
> use PhpOffice\PhpSpreadsheet\Settings;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
> use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalDataBar;
use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column;
> use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalFormattingRuleExtension;
use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule;
use PhpOffice\PhpSpreadsheet\Worksheet\SheetView;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet as PhpspreadsheetWorksheet;
< use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
< /**
< * @category PhpSpreadsheet
< *
< * @copyright Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
< */
class Worksheet extends WriterPart
{
/**
* Write worksheet to XML format.
*
< * @param PhpspreadsheetWorksheet $pSheet
< * @param string[] $pStringTable
> * @param string[] $stringTable
* @param bool $includeCharts Flag indicating if we should write charts
*
< * @throws WriterException
< *
* @return string XML Output
*/
< public function writeWorksheet(PhpspreadsheetWorksheet $pSheet, $pStringTable = null, $includeCharts = false)
> public function writeWorksheet(PhpspreadsheetWorksheet $worksheet, $stringTable = [], $includeCharts = false)
{
// Create XML writer
$objWriter = null;
if ($this->getParentWriter()->getUseDiskCaching()) {
$objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
} else {
$objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
}
// XML header
$objWriter->startDocument('1.0', 'UTF-8', 'yes');
// Worksheet
$objWriter->startElement('worksheet');
$objWriter->writeAttribute('xml:space', 'preserve');
$objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
$objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
$objWriter->writeAttribute('xmlns:xdr', 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing');
$objWriter->writeAttribute('xmlns:x14', 'http://schemas.microsoft.com/office/spreadsheetml/2009/9/main');
> $objWriter->writeAttribute('xmlns:xm', 'http://schemas.microsoft.com/office/excel/2006/main');
$objWriter->writeAttribute('xmlns:mc', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
$objWriter->writeAttribute('mc:Ignorable', 'x14ac');
$objWriter->writeAttribute('xmlns:x14ac', 'http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac');
// sheetPr
< $this->writeSheetPr($objWriter, $pSheet);
> $this->writeSheetPr($objWriter, $worksheet);
// Dimension
< $this->writeDimension($objWriter, $pSheet);
> $this->writeDimension($objWriter, $worksheet);
// sheetViews
< $this->writeSheetViews($objWriter, $pSheet);
> $this->writeSheetViews($objWriter, $worksheet);
// sheetFormatPr
< $this->writeSheetFormatPr($objWriter, $pSheet);
> $this->writeSheetFormatPr($objWriter, $worksheet);
// cols
< $this->writeCols($objWriter, $pSheet);
> $this->writeCols($objWriter, $worksheet);
// sheetData
< $this->writeSheetData($objWriter, $pSheet, $pStringTable);
> $this->writeSheetData($objWriter, $worksheet, $stringTable);
// sheetProtection
< $this->writeSheetProtection($objWriter, $pSheet);
> $this->writeSheetProtection($objWriter, $worksheet);
// protectedRanges
< $this->writeProtectedRanges($objWriter, $pSheet);
> $this->writeProtectedRanges($objWriter, $worksheet);
// autoFilter
< $this->writeAutoFilter($objWriter, $pSheet);
> $this->writeAutoFilter($objWriter, $worksheet);
// mergeCells
< $this->writeMergeCells($objWriter, $pSheet);
> $this->writeMergeCells($objWriter, $worksheet);
// conditionalFormatting
< $this->writeConditionalFormatting($objWriter, $pSheet);
> $this->writeConditionalFormatting($objWriter, $worksheet);
// dataValidations
< $this->writeDataValidations($objWriter, $pSheet);
> $this->writeDataValidations($objWriter, $worksheet);
// hyperlinks
< $this->writeHyperlinks($objWriter, $pSheet);
> $this->writeHyperlinks($objWriter, $worksheet);
// Print options
< $this->writePrintOptions($objWriter, $pSheet);
> $this->writePrintOptions($objWriter, $worksheet);
// Page margins
< $this->writePageMargins($objWriter, $pSheet);
> $this->writePageMargins($objWriter, $worksheet);
// Page setup
< $this->writePageSetup($objWriter, $pSheet);
> $this->writePageSetup($objWriter, $worksheet);
// Header / footer
< $this->writeHeaderFooter($objWriter, $pSheet);
> $this->writeHeaderFooter($objWriter, $worksheet);
// Breaks
< $this->writeBreaks($objWriter, $pSheet);
> $this->writeBreaks($objWriter, $worksheet);
// Drawings and/or Charts
< $this->writeDrawings($objWriter, $pSheet, $includeCharts);
> $this->writeDrawings($objWriter, $worksheet, $includeCharts);
// LegacyDrawing
< $this->writeLegacyDrawing($objWriter, $pSheet);
> $this->writeLegacyDrawing($objWriter, $worksheet);
// LegacyDrawingHF
< $this->writeLegacyDrawingHF($objWriter, $pSheet);
> $this->writeLegacyDrawingHF($objWriter, $worksheet);
// AlternateContent
< $this->writeAlternateContent($objWriter, $pSheet);
> $this->writeAlternateContent($objWriter, $worksheet);
>
> // Table
> $this->writeTable($objWriter, $worksheet);
>
> // ConditionalFormattingRuleExtensionList
> // (Must be inserted last. Not insert last, an Excel parse error will occur)
> $this->writeExtLst($objWriter, $worksheet);
$objWriter->endElement();
// Return
return $objWriter->getData();
}
/**
* Write SheetPr.
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
*/
< private function writeSheetPr(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeSheetPr(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
{
// sheetPr
$objWriter->startElement('sheetPr');
< if ($pSheet->getParent()->hasMacros()) {
> if ($worksheet->getParent()->hasMacros()) {
//if the workbook have macros, we need to have codeName for the sheet
< if (!$pSheet->hasCodeName()) {
< $pSheet->setCodeName($pSheet->getTitle());
> if (!$worksheet->hasCodeName()) {
> $worksheet->setCodeName($worksheet->getTitle());
}
< $objWriter->writeAttribute('codeName', $pSheet->getCodeName());
> self::writeAttributeNotNull($objWriter, 'codeName', $worksheet->getCodeName());
}
< $autoFilterRange = $pSheet->getAutoFilter()->getRange();
> $autoFilterRange = $worksheet->getAutoFilter()->getRange();
if (!empty($autoFilterRange)) {
< $objWriter->writeAttribute('filterMode', 1);
< $pSheet->getAutoFilter()->showHideRows();
> $objWriter->writeAttribute('filterMode', '1');
> if (!$worksheet->getAutoFilter()->getEvaluated()) {
> $worksheet->getAutoFilter()->showHideRows();
> }
}
// tabColor
< if ($pSheet->isTabColorSet()) {
> if ($worksheet->isTabColorSet()) {
$objWriter->startElement('tabColor');
< $objWriter->writeAttribute('rgb', $pSheet->getTabColor()->getARGB());
> $objWriter->writeAttribute('rgb', $worksheet->getTabColor()->getARGB() ?? '');
$objWriter->endElement();
}
// outlinePr
$objWriter->startElement('outlinePr');
< $objWriter->writeAttribute('summaryBelow', ($pSheet->getShowSummaryBelow() ? '1' : '0'));
< $objWriter->writeAttribute('summaryRight', ($pSheet->getShowSummaryRight() ? '1' : '0'));
> $objWriter->writeAttribute('summaryBelow', ($worksheet->getShowSummaryBelow() ? '1' : '0'));
> $objWriter->writeAttribute('summaryRight', ($worksheet->getShowSummaryRight() ? '1' : '0'));
$objWriter->endElement();
// pageSetUpPr
< if ($pSheet->getPageSetup()->getFitToPage()) {
> if ($worksheet->getPageSetup()->getFitToPage()) {
$objWriter->startElement('pageSetUpPr');
$objWriter->writeAttribute('fitToPage', '1');
$objWriter->endElement();
}
$objWriter->endElement();
}
/**
* Write Dimension.
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
*/
< private function writeDimension(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeDimension(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
{
// dimension
$objWriter->startElement('dimension');
< $objWriter->writeAttribute('ref', $pSheet->calculateWorksheetDimension());
> $objWriter->writeAttribute('ref', $worksheet->calculateWorksheetDimension());
$objWriter->endElement();
}
/**
* Write SheetViews.
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< *
< * @throws WriterException
*/
< private function writeSheetViews(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeSheetViews(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
{
// sheetViews
$objWriter->startElement('sheetViews');
// Sheet selected?
$sheetSelected = false;
< if ($this->getParentWriter()->getSpreadsheet()->getIndex($pSheet) == $this->getParentWriter()->getSpreadsheet()->getActiveSheetIndex()) {
> if ($this->getParentWriter()->getSpreadsheet()->getIndex($worksheet) == $this->getParentWriter()->getSpreadsheet()->getActiveSheetIndex()) {
$sheetSelected = true;
}
// sheetView
$objWriter->startElement('sheetView');
$objWriter->writeAttribute('tabSelected', $sheetSelected ? '1' : '0');
$objWriter->writeAttribute('workbookViewId', '0');
// Zoom scales
< if ($pSheet->getSheetView()->getZoomScale() != 100) {
< $objWriter->writeAttribute('zoomScale', $pSheet->getSheetView()->getZoomScale());
> if ($worksheet->getSheetView()->getZoomScale() != 100) {
> $objWriter->writeAttribute('zoomScale', (string) $worksheet->getSheetView()->getZoomScale());
}
< if ($pSheet->getSheetView()->getZoomScaleNormal() != 100) {
< $objWriter->writeAttribute('zoomScaleNormal', $pSheet->getSheetView()->getZoomScaleNormal());
> if ($worksheet->getSheetView()->getZoomScaleNormal() != 100) {
> $objWriter->writeAttribute('zoomScaleNormal', (string) $worksheet->getSheetView()->getZoomScaleNormal());
}
// Show zeros (Excel also writes this attribute only if set to false)
< if ($pSheet->getSheetView()->getShowZeros() === false) {
< $objWriter->writeAttribute('showZeros', 0);
> if ($worksheet->getSheetView()->getShowZeros() === false) {
> $objWriter->writeAttribute('showZeros', '0');
}
// View Layout Type
< if ($pSheet->getSheetView()->getView() !== SheetView::SHEETVIEW_NORMAL) {
< $objWriter->writeAttribute('view', $pSheet->getSheetView()->getView());
> if ($worksheet->getSheetView()->getView() !== SheetView::SHEETVIEW_NORMAL) {
> $objWriter->writeAttribute('view', $worksheet->getSheetView()->getView());
}
// Gridlines
< if ($pSheet->getShowGridlines()) {
> if ($worksheet->getShowGridlines()) {
$objWriter->writeAttribute('showGridLines', 'true');
} else {
$objWriter->writeAttribute('showGridLines', 'false');
}
// Row and column headers
< if ($pSheet->getShowRowColHeaders()) {
> if ($worksheet->getShowRowColHeaders()) {
$objWriter->writeAttribute('showRowColHeaders', '1');
} else {
$objWriter->writeAttribute('showRowColHeaders', '0');
}
// Right-to-left
< if ($pSheet->getRightToLeft()) {
> if ($worksheet->getRightToLeft()) {
$objWriter->writeAttribute('rightToLeft', 'true');
}
< $activeCell = $pSheet->getActiveCell();
< $sqref = $pSheet->getSelectedCells();
> $topLeftCell = $worksheet->getTopLeftCell();
> $activeCell = $worksheet->getActiveCell();
> $sqref = $worksheet->getSelectedCells();
// Pane
$pane = '';
< if ($pSheet->getFreezePane()) {
< [$xSplit, $ySplit] = Coordinate::coordinateFromString($pSheet->getFreezePane());
> if ($worksheet->getFreezePane()) {
> [$xSplit, $ySplit] = Coordinate::coordinateFromString($worksheet->getFreezePane());
$xSplit = Coordinate::columnIndexFromString($xSplit);
--$xSplit;
--$ySplit;
< $topLeftCell = $pSheet->getTopLeftCell();
< $activeCell = $topLeftCell;
< $sqref = $topLeftCell;
<
// pane
$pane = 'topRight';
$objWriter->startElement('pane');
if ($xSplit > 0) {
< $objWriter->writeAttribute('xSplit', $xSplit);
> $objWriter->writeAttribute('xSplit', "$xSplit");
}
if ($ySplit > 0) {
$objWriter->writeAttribute('ySplit', $ySplit);
$pane = ($xSplit > 0) ? 'bottomRight' : 'bottomLeft';
}
< $objWriter->writeAttribute('topLeftCell', $topLeftCell);
> self::writeAttributeNotNull($objWriter, 'topLeftCell', $topLeftCell);
$objWriter->writeAttribute('activePane', $pane);
$objWriter->writeAttribute('state', 'frozen');
$objWriter->endElement();
if (($xSplit > 0) && ($ySplit > 0)) {
// Write additional selections if more than two panes (ie both an X and a Y split)
$objWriter->startElement('selection');
$objWriter->writeAttribute('pane', 'topRight');
$objWriter->endElement();
$objWriter->startElement('selection');
$objWriter->writeAttribute('pane', 'bottomLeft');
$objWriter->endElement();
}
> } else {
}
> self::writeAttributeNotNull($objWriter, 'topLeftCell', $topLeftCell);
// Selection
// Only need to write selection element if we have a split pane
// We cheat a little by over-riding the active cell selection, setting it to the split cell
$objWriter->startElement('selection');
if ($pane != '') {
$objWriter->writeAttribute('pane', $pane);
}
$objWriter->writeAttribute('activeCell', $activeCell);
$objWriter->writeAttribute('sqref', $sqref);
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
}
/**
* Write SheetFormatPr.
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
*/
< private function writeSheetFormatPr(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeSheetFormatPr(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
{
// sheetFormatPr
$objWriter->startElement('sheetFormatPr');
// Default row height
< if ($pSheet->getDefaultRowDimension()->getRowHeight() >= 0) {
> if ($worksheet->getDefaultRowDimension()->getRowHeight() >= 0) {
$objWriter->writeAttribute('customHeight', 'true');
< $objWriter->writeAttribute('defaultRowHeight', StringHelper::formatNumber($pSheet->getDefaultRowDimension()->getRowHeight()));
> $objWriter->writeAttribute('defaultRowHeight', StringHelper::formatNumber($worksheet->getDefaultRowDimension()->getRowHeight()));
} else {
$objWriter->writeAttribute('defaultRowHeight', '14.4');
}
// Set Zero Height row
< if ((string) $pSheet->getDefaultRowDimension()->getZeroHeight() === '1' ||
< strtolower((string) $pSheet->getDefaultRowDimension()->getZeroHeight()) == 'true') {
> if ($worksheet->getDefaultRowDimension()->getZeroHeight()) {
$objWriter->writeAttribute('zeroHeight', '1');
}
// Default column width
< if ($pSheet->getDefaultColumnDimension()->getWidth() >= 0) {
< $objWriter->writeAttribute('defaultColWidth', StringHelper::formatNumber($pSheet->getDefaultColumnDimension()->getWidth()));
> if ($worksheet->getDefaultColumnDimension()->getWidth() >= 0) {
> $objWriter->writeAttribute('defaultColWidth', StringHelper::formatNumber($worksheet->getDefaultColumnDimension()->getWidth()));
}
// Outline level - row
$outlineLevelRow = 0;
< foreach ($pSheet->getRowDimensions() as $dimension) {
> foreach ($worksheet->getRowDimensions() as $dimension) {
if ($dimension->getOutlineLevel() > $outlineLevelRow) {
$outlineLevelRow = $dimension->getOutlineLevel();
}
}
< $objWriter->writeAttribute('outlineLevelRow', (int) $outlineLevelRow);
> $objWriter->writeAttribute('outlineLevelRow', (string) (int) $outlineLevelRow);
// Outline level - column
$outlineLevelCol = 0;
< foreach ($pSheet->getColumnDimensions() as $dimension) {
> foreach ($worksheet->getColumnDimensions() as $dimension) {
if ($dimension->getOutlineLevel() > $outlineLevelCol) {
$outlineLevelCol = $dimension->getOutlineLevel();
}
}
< $objWriter->writeAttribute('outlineLevelCol', (int) $outlineLevelCol);
> $objWriter->writeAttribute('outlineLevelCol', (string) (int) $outlineLevelCol);
$objWriter->endElement();
}
/**
* Write Cols.
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
*/
< private function writeCols(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeCols(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
{
// cols
< if (count($pSheet->getColumnDimensions()) > 0) {
> if (count($worksheet->getColumnDimensions()) > 0) {
$objWriter->startElement('cols');
< $pSheet->calculateColumnWidths();
> $worksheet->calculateColumnWidths();
// Loop through column dimensions
< foreach ($pSheet->getColumnDimensions() as $colDimension) {
> foreach ($worksheet->getColumnDimensions() as $colDimension) {
// col
$objWriter->startElement('col');
< $objWriter->writeAttribute('min', Coordinate::columnIndexFromString($colDimension->getColumnIndex()));
< $objWriter->writeAttribute('max', Coordinate::columnIndexFromString($colDimension->getColumnIndex()));
> $objWriter->writeAttribute('min', (string) Coordinate::columnIndexFromString($colDimension->getColumnIndex()));
> $objWriter->writeAttribute('max', (string) Coordinate::columnIndexFromString($colDimension->getColumnIndex()));
if ($colDimension->getWidth() < 0) {
// No width set, apply default of 10
$objWriter->writeAttribute('width', '9.10');
} else {
// Width set
$objWriter->writeAttribute('width', StringHelper::formatNumber($colDimension->getWidth()));
}
// Column visibility
if ($colDimension->getVisible() === false) {
$objWriter->writeAttribute('hidden', 'true');
}
// Auto size?
if ($colDimension->getAutoSize()) {
$objWriter->writeAttribute('bestFit', 'true');
}
// Custom width?
< if ($colDimension->getWidth() != $pSheet->getDefaultColumnDimension()->getWidth()) {
> if ($colDimension->getWidth() != $worksheet->getDefaultColumnDimension()->getWidth()) {
$objWriter->writeAttribute('customWidth', 'true');
}
// Collapsed
if ($colDimension->getCollapsed() === true) {
$objWriter->writeAttribute('collapsed', 'true');
}
// Outline level
if ($colDimension->getOutlineLevel() > 0) {
< $objWriter->writeAttribute('outlineLevel', $colDimension->getOutlineLevel());
> $objWriter->writeAttribute('outlineLevel', (string) $colDimension->getOutlineLevel());
}
// Style
< $objWriter->writeAttribute('style', $colDimension->getXfIndex());
> $objWriter->writeAttribute('style', (string) $colDimension->getXfIndex());
$objWriter->endElement();
}
$objWriter->endElement();
}
}
/**
* Write SheetProtection.
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
*/
< private function writeSheetProtection(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeSheetProtection(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
{
// sheetProtection
$objWriter->startElement('sheetProtection');
< if ($pSheet->getProtection()->getPassword() !== '') {
< $objWriter->writeAttribute('password', $pSheet->getProtection()->getPassword());
> $protection = $worksheet->getProtection();
>
> if ($protection->getAlgorithm()) {
> $objWriter->writeAttribute('algorithmName', $protection->getAlgorithm());
> $objWriter->writeAttribute('hashValue', $protection->getPassword());
> $objWriter->writeAttribute('saltValue', $protection->getSalt());
> $objWriter->writeAttribute('spinCount', (string) $protection->getSpinCount());
> } elseif ($protection->getPassword() !== '') {
> $objWriter->writeAttribute('password', $protection->getPassword());
}
< $objWriter->writeAttribute('sheet', ($pSheet->getProtection()->getSheet() ? 'true' : 'false'));
< $objWriter->writeAttribute('objects', ($pSheet->getProtection()->getObjects() ? 'true' : 'false'));
< $objWriter->writeAttribute('scenarios', ($pSheet->getProtection()->getScenarios() ? 'true' : 'false'));
< $objWriter->writeAttribute('formatCells', ($pSheet->getProtection()->getFormatCells() ? 'true' : 'false'));
< $objWriter->writeAttribute('formatColumns', ($pSheet->getProtection()->getFormatColumns() ? 'true' : 'false'));
< $objWriter->writeAttribute('formatRows', ($pSheet->getProtection()->getFormatRows() ? 'true' : 'false'));
< $objWriter->writeAttribute('insertColumns', ($pSheet->getProtection()->getInsertColumns() ? 'true' : 'false'));
< $objWriter->writeAttribute('insertRows', ($pSheet->getProtection()->getInsertRows() ? 'true' : 'false'));
< $objWriter->writeAttribute('insertHyperlinks', ($pSheet->getProtection()->getInsertHyperlinks() ? 'true' : 'false'));
< $objWriter->writeAttribute('deleteColumns', ($pSheet->getProtection()->getDeleteColumns() ? 'true' : 'false'));
< $objWriter->writeAttribute('deleteRows', ($pSheet->getProtection()->getDeleteRows() ? 'true' : 'false'));
< $objWriter->writeAttribute('selectLockedCells', ($pSheet->getProtection()->getSelectLockedCells() ? 'true' : 'false'));
< $objWriter->writeAttribute('sort', ($pSheet->getProtection()->getSort() ? 'true' : 'false'));
< $objWriter->writeAttribute('autoFilter', ($pSheet->getProtection()->getAutoFilter() ? 'true' : 'false'));
< $objWriter->writeAttribute('pivotTables', ($pSheet->getProtection()->getPivotTables() ? 'true' : 'false'));
< $objWriter->writeAttribute('selectUnlockedCells', ($pSheet->getProtection()->getSelectUnlockedCells() ? 'true' : 'false'));
> $objWriter->writeAttribute('sheet', ($protection->getSheet() ? 'true' : 'false'));
> $objWriter->writeAttribute('objects', ($protection->getObjects() ? 'true' : 'false'));
> $objWriter->writeAttribute('scenarios', ($protection->getScenarios() ? 'true' : 'false'));
> $objWriter->writeAttribute('formatCells', ($protection->getFormatCells() ? 'true' : 'false'));
> $objWriter->writeAttribute('formatColumns', ($protection->getFormatColumns() ? 'true' : 'false'));
> $objWriter->writeAttribute('formatRows', ($protection->getFormatRows() ? 'true' : 'false'));
> $objWriter->writeAttribute('insertColumns', ($protection->getInsertColumns() ? 'true' : 'false'));
> $objWriter->writeAttribute('insertRows', ($protection->getInsertRows() ? 'true' : 'false'));
> $objWriter->writeAttribute('insertHyperlinks', ($protection->getInsertHyperlinks() ? 'true' : 'false'));
> $objWriter->writeAttribute('deleteColumns', ($protection->getDeleteColumns() ? 'true' : 'false'));
> $objWriter->writeAttribute('deleteRows', ($protection->getDeleteRows() ? 'true' : 'false'));
> $objWriter->writeAttribute('selectLockedCells', ($protection->getSelectLockedCells() ? 'true' : 'false'));
> $objWriter->writeAttribute('sort', ($protection->getSort() ? 'true' : 'false'));
> $objWriter->writeAttribute('autoFilter', ($protection->getAutoFilter() ? 'true' : 'false'));
> $objWriter->writeAttribute('pivotTables', ($protection->getPivotTables() ? 'true' : 'false'));
> $objWriter->writeAttribute('selectUnlockedCells', ($protection->getSelectUnlockedCells() ? 'true' : 'false'));
$objWriter->endElement();
}
> private static function writeAttributeIf(XMLWriter $objWriter, ?bool $condition, string $attr, string $val): void
/**
> {
* Write ConditionalFormatting.
> if ($condition) {
*
> $objWriter->writeAttribute($attr, $val);
* @param XMLWriter $objWriter XML Writer
> }
* @param PhpspreadsheetWorksheet $pSheet Worksheet
> }
*
>
* @throws WriterException
> private static function writeAttributeNotNull(XMLWriter $objWriter, string $attr, ?string $val): void
*/
> {
private function writeConditionalFormatting(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> if ($val !== null) {
{
> $objWriter->writeAttribute($attr, $val);
// Conditional id
> }
$id = 1;
> }
>
// Loop through styles in the current worksheet
> private static function writeElementIf(XMLWriter $objWriter, bool $condition, string $attr, string $val): void
foreach ($pSheet->getConditionalStylesCollection() as $cellCoordinate => $conditionalStyles) {
> {
foreach ($conditionalStyles as $conditional) {
> if ($condition) {
// WHY was this again?
> $objWriter->writeElement($attr, $val);
// if ($this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode($conditional->getHashCode()) == '') {
> }
// continue;
> }
// }
>
if ($conditional->getConditionType() != Conditional::CONDITION_NONE) {
> private static function writeOtherCondElements(XMLWriter $objWriter, Conditional $conditional, string $cellCoordinate): void
// conditionalFormatting
> {
$objWriter->startElement('conditionalFormatting');
> $conditions = $conditional->getConditions();
$objWriter->writeAttribute('sqref', $cellCoordinate);
> if (
> $conditional->getConditionType() == Conditional::CONDITION_CELLIS
// cfRule
> || $conditional->getConditionType() == Conditional::CONDITION_EXPRESSION
$objWriter->startElement('cfRule');
> || !empty($conditions)
$objWriter->writeAttribute('type', $conditional->getConditionType());
> ) {
$objWriter->writeAttribute('dxfId', $this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode($conditional->getHashCode()));
> foreach ($conditions as $formula) {
$objWriter->writeAttribute('priority', $id++);
> // Formula
> if (is_bool($formula)) {
if (($conditional->getConditionType() == Conditional::CONDITION_CELLIS || $conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT)
> $formula = $formula ? 'TRUE' : 'FALSE';
&& $conditional->getOperatorType() != Conditional::OPERATOR_NONE) {
> }
$objWriter->writeAttribute('operator', $conditional->getOperatorType());
> $objWriter->writeElement('formula', Xlfn::addXlfn("$formula"));
}
> }
> } else {
if ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT
> if ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSBLANKS) {
&& $conditional->getText() !== null) {
> // formula copied from ms xlsx xml source file
$objWriter->writeAttribute('text', $conditional->getText());
> $objWriter->writeElement('formula', 'LEN(TRIM(' . $cellCoordinate . '))=0');
}
> } elseif ($conditional->getConditionType() == Conditional::CONDITION_NOTCONTAINSBLANKS) {
> // formula copied from ms xlsx xml source file
if ($conditional->getStopIfTrue()) {
> $objWriter->writeElement('formula', 'LEN(TRIM(' . $cellCoordinate . '))>0');
$objWriter->writeAttribute('stopIfTrue', '1');
> } elseif ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSERRORS) {
}
> // formula copied from ms xlsx xml source file
> $objWriter->writeElement('formula', 'ISERROR(' . $cellCoordinate . ')');
if ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT
> } elseif ($conditional->getConditionType() == Conditional::CONDITION_NOTCONTAINSERRORS) {
&& $conditional->getOperatorType() == Conditional::OPERATOR_CONTAINSTEXT
> // formula copied from ms xlsx xml source file
&& $conditional->getText() !== null) {
> $objWriter->writeElement('formula', 'NOT(ISERROR(' . $cellCoordinate . '))');
$objWriter->writeElement('formula', 'NOT(ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . ')))');
> }
} elseif ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT
> }
&& $conditional->getOperatorType() == Conditional::OPERATOR_BEGINSWITH
> }
&& $conditional->getText() !== null) {
>
$objWriter->writeElement('formula', 'LEFT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
> private static function writeTimePeriodCondElements(XMLWriter $objWriter, Conditional $conditional, string $cellCoordinate): void
} elseif ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT
> {
&& $conditional->getOperatorType() == Conditional::OPERATOR_ENDSWITH
> $txt = $conditional->getText();
&& $conditional->getText() !== null) {
> if (!empty($txt)) {
$objWriter->writeElement('formula', 'RIGHT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
> $objWriter->writeAttribute('timePeriod', $txt);
} elseif ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT
> if (empty($conditional->getConditions())) {
&& $conditional->getOperatorType() == Conditional::OPERATOR_NOTCONTAINS
> if ($conditional->getOperatorType() == Conditional::TIMEPERIOD_TODAY) {
&& $conditional->getText() !== null) {
> $objWriter->writeElement('formula', 'FLOOR(' . $cellCoordinate . ')=TODAY()');
$objWriter->writeElement('formula', 'ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . '))');
> } elseif ($conditional->getOperatorType() == Conditional::TIMEPERIOD_TOMORROW) {
} elseif ($conditional->getConditionType() == Conditional::CONDITION_CELLIS
> $objWriter->writeElement('formula', 'FLOOR(' . $cellCoordinate . ')=TODAY()+1');
|| $conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT
> } elseif ($conditional->getOperatorType() == Conditional::TIMEPERIOD_YESTERDAY) {
|| $conditional->getConditionType() == Conditional::CONDITION_EXPRESSION) {
> $objWriter->writeElement('formula', 'FLOOR(' . $cellCoordinate . ')=TODAY()-1');
foreach ($conditional->getConditions() as $formula) {
> } elseif ($conditional->getOperatorType() == Conditional::TIMEPERIOD_LAST_7_DAYS) {
// Formula
> $objWriter->writeElement('formula', 'AND(TODAY()-FLOOR(' . $cellCoordinate . ',1)<=6,FLOOR(' . $cellCoordinate . ',1)<=TODAY())');
$objWriter->writeElement('formula', $formula);
> } elseif ($conditional->getOperatorType() == Conditional::TIMEPERIOD_LAST_WEEK) {
}
> $objWriter->writeElement('formula', 'AND(TODAY()-ROUNDDOWN(' . $cellCoordinate . ',0)>=(WEEKDAY(TODAY())),TODAY()-ROUNDDOWN(' . $cellCoordinate . ',0)<(WEEKDAY(TODAY())+7))');
} elseif ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSBLANKS) {
> } elseif ($conditional->getOperatorType() == Conditional::TIMEPERIOD_THIS_WEEK) {
// formula copied from ms xlsx xml source file
> $objWriter->writeElement('formula', 'AND(TODAY()-ROUNDDOWN(' . $cellCoordinate . ',0)<=WEEKDAY(TODAY())-1,ROUNDDOWN(' . $cellCoordinate . ',0)-TODAY()<=7-WEEKDAY(TODAY()))');
$objWriter->writeElement('formula', 'LEN(TRIM(' . $cellCoordinate . '))=0');
> } elseif ($conditional->getOperatorType() == Conditional::TIMEPERIOD_NEXT_WEEK) {
}
> $objWriter->writeElement('formula', 'AND(ROUNDDOWN(' . $cellCoordinate . ',0)-TODAY()>(7-WEEKDAY(TODAY())),ROUNDDOWN(' . $cellCoordinate . ',0)-TODAY()<(15-WEEKDAY(TODAY())))');
> } elseif ($conditional->getOperatorType() == Conditional::TIMEPERIOD_LAST_MONTH) {
$objWriter->endElement();
> $objWriter->writeElement('formula', 'AND(MONTH(' . $cellCoordinate . ')=MONTH(EDATE(TODAY(),0-1)),YEAR(' . $cellCoordinate . ')=YEAR(EDATE(TODAY(),0-1)))');
> } elseif ($conditional->getOperatorType() == Conditional::TIMEPERIOD_THIS_MONTH) {
$objWriter->endElement();
> $objWriter->writeElement('formula', 'AND(MONTH(' . $cellCoordinate . ')=MONTH(TODAY()),YEAR(' . $cellCoordinate . ')=YEAR(TODAY()))');
}
> } elseif ($conditional->getOperatorType() == Conditional::TIMEPERIOD_NEXT_MONTH) {
}
> $objWriter->writeElement('formula', 'AND(MONTH(' . $cellCoordinate . ')=MONTH(EDATE(TODAY(),0+1)),YEAR(' . $cellCoordinate . ')=YEAR(EDATE(TODAY(),0+1)))');
}
> }
}
> } else {
> $objWriter->writeElement('formula', (string) ($conditional->getConditions()[0]));
/**
> }
* Write DataValidations.
> }
*
> }
* @param XMLWriter $objWriter XML Writer
>
* @param PhpspreadsheetWorksheet $pSheet Worksheet
> private static function writeTextCondElements(XMLWriter $objWriter, Conditional $conditional, string $cellCoordinate): void
*/
> {
private function writeDataValidations(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> $txt = $conditional->getText();
{
> if (!empty($txt)) {
// Datavalidation collection
> $objWriter->writeAttribute('text', $txt);
$dataValidationCollection = $pSheet->getDataValidationCollection();
> if (empty($conditional->getConditions())) {
> if ($conditional->getOperatorType() == Conditional::OPERATOR_CONTAINSTEXT) {
// Write data validations?
> $objWriter->writeElement('formula', 'NOT(ISERROR(SEARCH("' . $txt . '",' . $cellCoordinate . ')))');
if (!empty($dataValidationCollection)) {
> } elseif ($conditional->getOperatorType() == Conditional::OPERATOR_BEGINSWITH) {
$dataValidationCollection = Coordinate::mergeRangesInCollection($dataValidationCollection);
> $objWriter->writeElement('formula', 'LEFT(' . $cellCoordinate . ',LEN("' . $txt . '"))="' . $txt . '"');
$objWriter->startElement('dataValidations');
> } elseif ($conditional->getOperatorType() == Conditional::OPERATOR_ENDSWITH) {
$objWriter->writeAttribute('count', count($dataValidationCollection));
> $objWriter->writeElement('formula', 'RIGHT(' . $cellCoordinate . ',LEN("' . $txt . '"))="' . $txt . '"');
> } elseif ($conditional->getOperatorType() == Conditional::OPERATOR_NOTCONTAINS) {
foreach ($dataValidationCollection as $coordinate => $dv) {
> $objWriter->writeElement('formula', 'ISERROR(SEARCH("' . $txt . '",' . $cellCoordinate . '))');
$objWriter->startElement('dataValidation');
> }
> } else {
if ($dv->getType() != '') {
> $objWriter->writeElement('formula', (string) ($conditional->getConditions()[0]));
$objWriter->writeAttribute('type', $dv->getType());
> }
}
> }
> }
if ($dv->getErrorStyle() != '') {
>
$objWriter->writeAttribute('errorStyle', $dv->getErrorStyle());
> private static function writeExtConditionalFormattingElements(XMLWriter $objWriter, ConditionalFormattingRuleExtension $ruleExtension): void
}
> {
> $prefix = 'x14';
if ($dv->getOperator() != '') {
> $objWriter->startElementNs($prefix, 'conditionalFormatting', null);
$objWriter->writeAttribute('operator', $dv->getOperator());
>
}
> $objWriter->startElementNs($prefix, 'cfRule', null);
> $objWriter->writeAttribute('type', $ruleExtension->getCfRule());
$objWriter->writeAttribute('allowBlank', ($dv->getAllowBlank() ? '1' : '0'));
> $objWriter->writeAttribute('id', $ruleExtension->getId());
$objWriter->writeAttribute('showDropDown', (!$dv->getShowDropDown() ? '1' : '0'));
> $objWriter->startElementNs($prefix, 'dataBar', null);
$objWriter->writeAttribute('showInputMessage', ($dv->getShowInputMessage() ? '1' : '0'));
> $dataBar = $ruleExtension->getDataBarExt();
$objWriter->writeAttribute('showErrorMessage', ($dv->getShowErrorMessage() ? '1' : '0'));
> foreach ($dataBar->getXmlAttributes() as $attrKey => $val) {
> $objWriter->writeAttribute($attrKey, $val);
if ($dv->getErrorTitle() !== '') {
> }
$objWriter->writeAttribute('errorTitle', $dv->getErrorTitle());
> $minCfvo = $dataBar->getMinimumConditionalFormatValueObject();
}
> if ($minCfvo !== null) {
if ($dv->getError() !== '') {
> $objWriter->startElementNs($prefix, 'cfvo', null);
$objWriter->writeAttribute('error', $dv->getError());
> $objWriter->writeAttribute('type', $minCfvo->getType());
}
> if ($minCfvo->getCellFormula()) {
if ($dv->getPromptTitle() !== '') {
> $objWriter->writeElement('xm:f', $minCfvo->getCellFormula());
$objWriter->writeAttribute('promptTitle', $dv->getPromptTitle());
> }
}
> $objWriter->endElement(); //end cfvo
if ($dv->getPrompt() !== '') {
> }
$objWriter->writeAttribute('prompt', $dv->getPrompt());
>
}
> $maxCfvo = $dataBar->getMaximumConditionalFormatValueObject();
> if ($maxCfvo !== null) {
$objWriter->writeAttribute('sqref', $coordinate);
> $objWriter->startElementNs($prefix, 'cfvo', null);
> $objWriter->writeAttribute('type', $maxCfvo->getType());
if ($dv->getFormula1() !== '') {
> if ($maxCfvo->getCellFormula()) {
$objWriter->writeElement('formula1', $dv->getFormula1());
> $objWriter->writeElement('xm:f', $maxCfvo->getCellFormula());
}
> }
if ($dv->getFormula2() !== '') {
> $objWriter->endElement(); //end cfvo
$objWriter->writeElement('formula2', $dv->getFormula2());
> }
}
>
> foreach ($dataBar->getXmlElements() as $elmKey => $elmAttr) {
$objWriter->endElement();
> $objWriter->startElementNs($prefix, $elmKey, null);
}
> foreach ($elmAttr as $attrKey => $attrVal) {
> $objWriter->writeAttribute($attrKey, $attrVal);
$objWriter->endElement();
> }
}
> $objWriter->endElement(); //end elmKey
}
> }
> $objWriter->endElement(); //end dataBar
/**
> $objWriter->endElement(); //end cfRule
* Write Hyperlinks.
> $objWriter->writeElement('xm:sqref', $ruleExtension->getSqref());
*
> $objWriter->endElement(); //end conditionalFormatting
* @param XMLWriter $objWriter XML Writer
> }
* @param PhpspreadsheetWorksheet $pSheet Worksheet
>
*/
> private static function writeDataBarElements(XMLWriter $objWriter, ?ConditionalDataBar $dataBar): void
private function writeHyperlinks(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> {
{
> if ($dataBar) {
// Hyperlink collection
> $objWriter->startElement('dataBar');
$hyperlinkCollection = $pSheet->getHyperlinkCollection();
> self::writeAttributeIf($objWriter, null !== $dataBar->getShowValue(), 'showValue', $dataBar->getShowValue() ? '1' : '0');
>
// Relation ID
> $minCfvo = $dataBar->getMinimumConditionalFormatValueObject();
$relationId = 1;
> if ($minCfvo) {
> $objWriter->startElement('cfvo');
// Write hyperlinks?
> self::writeAttributeIf($objWriter, $minCfvo->getType(), 'type', (string) $minCfvo->getType());
if (!empty($hyperlinkCollection)) {
> self::writeAttributeIf($objWriter, $minCfvo->getValue(), 'val', (string) $minCfvo->getValue());
$objWriter->startElement('hyperlinks');
> $objWriter->endElement();
> }
foreach ($hyperlinkCollection as $coordinate => $hyperlink) {
> $maxCfvo = $dataBar->getMaximumConditionalFormatValueObject();
$objWriter->startElement('hyperlink');
> if ($maxCfvo) {
> $objWriter->startElement('cfvo');
$objWriter->writeAttribute('ref', $coordinate);
> self::writeAttributeIf($objWriter, $maxCfvo->getType(), 'type', (string) $maxCfvo->getType());
if (!$hyperlink->isInternal()) {
> self::writeAttributeIf($objWriter, $maxCfvo->getValue(), 'val', (string) $maxCfvo->getValue());
$objWriter->writeAttribute('r:id', 'rId_hyperlink_' . $relationId);
> $objWriter->endElement();
++$relationId;
> }
} else {
> if ($dataBar->getColor()) {
$objWriter->writeAttribute('location', str_replace('sheet://', '', $hyperlink->getUrl()));
> $objWriter->startElement('color');
}
> $objWriter->writeAttribute('rgb', $dataBar->getColor());
> $objWriter->endElement();
if ($hyperlink->getTooltip() !== '') {
> }
$objWriter->writeAttribute('tooltip', $hyperlink->getTooltip());
> $objWriter->endElement(); // end dataBar
$objWriter->writeAttribute('display', $hyperlink->getTooltip());
>
}
> if ($dataBar->getConditionalFormattingRuleExt()) {
> $objWriter->startElement('extLst');
$objWriter->endElement();
> $extension = $dataBar->getConditionalFormattingRuleExt();
}
> $objWriter->startElement('ext');
> $objWriter->writeAttribute('uri', '{B025F937-C7B1-47D3-B67F-A62EFF666E3E}');
$objWriter->endElement();
> $objWriter->startElementNs('x14', 'id', null);
}
> $objWriter->text($extension->getId());
}
> $objWriter->endElement();
> $objWriter->endElement();
/**
> $objWriter->endElement(); //end extLst
* Write ProtectedRanges.
> }
*
> }
* @param XMLWriter $objWriter XML Writer
> }
* @param PhpspreadsheetWorksheet $pSheet Worksheet
>
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< *
< * @throws WriterException
< private function writeConditionalFormatting(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeConditionalFormatting(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
< foreach ($pSheet->getConditionalStylesCollection() as $cellCoordinate => $conditionalStyles) {
> foreach ($worksheet->getConditionalStylesCollection() as $cellCoordinate => $conditionalStyles) {
> $objWriter->startElement('conditionalFormatting');
> $objWriter->writeAttribute('sqref', $cellCoordinate);
>
< if ($conditional->getConditionType() != Conditional::CONDITION_NONE) {
< // conditionalFormatting
< $objWriter->startElement('conditionalFormatting');
< $objWriter->writeAttribute('sqref', $cellCoordinate);
<
< $objWriter->writeAttribute('dxfId', $this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode($conditional->getHashCode()));
< $objWriter->writeAttribute('priority', $id++);
<
< if (($conditional->getConditionType() == Conditional::CONDITION_CELLIS || $conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT)
< && $conditional->getOperatorType() != Conditional::OPERATOR_NONE) {
< $objWriter->writeAttribute('operator', $conditional->getOperatorType());
< }
<
< if ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT
< && $conditional->getText() !== null) {
< $objWriter->writeAttribute('text', $conditional->getText());
< }
<
< if ($conditional->getStopIfTrue()) {
< $objWriter->writeAttribute('stopIfTrue', '1');
< }
<
< if ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT
< && $conditional->getOperatorType() == Conditional::OPERATOR_CONTAINSTEXT
< && $conditional->getText() !== null) {
< $objWriter->writeElement('formula', 'NOT(ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . ')))');
< } elseif ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT
< && $conditional->getOperatorType() == Conditional::OPERATOR_BEGINSWITH
< && $conditional->getText() !== null) {
< $objWriter->writeElement('formula', 'LEFT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
< } elseif ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT
< && $conditional->getOperatorType() == Conditional::OPERATOR_ENDSWITH
< && $conditional->getText() !== null) {
< $objWriter->writeElement('formula', 'RIGHT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
< } elseif ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT
< && $conditional->getOperatorType() == Conditional::OPERATOR_NOTCONTAINS
< && $conditional->getText() !== null) {
< $objWriter->writeElement('formula', 'ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . '))');
< } elseif ($conditional->getConditionType() == Conditional::CONDITION_CELLIS
< || $conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT
< || $conditional->getConditionType() == Conditional::CONDITION_EXPRESSION) {
< foreach ($conditional->getConditions() as $formula) {
< // Formula
< $objWriter->writeElement('formula', $formula);
< }
< } elseif ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSBLANKS) {
< // formula copied from ms xlsx xml source file
< $objWriter->writeElement('formula', 'LEN(TRIM(' . $cellCoordinate . '))=0');
> self::writeAttributeIf(
> $objWriter,
> ($conditional->getConditionType() != Conditional::CONDITION_DATABAR),
> 'dxfId',
> (string) $this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode($conditional->getHashCode())
> );
> $objWriter->writeAttribute('priority', (string) $id++);
>
> self::writeAttributeif(
> $objWriter,
> (
> $conditional->getConditionType() === Conditional::CONDITION_CELLIS
> || $conditional->getConditionType() === Conditional::CONDITION_CONTAINSTEXT
> || $conditional->getConditionType() === Conditional::CONDITION_NOTCONTAINSTEXT
> || $conditional->getConditionType() === Conditional::CONDITION_BEGINSWITH
> || $conditional->getConditionType() === Conditional::CONDITION_ENDSWITH
> ) && $conditional->getOperatorType() !== Conditional::OPERATOR_NONE,
> 'operator',
> $conditional->getOperatorType()
> );
>
> self::writeAttributeIf($objWriter, $conditional->getStopIfTrue(), 'stopIfTrue', '1');
>
> $cellRange = Coordinate::splitRange(str_replace('$', '', strtoupper($cellCoordinate)));
> [$topLeftCell] = $cellRange[0];
>
> if (
> $conditional->getConditionType() === Conditional::CONDITION_CONTAINSTEXT
> || $conditional->getConditionType() === Conditional::CONDITION_NOTCONTAINSTEXT
> || $conditional->getConditionType() === Conditional::CONDITION_BEGINSWITH
> || $conditional->getConditionType() === Conditional::CONDITION_ENDSWITH
> ) {
> self::writeTextCondElements($objWriter, $conditional, $topLeftCell);
> } elseif ($conditional->getConditionType() === Conditional::CONDITION_TIMEPERIOD) {
> self::writeTimePeriodCondElements($objWriter, $conditional, $topLeftCell);
> } else {
> self::writeOtherCondElements($objWriter, $conditional, $topLeftCell);
< $objWriter->endElement();
> //<dataBar>
> self::writeDataBarElements($objWriter, $conditional->getDataBar());
< $objWriter->endElement();
< }
> $objWriter->endElement(); //end cfRule
$objWriter->writeAttribute('gridLines', ($pSheet->getPrintGridlines() ? 'true' : 'false'));
>
$objWriter->writeAttribute('gridLinesSet', 'true');
> $objWriter->endElement(); //end conditionalFormatting
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< private function writeDataValidations(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeDataValidations(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
< $dataValidationCollection = $pSheet->getDataValidationCollection();
> $dataValidationCollection = $worksheet->getDataValidationCollection();
< $objWriter->writeAttribute('count', count($dataValidationCollection));
> $objWriter->writeAttribute('count', (string) count($dataValidationCollection));
< $objWriter->writeAttribute('sqref', $coordinate);
> $objWriter->writeAttribute('sqref', $dv->getSqref() ?? $coordinate);
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< private function writeHyperlinks(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeHyperlinks(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
< $hyperlinkCollection = $pSheet->getHyperlinkCollection();
> $hyperlinkCollection = $worksheet->getHyperlinkCollection();
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< private function writeProtectedRanges(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeProtectedRanges(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
< if (count($pSheet->getProtectedCells()) > 0) {
> if (count($worksheet->getProtectedCells()) > 0) {
< foreach ($pSheet->getProtectedCells() as $protectedCell => $passwordHash) {
> foreach ($worksheet->getProtectedCells() as $protectedCell => $passwordHash) {
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< private function writeMergeCells(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeMergeCells(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
< if (count($pSheet->getMergeCells()) > 0) {
> if (count($worksheet->getMergeCells()) > 0) {
< foreach ($pSheet->getMergeCells() as $mergeCell) {
> foreach ($worksheet->getMergeCells() as $mergeCell) {
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< private function writePrintOptions(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writePrintOptions(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
< $objWriter->writeAttribute('gridLines', ($pSheet->getPrintGridlines() ? 'true' : 'false'));
> $objWriter->writeAttribute('gridLines', ($worksheet->getPrintGridlines() ? 'true' : 'false'));
< if ($pSheet->getPageSetup()->getHorizontalCentered()) {
> if ($worksheet->getPageSetup()->getHorizontalCentered()) {
< if ($pSheet->getPageSetup()->getVerticalCentered()) {
> if ($worksheet->getPageSetup()->getVerticalCentered()) {
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< private function writePageMargins(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writePageMargins(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
< $objWriter->writeAttribute('left', StringHelper::formatNumber($pSheet->getPageMargins()->getLeft()));
< $objWriter->writeAttribute('right', StringHelper::formatNumber($pSheet->getPageMargins()->getRight()));
< $objWriter->writeAttribute('top', StringHelper::formatNumber($pSheet->getPageMargins()->getTop()));
< $objWriter->writeAttribute('bottom', StringHelper::formatNumber($pSheet->getPageMargins()->getBottom()));
< $objWriter->writeAttribute('header', StringHelper::formatNumber($pSheet->getPageMargins()->getHeader()));
< $objWriter->writeAttribute('footer', StringHelper::formatNumber($pSheet->getPageMargins()->getFooter()));
> $objWriter->writeAttribute('left', StringHelper::formatNumber($worksheet->getPageMargins()->getLeft()));
> $objWriter->writeAttribute('right', StringHelper::formatNumber($worksheet->getPageMargins()->getRight()));
> $objWriter->writeAttribute('top', StringHelper::formatNumber($worksheet->getPageMargins()->getTop()));
> $objWriter->writeAttribute('bottom', StringHelper::formatNumber($worksheet->getPageMargins()->getBottom()));
> $objWriter->writeAttribute('header', StringHelper::formatNumber($worksheet->getPageMargins()->getHeader()));
> $objWriter->writeAttribute('footer', StringHelper::formatNumber($worksheet->getPageMargins()->getFooter()));
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< private function writeAutoFilter(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeAutoFilter(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
< $autoFilterRange = $pSheet->getAutoFilter()->getRange();
> $autoFilterRange = $worksheet->getAutoFilter()->getRange();
$range = $range[0];
// Strip any worksheet ref
[$ws, $range[0]] = PhpspreadsheetWorksheet::extractSheetTitle($range[0], true);
$range = implode(':', $range);
$objWriter->writeAttribute('ref', str_replace('$', '', $range));
< $columns = $pSheet->getAutoFilter()->getColumns();
> $columns = $worksheet->getAutoFilter()->getColumns();
if (count($columns) > 0) {
foreach ($columns as $columnID => $column) {
$rules = $column->getRules();
if (count($rules) > 0) {
$objWriter->startElement('filterColumn');
< $objWriter->writeAttribute('colId', $pSheet->getAutoFilter()->getColumnOffset($columnID));
> $objWriter->writeAttribute('colId', (string) $worksheet->getAutoFilter()->getColumnOffset($columnID));
$objWriter->startElement($column->getFilterType());
if ($column->getJoin() == Column::AUTOFILTER_COLUMN_JOIN_AND) {
< $objWriter->writeAttribute('and', 1);
> $objWriter->writeAttribute('and', '1');
}
foreach ($rules as $rule) {
< if (($column->getFilterType() === Column::AUTOFILTER_FILTERTYPE_FILTER) &&
> if (
> ($column->getFilterType() === Column::AUTOFILTER_FILTERTYPE_FILTER) &&
($rule->getOperator() === Rule::AUTOFILTER_COLUMN_RULE_EQUAL) &&
< ($rule->getValue() === '')) {
> ($rule->getValue() === '')
> ) {
// Filter rule for Blanks
< $objWriter->writeAttribute('blank', 1);
> $objWriter->writeAttribute('blank', '1');
} elseif ($rule->getRuleType() === Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER) {
// Dynamic Filter Rule
$objWriter->writeAttribute('type', $rule->getGrouping());
$val = $column->getAttribute('val');
if ($val !== null) {
< $objWriter->writeAttribute('val', $val);
> $objWriter->writeAttribute('val', "$val");
}
$maxVal = $column->getAttribute('maxVal');
if ($maxVal !== null) {
< $objWriter->writeAttribute('maxVal', $maxVal);
> $objWriter->writeAttribute('maxVal', "$maxVal");
}
} elseif ($rule->getRuleType() === Rule::AUTOFILTER_RULETYPE_TOPTENFILTER) {
// Top 10 Filter Rule
< $objWriter->writeAttribute('val', $rule->getValue());
> $ruleValue = $rule->getValue();
> if (!is_array($ruleValue)) {
> $objWriter->writeAttribute('val', "$ruleValue");
> }
$objWriter->writeAttribute('percent', (($rule->getOperator() === Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT) ? '1' : '0'));
$objWriter->writeAttribute('top', (($rule->getGrouping() === Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP) ? '1' : '0'));
} else {
// Filter, DateGroupItem or CustomFilter
$objWriter->startElement($rule->getRuleType());
if ($rule->getOperator() !== Rule::AUTOFILTER_COLUMN_RULE_EQUAL) {
$objWriter->writeAttribute('operator', $rule->getOperator());
}
if ($rule->getRuleType() === Rule::AUTOFILTER_RULETYPE_DATEGROUP) {
// Date Group filters
< foreach ($rule->getValue() as $key => $value) {
< if ($value > '') {
< $objWriter->writeAttribute($key, $value);
> $ruleValue = $rule->getValue();
> if (is_array($ruleValue)) {
> foreach ($ruleValue as $key => $value) {
> $objWriter->writeAttribute($key, "$value");
}
}
$objWriter->writeAttribute('dateTimeGrouping', $rule->getGrouping());
} else {
< $objWriter->writeAttribute('val', $rule->getValue());
> $ruleValue = $rule->getValue();
> if (!is_array($ruleValue)) {
> $objWriter->writeAttribute('val', "$ruleValue");
> }
}
$objWriter->endElement();
}
}
$objWriter->endElement();
$objWriter->endElement();
}
}
}
$objWriter->endElement();
}
}
/**
> * Write Table.
* Write PageSetup.
> */
*
> private function writeTable(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
* @param XMLWriter $objWriter XML Writer
> {
* @param PhpspreadsheetWorksheet $pSheet Worksheet
> $tableCount = $worksheet->getTableCollection()->count();
*/
>
private function writePageSetup(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> $objWriter->startElement('tableParts');
{
> $objWriter->writeAttribute('count', (string) $tableCount);
// pageSetup
>
$objWriter->startElement('pageSetup');
> for ($t = 1; $t <= $tableCount; ++$t) {
$objWriter->writeAttribute('paperSize', $pSheet->getPageSetup()->getPaperSize());
> $objWriter->startElement('tablePart');
$objWriter->writeAttribute('orientation', $pSheet->getPageSetup()->getOrientation());
> $objWriter->writeAttribute('r:id', 'rId_table_' . $t);
> $objWriter->endElement();
if ($pSheet->getPageSetup()->getScale() !== null) {
> }
$objWriter->writeAttribute('scale', $pSheet->getPageSetup()->getScale());
>
}
> $objWriter->endElement();
if ($pSheet->getPageSetup()->getFitToHeight() !== null) {
> }
$objWriter->writeAttribute('fitToHeight', $pSheet->getPageSetup()->getFitToHeight());
>
} else {
> /**
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< private function writePageSetup(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writePageSetup(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
< $objWriter->writeAttribute('paperSize', $pSheet->getPageSetup()->getPaperSize());
< $objWriter->writeAttribute('orientation', $pSheet->getPageSetup()->getOrientation());
> $objWriter->writeAttribute('paperSize', (string) $worksheet->getPageSetup()->getPaperSize());
> $objWriter->writeAttribute('orientation', $worksheet->getPageSetup()->getOrientation());
< if ($pSheet->getPageSetup()->getScale() !== null) {
< $objWriter->writeAttribute('scale', $pSheet->getPageSetup()->getScale());
> if ($worksheet->getPageSetup()->getScale() !== null) {
> $objWriter->writeAttribute('scale', (string) $worksheet->getPageSetup()->getScale());
< if ($pSheet->getPageSetup()->getFitToHeight() !== null) {
< $objWriter->writeAttribute('fitToHeight', $pSheet->getPageSetup()->getFitToHeight());
> if ($worksheet->getPageSetup()->getFitToHeight() !== null) {
> $objWriter->writeAttribute('fitToHeight', (string) $worksheet->getPageSetup()->getFitToHeight());
< if ($pSheet->getPageSetup()->getFitToWidth() !== null) {
< $objWriter->writeAttribute('fitToWidth', $pSheet->getPageSetup()->getFitToWidth());
> if ($worksheet->getPageSetup()->getFitToWidth() !== null) {
> $objWriter->writeAttribute('fitToWidth', (string) $worksheet->getPageSetup()->getFitToWidth());
< if ($pSheet->getPageSetup()->getFirstPageNumber() !== null) {
< $objWriter->writeAttribute('firstPageNumber', $pSheet->getPageSetup()->getFirstPageNumber());
> if (!empty($worksheet->getPageSetup()->getFirstPageNumber())) {
> $objWriter->writeAttribute('firstPageNumber', (string) $worksheet->getPageSetup()->getFirstPageNumber());
$objWriter->writeAttribute('r:id', $getUnparsedLoadedData['sheets'][$pSheet->getCodeName()]['pageSetupRelId']);
> $objWriter->writeAttribute('pageOrder', $worksheet->getPageSetup()->getPageOrder());
< $getUnparsedLoadedData = $pSheet->getParent()->getUnparsedLoadedData();
< if (isset($getUnparsedLoadedData['sheets'][$pSheet->getCodeName()]['pageSetupRelId'])) {
< $objWriter->writeAttribute('r:id', $getUnparsedLoadedData['sheets'][$pSheet->getCodeName()]['pageSetupRelId']);
> $getUnparsedLoadedData = $worksheet->getParent()->getUnparsedLoadedData();
> if (isset($getUnparsedLoadedData['sheets'][$worksheet->getCodeName()]['pageSetupRelId'])) {
> $objWriter->writeAttribute('r:id', $getUnparsedLoadedData['sheets'][$worksheet->getCodeName()]['pageSetupRelId']);
}
/**
* Write Header / Footer.
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
*/
< private function writeHeaderFooter(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeHeaderFooter(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
{
// headerFooter
$objWriter->startElement('headerFooter');
< $objWriter->writeAttribute('differentOddEven', ($pSheet->getHeaderFooter()->getDifferentOddEven() ? 'true' : 'false'));
< $objWriter->writeAttribute('differentFirst', ($pSheet->getHeaderFooter()->getDifferentFirst() ? 'true' : 'false'));
< $objWriter->writeAttribute('scaleWithDoc', ($pSheet->getHeaderFooter()->getScaleWithDocument() ? 'true' : 'false'));
< $objWriter->writeAttribute('alignWithMargins', ($pSheet->getHeaderFooter()->getAlignWithMargins() ? 'true' : 'false'));
<
< $objWriter->writeElement('oddHeader', $pSheet->getHeaderFooter()->getOddHeader());
< $objWriter->writeElement('oddFooter', $pSheet->getHeaderFooter()->getOddFooter());
< $objWriter->writeElement('evenHeader', $pSheet->getHeaderFooter()->getEvenHeader());
< $objWriter->writeElement('evenFooter', $pSheet->getHeaderFooter()->getEvenFooter());
< $objWriter->writeElement('firstHeader', $pSheet->getHeaderFooter()->getFirstHeader());
< $objWriter->writeElement('firstFooter', $pSheet->getHeaderFooter()->getFirstFooter());
> $objWriter->writeAttribute('differentOddEven', ($worksheet->getHeaderFooter()->getDifferentOddEven() ? 'true' : 'false'));
> $objWriter->writeAttribute('differentFirst', ($worksheet->getHeaderFooter()->getDifferentFirst() ? 'true' : 'false'));
> $objWriter->writeAttribute('scaleWithDoc', ($worksheet->getHeaderFooter()->getScaleWithDocument() ? 'true' : 'false'));
> $objWriter->writeAttribute('alignWithMargins', ($worksheet->getHeaderFooter()->getAlignWithMargins() ? 'true' : 'false'));
>
> $objWriter->writeElement('oddHeader', $worksheet->getHeaderFooter()->getOddHeader());
> $objWriter->writeElement('oddFooter', $worksheet->getHeaderFooter()->getOddFooter());
> $objWriter->writeElement('evenHeader', $worksheet->getHeaderFooter()->getEvenHeader());
> $objWriter->writeElement('evenFooter', $worksheet->getHeaderFooter()->getEvenFooter());
> $objWriter->writeElement('firstHeader', $worksheet->getHeaderFooter()->getFirstHeader());
> $objWriter->writeElement('firstFooter', $worksheet->getHeaderFooter()->getFirstFooter());
$objWriter->endElement();
}
/**
* Write Breaks.
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
*/
< private function writeBreaks(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeBreaks(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
{
// Get row and column breaks
$aRowBreaks = [];
$aColumnBreaks = [];
< foreach ($pSheet->getBreaks() as $cell => $breakType) {
> foreach ($worksheet->getBreaks() as $cell => $breakType) {
if ($breakType == PhpspreadsheetWorksheet::BREAK_ROW) {
$aRowBreaks[] = $cell;
} elseif ($breakType == PhpspreadsheetWorksheet::BREAK_COLUMN) {
$aColumnBreaks[] = $cell;
}
}
// rowBreaks
if (!empty($aRowBreaks)) {
$objWriter->startElement('rowBreaks');
< $objWriter->writeAttribute('count', count($aRowBreaks));
< $objWriter->writeAttribute('manualBreakCount', count($aRowBreaks));
> $objWriter->writeAttribute('count', (string) count($aRowBreaks));
> $objWriter->writeAttribute('manualBreakCount', (string) count($aRowBreaks));
foreach ($aRowBreaks as $cell) {
$coords = Coordinate::coordinateFromString($cell);
$objWriter->startElement('brk');
$objWriter->writeAttribute('id', $coords[1]);
$objWriter->writeAttribute('man', '1');
$objWriter->endElement();
}
$objWriter->endElement();
}
// Second, write column breaks
if (!empty($aColumnBreaks)) {
$objWriter->startElement('colBreaks');
< $objWriter->writeAttribute('count', count($aColumnBreaks));
< $objWriter->writeAttribute('manualBreakCount', count($aColumnBreaks));
> $objWriter->writeAttribute('count', (string) count($aColumnBreaks));
> $objWriter->writeAttribute('manualBreakCount', (string) count($aColumnBreaks));
foreach ($aColumnBreaks as $cell) {
$coords = Coordinate::coordinateFromString($cell);
$objWriter->startElement('brk');
< $objWriter->writeAttribute('id', Coordinate::columnIndexFromString($coords[0]) - 1);
> $objWriter->writeAttribute('id', (string) (Coordinate::columnIndexFromString($coords[0]) - 1));
$objWriter->writeAttribute('man', '1');
$objWriter->endElement();
}
$objWriter->endElement();
}
}
/**
* Write SheetData.
*
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< * @param string[] $pStringTable String table
< *
< * @throws WriterException
> * @param string[] $stringTable String table
*/
< private function writeSheetData(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet, array $pStringTable)
> private function writeSheetData(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet, array $stringTable): void
{
// Flipped stringtable, for faster index searching
< $aFlippedStringTable = $this->getParentWriter()->getWriterPart('stringtable')->flipStringTable($pStringTable);
> $aFlippedStringTable = $this->getParentWriter()->getWriterPartstringtable()->flipStringTable($stringTable);
// sheetData
$objWriter->startElement('sheetData');
// Get column count
< $colCount = Coordinate::columnIndexFromString($pSheet->getHighestColumn());
> $colCount = Coordinate::columnIndexFromString($worksheet->getHighestColumn());
// Highest row number
< $highestRow = $pSheet->getHighestRow();
> $highestRow = $worksheet->getHighestRow();
< // Loop through cells
> // Loop through cells building a comma-separated list of the columns in each row
> // This is a trade-off between the memory usage that is required for a full array of columns,
> // and execution speed
> /** @var array<int, string> $cellsByRow */
$cellsByRow = [];
< foreach ($pSheet->getCoordinates() as $coordinate) {
< $cellAddress = Coordinate::coordinateFromString($coordinate);
< $cellsByRow[$cellAddress[1]][] = $coordinate;
> foreach ($worksheet->getCoordinates() as $coordinate) {
> [$column, $row] = Coordinate::coordinateFromString($coordinate);
> $cellsByRow[$row] = $cellsByRow[$row] ?? '';
> $cellsByRow[$row] .= "{$column},";
}
$currentRow = 0;
while ($currentRow++ < $highestRow) {
> $isRowSet = isset($cellsByRow[$currentRow]);
// Get row dimension
> if ($isRowSet || $worksheet->rowDimensionExists($currentRow)) {
< $rowDimension = $pSheet->getRowDimension($currentRow);
> $rowDimension = $worksheet->getRowDimension($currentRow);
// Write current row?
< $writeCurrentRow = isset($cellsByRow[$currentRow]) || $rowDimension->getRowHeight() >= 0 || $rowDimension->getVisible() == false || $rowDimension->getCollapsed() == true || $rowDimension->getOutlineLevel() > 0 || $rowDimension->getXfIndex() !== null;
> $writeCurrentRow = $isRowSet || $rowDimension->getRowHeight() >= 0 || $rowDimension->getVisible() === false || $rowDimension->getCollapsed() === true || $rowDimension->getOutlineLevel() > 0 || $rowDimension->getXfIndex() !== null;
if ($writeCurrentRow) {
// Start a new row
$objWriter->startElement('row');
< $objWriter->writeAttribute('r', $currentRow);
> $objWriter->writeAttribute('r', "$currentRow");
$objWriter->writeAttribute('spans', '1:' . $colCount);
// Row dimensions
if ($rowDimension->getRowHeight() >= 0) {
$objWriter->writeAttribute('customHeight', '1');
$objWriter->writeAttribute('ht', StringHelper::formatNumber($rowDimension->getRowHeight()));
}
// Row visibility
if (!$rowDimension->getVisible() === true) {
$objWriter->writeAttribute('hidden', 'true');
}
// Collapsed
if ($rowDimension->getCollapsed() === true) {
$objWriter->writeAttribute('collapsed', 'true');
}
// Outline level
if ($rowDimension->getOutlineLevel() > 0) {
< $objWriter->writeAttribute('outlineLevel', $rowDimension->getOutlineLevel());
> $objWriter->writeAttribute('outlineLevel', (string) $rowDimension->getOutlineLevel());
}
// Style
if ($rowDimension->getXfIndex() !== null) {
< $objWriter->writeAttribute('s', $rowDimension->getXfIndex());
> $objWriter->writeAttribute('s', (string) $rowDimension->getXfIndex());
$objWriter->writeAttribute('customFormat', '1');
}
// Write cells
if (isset($cellsByRow[$currentRow])) {
< foreach ($cellsByRow[$currentRow] as $cellAddress) {
> // We have a comma-separated list of column names (with a trailing entry); split to an array
> $columnsInRow = explode(',', $cellsByRow[$currentRow]);
> array_pop($columnsInRow);
> foreach ($columnsInRow as $column) {
// Write cell
< $this->writeCell($objWriter, $pSheet, $cellAddress, $aFlippedStringTable);
> $this->writeCell($objWriter, $worksheet, "{$column}{$currentRow}", $aFlippedStringTable);
}
}
// End row
$objWriter->endElement();
}
}
> }
$objWriter->endElement();
}
/**
< * Write Cell.
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< * @param Cell $pCellAddress Cell Address
< * @param string[] $pFlippedStringTable String table (flipped), for faster index searching
< *
< * @throws WriterException
> * @param RichText|string $cellValue
*/
< private function writeCell(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet, $pCellAddress, array $pFlippedStringTable)
> private function writeCellInlineStr(XMLWriter $objWriter, string $mappedType, $cellValue): void
{
< // Cell
< $pCell = $pSheet->getCell($pCellAddress);
< $objWriter->startElement('c');
< $objWriter->writeAttribute('r', $pCellAddress);
<
< // Sheet styles
< if ($pCell->getXfIndex() != '') {
< $objWriter->writeAttribute('s', $pCell->getXfIndex());
> $objWriter->writeAttribute('t', $mappedType);
> if (!$cellValue instanceof RichText) {
> $objWriter->startElement('is');
> $objWriter->writeElement(
> 't',
> StringHelper::controlCharacterPHP2OOXML(htmlspecialchars($cellValue, Settings::htmlEntityFlags()))
> );
> $objWriter->endElement();
> } else {
> $objWriter->startElement('is');
> $this->getParentWriter()->getWriterPartstringtable()->writeRichText($objWriter, $cellValue);
> $objWriter->endElement();
> }
}
< // If cell value is supplied, write cell value
< $cellValue = $pCell->getValue();
< if (is_object($cellValue) || $cellValue !== '') {
< // Map type
< $mappedType = $pCell->getDataType();
<
< // Write data type depending on its type
< switch (strtolower($mappedType)) {
< case 'inlinestr': // Inline string
< case 's': // String
< case 'b': // Boolean
> /**
> * @param RichText|string $cellValue
> * @param string[] $flippedStringTable
> */
> private function writeCellString(XMLWriter $objWriter, string $mappedType, $cellValue, array $flippedStringTable): void
> {
$objWriter->writeAttribute('t', $mappedType);
> if (!$cellValue instanceof RichText) {
> self::writeElementIf($objWriter, isset($flippedStringTable[$cellValue]), 'v', $flippedStringTable[$cellValue] ?? '');
break;
> } else {
case 'f': // Formula
> $objWriter->writeElement('v', $flippedStringTable[$cellValue->getHashCode()]);
$calculatedValue = ($this->getParentWriter()->getPreCalculateFormulas()) ?
> }
$pCell->getCalculatedValue() : $cellValue;
> }
< break;
< case 'f': // Formula
< $calculatedValue = ($this->getParentWriter()->getPreCalculateFormulas()) ?
< $pCell->getCalculatedValue() : $cellValue;
< if (is_string($calculatedValue)) {
< $objWriter->writeAttribute('t', 'str');
< } elseif (is_bool($calculatedValue)) {
< $objWriter->writeAttribute('t', 'b');
> /**
> * @param float|int $cellValue
> */
> private function writeCellNumeric(XMLWriter $objWriter, $cellValue): void
> {
> //force a decimal to be written if the type is float
> if (is_float($cellValue)) {
> // force point as decimal separator in case current locale uses comma
> $cellValue = str_replace(',', '.', (string) $cellValue);
> if (strpos($cellValue, '.') === false) {
> $cellValue = $cellValue . '.0';
> }
> }
> $objWriter->writeElement('v', "$cellValue");
< break;
< case 'e': // Error
> private function writeCellBoolean(XMLWriter $objWriter, string $mappedType, bool $cellValue): void
> {
> $objWriter->writeElement('v', $cellValue ? '1' : '0');
< // Write data depending on its type
< switch (strtolower($mappedType)) {
< case 'inlinestr': // Inline string
< if (!$cellValue instanceof RichText) {
< $objWriter->writeElement('t', StringHelper::controlCharacterPHP2OOXML(htmlspecialchars($cellValue)));
< } elseif ($cellValue instanceof RichText) {
< $objWriter->startElement('is');
< $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter, $cellValue);
< $objWriter->endElement();
> private function writeCellError(XMLWriter $objWriter, string $mappedType, string $cellValue, string $formulaerr = '#NULL!'): void
> {
> $objWriter->writeAttribute('t', $mappedType);
> $cellIsFormula = substr($cellValue, 0, 1) === '=';
> self::writeElementIf($objWriter, $cellIsFormula, 'f', Xlfn::addXlfnStripEquals($cellValue));
> $objWriter->writeElement('v', $cellIsFormula ? $formulaerr : $cellValue);
}
< break;
< case 's': // String
< if (!$cellValue instanceof RichText) {
< if (isset($pFlippedStringTable[$cellValue])) {
< $objWriter->writeElement('v', $pFlippedStringTable[$cellValue]);
> private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell $cell): void
> {
> $calculatedValue = $this->getParentWriter()->getPreCalculateFormulas() ? $cell->getCalculatedValue() : $cellValue;
> if (is_string($calculatedValue)) {
> if (ErrorValue::isError($calculatedValue)) {
> $this->writeCellError($objWriter, 'e', $cellValue, $calculatedValue);
>
> return;
}
< } elseif ($cellValue instanceof RichText) {
< $objWriter->writeElement('v', $pFlippedStringTable[$cellValue->getHashCode()]);
> $objWriter->writeAttribute('t', 'str');
> $calculatedValue = StringHelper::controlCharacterPHP2OOXML($calculatedValue);
> } elseif (is_bool($calculatedValue)) {
> $objWriter->writeAttribute('t', 'b');
> $calculatedValue = (int) $calculatedValue;
}
< break;
< case 'f': // Formula
< $attributes = $pCell->getFormulaAttributes();
> $attributes = $cell->getFormulaAttributes();
if (($attributes['t'] ?? null) === 'array') {
$objWriter->startElement('f');
$objWriter->writeAttribute('t', 'array');
< $objWriter->writeAttribute('ref', $pCellAddress);
> $objWriter->writeAttribute('ref', $cell->getCoordinate());
$objWriter->writeAttribute('aca', '1');
$objWriter->writeAttribute('ca', '1');
$objWriter->text(substr($cellValue, 1));
$objWriter->endElement();
} else {
< $objWriter->writeElement('f', substr($cellValue, 1));
< }
< if ($this->getParentWriter()->getOffice2003Compatibility() === false) {
< if ($this->getParentWriter()->getPreCalculateFormulas()) {
< if (!is_array($calculatedValue) && substr($calculatedValue, 0, 1) !== '#') {
< $objWriter->writeElement('v', StringHelper::formatNumber($calculatedValue));
< } else {
< $objWriter->writeElement('v', '0');
< }
< } else {
< $objWriter->writeElement('v', '0');
> $objWriter->writeElement('f', Xlfn::addXlfnStripEquals($cellValue));
> self::writeElementIf(
> $objWriter,
> $this->getParentWriter()->getOffice2003Compatibility() === false,
> 'v',
> ($this->getParentWriter()->getPreCalculateFormulas() && !is_array($calculatedValue) && substr($calculatedValue ?? '', 0, 1) !== '#')
> ? StringHelper::formatNumber($calculatedValue) : '0'
> );
}
}
> /**
break;
> * Write Cell.
case 'n': // Numeric
> *
//force a decimal to be written if the type is float
> * @param string $cellAddress Cell Address
if (is_float($cellValue)) {
> * @param string[] $flippedStringTable String table (flipped), for faster index searching
$cellValue = (string) $cellValue;
> */
if (strpos($cellValue, '.') === false) {
> private function writeCell(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet, string $cellAddress, array $flippedStringTable): void
$cellValue = $cellValue . '.0';
> {
}
> // Cell
}
> $pCell = $worksheet->getCell($cellAddress);
// force point as decimal separator in case current locale uses comma
> $objWriter->startElement('c');
$objWriter->writeElement('v', str_replace(',', '.', $cellValue));
> $objWriter->writeAttribute('r', $cellAddress);
>
break;
> // Sheet styles
case 'b': // Boolean
> $xfi = $pCell->getXfIndex();
$objWriter->writeElement('v', ($cellValue ? '1' : '0'));
> self::writeAttributeIf($objWriter, (bool) $xfi, 's', "$xfi");
>
break;
> // If cell value is supplied, write cell value
case 'e': // Error
> $cellValue = $pCell->getValue();
if (substr($cellValue, 0, 1) === '=') {
> if (is_object($cellValue) || $cellValue !== '') {
$objWriter->writeElement('f', substr($cellValue, 1));
> // Map type
$objWriter->writeElement('v', substr($cellValue, 1));
> $mappedType = $pCell->getDataType();
} else {
>
$objWriter->writeElement('v', $cellValue);
> // Write data depending on its type
}
> switch (strtolower($mappedType)) {
> case 'inlinestr': // Inline string
break;
> $this->writeCellInlineStr($objWriter, $mappedType, $cellValue);
}
>
}
> break;
> case 's': // String
$objWriter->endElement();
> $this->writeCellString($objWriter, $mappedType, $cellValue, $flippedStringTable);
}
>
> break;
/**
> case 'f': // Formula
* Write Drawings.
> $this->writeCellFormula($objWriter, $cellValue, $pCell);
*
>
< //force a decimal to be written if the type is float
< if (is_float($cellValue)) {
< $cellValue = (string) $cellValue;
< if (strpos($cellValue, '.') === false) {
< $cellValue = $cellValue . '.0';
< }
< }
< // force point as decimal separator in case current locale uses comma
< $objWriter->writeElement('v', str_replace(',', '.', $cellValue));
> $this->writeCellNumeric($objWriter, $cellValue);
< $objWriter->writeElement('v', ($cellValue ? '1' : '0'));
> $this->writeCellBoolean($objWriter, $mappedType, $cellValue);
< if (substr($cellValue, 0, 1) === '=') {
< $objWriter->writeElement('f', substr($cellValue, 1));
< $objWriter->writeElement('v', substr($cellValue, 1));
< } else {
< $objWriter->writeElement('v', $cellValue);
< }
<
< break;
> $this->writeCellError($objWriter, $mappedType, $cellValue);
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
< private function writeDrawings(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet, $includeCharts = false)
> private function writeDrawings(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet, $includeCharts = false): void
< $unparsedLoadedData = $pSheet->getParent()->getUnparsedLoadedData();
< $hasUnparsedDrawing = isset($unparsedLoadedData['sheets'][$pSheet->getCodeName()]['drawingOriginalIds']);
< $chartCount = ($includeCharts) ? $pSheet->getChartCollection()->count() : 0;
< if ($chartCount == 0 && $pSheet->getDrawingCollection()->count() == 0 && !$hasUnparsedDrawing) {
> $unparsedLoadedData = $worksheet->getParent()->getUnparsedLoadedData();
> $hasUnparsedDrawing = isset($unparsedLoadedData['sheets'][$worksheet->getCodeName()]['drawingOriginalIds']);
> $chartCount = ($includeCharts) ? $worksheet->getChartCollection()->count() : 0;
> if ($chartCount == 0 && $worksheet->getDrawingCollection()->count() == 0 && !$hasUnparsedDrawing) {
< if (isset($unparsedLoadedData['sheets'][$pSheet->getCodeName()]['drawingOriginalIds'])) {
< $drawingOriginalIds = $unparsedLoadedData['sheets'][$pSheet->getCodeName()]['drawingOriginalIds'];
> if (isset($unparsedLoadedData['sheets'][$worksheet->getCodeName()]['drawingOriginalIds'])) {
> $drawingOriginalIds = $unparsedLoadedData['sheets'][$worksheet->getCodeName()]['drawingOriginalIds'];
/**
> // (! synchronize with \PhpOffice\PhpSpreadsheet\Writer\Xlsx\Rels::writeWorksheetRelationships)
* Write LegacyDrawing.
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
*/
< private function writeLegacyDrawing(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeLegacyDrawing(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
{
// If sheet contains comments, add the relationships
< if (count($pSheet->getComments()) > 0) {
> if (count($worksheet->getComments()) > 0) {
$objWriter->startElement('legacyDrawing');
$objWriter->writeAttribute('r:id', 'rId_comments_vml1');
$objWriter->endElement();
}
}
/**
* Write LegacyDrawingHF.
< *
< * @param XMLWriter $objWriter XML Writer
< * @param PhpspreadsheetWorksheet $pSheet Worksheet
*/
< private function writeLegacyDrawingHF(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeLegacyDrawingHF(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
{
// If sheet contains images, add the relationships
< if (count($pSheet->getHeaderFooter()->getImages()) > 0) {
> if (count($worksheet->getHeaderFooter()->getImages()) > 0) {
$objWriter->startElement('legacyDrawingHF');
$objWriter->writeAttribute('r:id', 'rId_headerfooter_vml1');
$objWriter->endElement();
}
}
< private function writeAlternateContent(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet)
> private function writeAlternateContent(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
{
< if (empty($pSheet->getParent()->getUnparsedLoadedData()['sheets'][$pSheet->getCodeName()]['AlternateContents'])) {
> if (empty($worksheet->getParent()->getUnparsedLoadedData()['sheets'][$worksheet->getCodeName()]['AlternateContents'])) {
return;
}
< foreach ($pSheet->getParent()->getUnparsedLoadedData()['sheets'][$pSheet->getCodeName()]['AlternateContents'] as $alternateContent) {
> foreach ($worksheet->getParent()->getUnparsedLoadedData()['sheets'][$worksheet->getCodeName()]['AlternateContents'] as $alternateContent) {
$objWriter->writeRaw($alternateContent);
> }
}
> }
}
>
}
> /**
> * write <ExtLst>
> * only implementation conditionalFormattings.
> *
> * @url https://docs.microsoft.com/en-us/openspecs/office_standards/ms-xlsx/07d607af-5618-4ca2-b683-6a78dc0d9627
> */
> private function writeExtLst(XMLWriter $objWriter, PhpspreadsheetWorksheet $worksheet): void
> {
> $conditionalFormattingRuleExtList = [];
> foreach ($worksheet->getConditionalStylesCollection() as $cellCoordinate => $conditionalStyles) {
> /** @var Conditional $conditional */
> foreach ($conditionalStyles as $conditional) {
> $dataBar = $conditional->getDataBar();
> if ($dataBar && $dataBar->getConditionalFormattingRuleExt()) {
> $conditionalFormattingRuleExtList[] = $dataBar->getConditionalFormattingRuleExt();
> }
> }
> }
>
> if (count($conditionalFormattingRuleExtList) > 0) {
> $conditionalFormattingRuleExtNsPrefix = 'x14';
> $objWriter->startElement('extLst');
> $objWriter->startElement('ext');
> $objWriter->writeAttribute('uri', '{78C0D931-6437-407d-A8EE-F0AAD7539E65}');
> $objWriter->startElementNs($conditionalFormattingRuleExtNsPrefix, 'conditionalFormattings', null);
> foreach ($conditionalFormattingRuleExtList as $extension) {
> self::writeExtConditionalFormattingElements($objWriter, $extension);
> }
> $objWriter->endElement(); //end conditionalFormattings
> $objWriter->endElement(); //end ext
> $objWriter->endElement(); //end extLst