Skip to content

Commit

Permalink
Sheet::setPrintArea($range): Sheet
Browse files Browse the repository at this point in the history
Sheet::setPrintTopRows($rows): Sheet
Sheet::setPrintLeftColumns($cols): Sheet
Sheet::setPrintGridlines($bool): Sheet
  • Loading branch information
aVadim483 committed Jun 29, 2024
1 parent 38fe6b9 commit a7602e1
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 22 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## New
Sheet::isName($name): bool -- Case-insensitive name checking
## v.5.6
Excel::setActiveSheet($name): Excel -- Set active (default) sheet by case-insensitive name
Sheet::isName($name): bool -- Case-insensitive name checking
Sheet::setPrintArea($range): Sheet
Sheet::setPrintTopRows($rows): Sheet
Sheet::setPrintLeftColumns($cols): Sheet
Sheet::setPrintGridlines($bool): Sheet

## v.5.5

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Jump To:
* [Define Named Ranges](/docs/02-sheets.md#define-named-ranges)
* [Freeze Panes and Autofilter](/docs/02-sheets.md#freeze-panes-and-autofilter)
* [Setting Active Cells](/docs/02-sheets.md#setting-active-cells)
* [Print Settings](/docs/02-sheets.md#print-settings)
* [Writing](/docs/03-writing.md)
* [Writing Row by Row vs Direct](/docs/03-writing.md#writing-row-by-row-vs-direct)
* [Direct Writing To Cells](/docs/03-writing.md#direct-writing-to-cells)
Expand Down
37 changes: 36 additions & 1 deletion docs/02-sheets.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,16 @@ $sheet->writeCell('=Value*Rate');

```

### Setting Active Cells
### Setting Active Sheet and Cells

You can select active (default) sheet in workbook

```php
// Set active (default) sheet by case-insensitive name
$excel->setActiveSheet($name);
```

To select active cell on specified sheet use the following code:

```php
// Selecting one active cell
Expand All @@ -296,4 +305,30 @@ $sheet1->setActiveCell('B2');
$sheet1->setActiveCell('B2:C3');
```

### Print settings

Specify printing area

```php
$sheet->setPrintArea('A2:F3,A8:F10');
```

To repeat specific rows/columns at top/left of a printing page, use the following code:

```php
$sheet->setPrintTopRows('1')->setPrintLeftColumns('A');
```

The following code is an example of how to repeat row 1 to 5 on each printed page:

```php
$sheet->setPrintTopRows('1:5');
```

To show/hide gridlines when printing, use the following code:

```php
$sheet->setPrintGridlines(true);
```

Returns to [README.md](/README.md)
81 changes: 79 additions & 2 deletions src/FastExcelWriter/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class Excel implements InterfaceBookWriter

protected array $bookViews = [];

protected array $definedNames = [];

/** @var bool */
protected bool $isRightToLeft = false;

Expand Down Expand Up @@ -1176,7 +1178,10 @@ public function makeSheet(string $sheetName = null): Sheet
}
$key = mb_strtolower($sheetName);
if (!isset($this->sheets[$key])) {
$this->sheets[$key] = static::createSheet($sheetName);
$sheet = static::createSheet($sheetName);
$sheet->localSheetId = count($this->sheets);
$this->sheets[$key] = $sheet;

$this->sheets[$key]->excel = $this;
$this->sheets[$key]->key = $key;
$this->sheets[$key]->index = count($this->sheets);
Expand Down Expand Up @@ -1238,8 +1243,10 @@ public function getSheet($index = null): ?Sheet
* Removes the first sheet of index omitted
*
* @param int|string|null $index
*
* @return $this
*/
public function removeSheet($index = null): void
public function removeSheet($index = null): Excel
{
if (null === $index) {
array_shift($this->sheets);
Expand All @@ -1259,6 +1266,12 @@ public function removeSheet($index = null): void
}
unset($this->sheets[$key]);
}
$localSheetId = 0;
foreach ($this->sheets as $sheet) {
$sheet->localSheetId = $localSheetId++;
}

return $this;
}

/**
Expand Down Expand Up @@ -1291,6 +1304,70 @@ public function addNamedRange(string $range, string $name): Excel
ExceptionRangeName::throwNew('Sheet name not defined in range address');
}

/**
* @param string $name
* @param string $range
* @param array|null $attributes
*
* @return $this
*/
public function addDefinedName(string $name, string $range, ?array $attributes = []): Excel
{
$attributes = array_replace(['name' => Writer::xmlSpecialChars($name)], $attributes);
if ($name === '_xlnm.Print_Area' && isset($attributes['localSheetId'])) {
// add print area
foreach ($this->definedNames as $key => $definedName) {
if ($definedName['_attr']['name'] === $name && isset($definedName['localSheetId']) && $definedName['localSheetId'] === $attributes['localSheetId']) {
$this->definedNames[$key]['_value'] .= $range;
return $this;
}
}
$this->definedNames[] = [
'_value' => $range,
'_attr' => $attributes,
];
}
elseif ($name === '_xlnm.Print_Titles' && isset($attributes['localSheetId'])) {
// set print title
foreach ($this->definedNames as $key => $definedName) {
if ($definedName['_attr']['name'] === $name && isset($definedName['localSheetId']) && $definedName['localSheetId'] === $attributes['localSheetId']) {
unset($this->definedNames[$key]);
}
}
$this->definedNames[] = [
'_value' => $range,
'_attr' => $attributes,
];
}
else {
$this->definedNames[$name] = [
'_value' => $range,
'_attr' => $attributes,
];
}

return $this;
}

/**
* @return array
*/
public function getDefinedNames(): array
{
$result = $this->definedNames;
foreach ($this->sheets as $sheet) {
if ($sheet->absoluteAutoFilter) {
$filterRange = $sheet->absoluteAutoFilter . ':' . Excel::cellAddress($sheet->rowCountWritten, $sheet->colCountWritten, true);
$fullAddress = "'" . $sheet->sanitizedSheetName . "'!" . $filterRange;
$result[] = [
'_value' => $fullAddress,
'_attr' => ['name' => '_xlnm._FilterDatabase', 'localSheetId' => $sheet->localSheetId, 'hidden' => '1'],
];
}
}
return $result;
}

/**
* @param string $imageBlob
*
Expand Down
Loading

0 comments on commit a7602e1

Please sign in to comment.