1
0
mirror of https://github.com/shuchkin/simplexlsxgen.git synced 2023-08-10 21:12:59 +03:00

Update SimpleXLSXGen.php

This commit is contained in:
Javier 2023-07-23 14:45:18 -03:00
parent 8557b06a47
commit b26e0dfc20

View File

@ -1145,11 +1145,11 @@ class SimpleXLSXGen
} }
/** /**
* Convert A1 cell reference style to R1C1 cell reference style (row/col number starting from 1) * Convert A1 cell reference format to R1C1 cell reference format (row/col number starting from 1)
* *
* @param string $cell Cell reference in A1 format * @param string $cell Cell reference in A1 format
* *
* @return array Cell reference in R1C1 format as a two element array [row (ie. y coord), col (ie. x coord)] * @return array Cell reference in R1C1 format as a 2-element integer array: [row (ie. y coord), col (ie. x coord)]
*/ */
public static function cell2coord($cell) public static function cell2coord($cell)
{ {
@ -1181,12 +1181,12 @@ class SimpleXLSXGen
} }
/** /**
* Convert R1C1 cell reference style (row/col 1-based) to A1 cell reference style * Convert R1C1 cell reference format (row/col 1-based) to A1 cell reference format
* *
* @param integer $y Row number (starting from 1) or a 2 element array with reference. * @param integer $y Row number (starting from 1) or a 2-element integer array with cell reference.
* @param integer $x Optional. Column number (starting from 1). Not used if $y is an array. * @param integer $x Optional. Column number (starting from 1). Not used if $y is an array.
* *
* @return string Cell reference in A1 format * @return string Cell reference in A1 format
*/ */
public static function coord2cell($y, $x = null) public static function coord2cell($y, $x = null)
{ {
@ -1202,11 +1202,11 @@ class SimpleXLSXGen
} }
/** /**
* Convert A1 range reference style to R1C1 cell reference style * Convert A1 range reference format to R1C1 range reference format
* *
* @param string $range Range reference in A1 format * @param string $range Range reference in A1 format
* *
* @return array Cell reference in R1C1 format as a four element array [top-left row, top-left col, bottom-right row, bottom-right col] * @return array Range reference in R1C1 format as a 4-element integer array: [top-left row, top-left col, bottom-right row, bottom-right col]
*/ */
public static function range2coord($range) public static function range2coord($range)
{ {
@ -1216,7 +1216,7 @@ class SimpleXLSXGen
} }
/** /**
* Convert R1C1 range reference style (row/col 1-based) to A1 range reference style * Convert R1C1 range reference format (row/col 1-based) to A1 range reference format
* *
* Possible parameters: * Possible parameters:
* top-left row number, top-left column number, bottom-right row number, bottom-right column number * top-left row number, top-left column number, bottom-right row number, bottom-right column number
@ -1239,11 +1239,12 @@ class SimpleXLSXGen
* Change or add cell style parameters without losing the original content or previously established styles * Change or add cell style parameters without losing the original content or previously established styles
* *
* @param string $data Data matrix (2-dim 0-based array) where style will be modified. Passed by reference. * @param string $data Data matrix (2-dim 0-based array) where style will be modified. Passed by reference.
* @param string|array $cell Cell to modify in A1 format (string) or R1C1 format (2 element array) * @param string|array $cell Cell to modify in A1 format (string) or R1C1 format (2 integer elements array)
* @param array $style Associative array with style attributes name/value pairs to change/add * @param array $style Associative array with style attributes name/value pairs to change/add. You can use wildcard for
* specific borders to retain original value.
* @param integer $edge Optional. If border attribute specified in $style, this parameter indicates on which edge is applied. * @param integer $edge Optional. If border attribute specified in $style, this parameter indicates on which edge is applied.
* The unspecified edges retain their original value (or 'none' if not specified). * The unspecified edges retain their original value (or 'none' if not specified).
* Default: all edges. You can combine (adding or oring) the constants A_TOP, A_RIGHT, A_BOTTOM, A_LEFT * Default: all edges. You can combine (adding or or-ing) the constants A_TOP, A_RIGHT, A_BOTTOM, A_LEFT
* @return void * @return void
*/ */
public static function setCellStyle(&$data, $cell, $style, $edge = self::A_DEFAULT) public static function setCellStyle(&$data, $cell, $style, $edge = self::A_DEFAULT)
@ -1252,7 +1253,7 @@ class SimpleXLSXGen
if (!array_key_exists($cell[0] - 1, $data) || !array_key_exists($cell[1] - 1, $data[$cell[0] - 1])) return;//quit if cell doesn't exist if (!array_key_exists($cell[0] - 1, $data) || !array_key_exists($cell[1] - 1, $data[$cell[0] - 1])) return;//quit if cell doesn't exist
if ($edge === self::A_DEFAULT) $edge = self::A_TOP + self::A_RIGHT + self::A_BOTTOM + self::A_LEFT; if ($edge === self::A_DEFAULT) $edge = self::A_TOP + self::A_RIGHT + self::A_BOTTOM + self::A_LEFT;
//border processing //border processing
if (array_key_exists('border', $style) && $edge !== (self::A_TOP + self::A_RIGHT + self::A_BOTTOM + self::A_LEFT)) { if (array_key_exists('border', $style)) {
//get original border as 4 element array //get original border as 4 element array
$oldBorder = self::getTagAttributes($data[$cell[0] - 1][$cell[1] - 1], 'style', 'border'); $oldBorder = self::getTagAttributes($data[$cell[0] - 1][$cell[1] - 1], 'style', 'border');
if (empty($oldBorder)) { if (empty($oldBorder)) {
@ -1266,11 +1267,11 @@ class SimpleXLSXGen
if (count($border) == 1) $border = [$border[0], $border[0], $border[0], $border[0]]; if (count($border) == 1) $border = [$border[0], $border[0], $border[0], $border[0]];
//new border copied from old one //new border copied from old one
$newBorder = $oldBorder; $newBorder = $oldBorder;
//set border following $edge //set border as indicated by $edge, except when $border has wildcard
if ($edge & self::A_TOP) $newBorder[0] = $border[0]; if ($edge & self::A_TOP && $border[0] != '*') $newBorder[0] = $border[0];
if ($edge & self::A_RIGHT) $newBorder[1] = $border[1]; if ($edge & self::A_RIGHT && $border[1] != '*') $newBorder[1] = $border[1];
if ($edge & self::A_BOTTOM) $newBorder[2] = $border[2]; if ($edge & self::A_BOTTOM && $border[2] != '*') $newBorder[2] = $border[2];
if ($edge & self::A_LEFT) $newBorder[3] = $border[3]; if ($edge & self::A_LEFT && $border[3] != '*') $newBorder[3] = $border[3];
//implode to the new border //implode to the new border
$style['border'] = implode(' ', $newBorder); $style['border'] = implode(' ', $newBorder);
} }
@ -1280,9 +1281,9 @@ class SimpleXLSXGen
/** /**
* Apply style to a sheet range. Border style are only applied to edges of range. * Apply style to a sheet range. Border style are only applied to edges of range.
* *
* @param array $data Data matrix (2-dim 0-based array) where style will be modified. Passed by reference. * @param array $data Data matrix (2-dim 0-based array) where style will be modified. Passed by reference.
* @param string|array $range Range to modify in A1 format (string) or R1C1 format (4 element array) * @param string|array $range Range to modify in A1 format (string) or R1C1 format (4-element integer array)
* @param array $style Associative array with style attributes name/value pairs to change * @param array $style Associative array with style attributes name/value pairs to change
* *
* @return void * @return void
*/ */
@ -1313,11 +1314,11 @@ class SimpleXLSXGen
* Get tag attributes from a HTML-style tag as an associative array with attributes name/value pairs. * Get tag attributes from a HTML-style tag as an associative array with attributes name/value pairs.
* If $attribute specified, get value of the specified attribute. * If $attribute specified, get value of the specified attribute.
* *
* @param string $str Text to extract tag attributes * @param string $str Text to extract tag attributes
* @param string $tag The tag to look for * @param string $tag The tag to look for
* @param string $attribute Optional. Get the value of specified attribute. Default: get all attributes name/value as an array. * @param string $attribute Optional. Get the value of specified attribute. Default: get all attributes name/value as an array.
*
* @return array|string Attributes name/value as an array ($attribute == '') or attribute value as a string ($attribute != ''). * @return array|string Attributes name/value as an array ($attribute == '') or attribute value as a string ($attribute != '').
*/ */
public static function getTagAttributes($str, $tag, $attribute = '') public static function getTagAttributes($str, $tag, $attribute = '')
{ {
@ -1340,14 +1341,15 @@ class SimpleXLSXGen
/** /**
* Change HTML-style tag attributes. If tag doesn't exist, is created surrounding the previous content. * Change HTML-style tag attributes. If tag doesn't exist, is created surrounding the previous content.
* *
* @param string $str Text to modify * @param string $str Text to modify
* @param string $tag Tag in text to modify * @param string $tag Tag in text to modify
* @param array $attributes Associative array with attribute name/value pairs * @param array $attributes Associative array with attribute name/value pairs
* *
* @return void * @return string Modified text
*/ */
public static function setTagAttributes($str, $tag, $attributes) public static function setTagAttributes($str, $tag, $attributes)
{ {
if (empty($attributes)) return $str;
$opening = "<{$tag}"; $opening = "<{$tag}";
if (preg_match("/(<{$tag}\s*[^>]*)(>.*<\/{$tag}>)/i", $str, $m)) { if (preg_match("/(<{$tag}\s*[^>]*)(>.*<\/{$tag}>)/i", $str, $m)) {
$stropen = $m[1]; $stropen = $m[1];