Protected Sheet and WorkBook

+ Protected Sheet
+ Protected WorkBook.
This commit is contained in:
Oleg Kosarev 2023-04-19 18:05:11 -05:00
parent 32276c4486
commit 2231c18f9e
2 changed files with 54 additions and 8 deletions

View File

@ -154,6 +154,27 @@ $xlsx->setAuthor('Sergey Shuchkin <sergey.shuchkin@gmail.com>')
->setDescription('This is Description')
->setCategory('This is Сategory')
->setApplication('Shuchkin\SimpleXLSXGen')
// Set Protected WorkBook Structure
$xlsx->setProtectedWorkBook()
/**
* @TODO Set Prtoected Sheet
* Set Premissions to edit.
* If the premissions are not specified, then Excel treats it as not allowed. Or you can specify all permissions 0 - allowed 1 - not allowed.
* formatCells - Format Cell
* formatColumns - Format Colums
* formatRows - Format - Rows
* insertColumns - Insert Colums
* insertRows - Insert Rows
* insertHyperlinks Insert Hyperlinks
* deleteColumns - Delete Columns
* deleteRows - Delete Rows
* sort - Sort
* autoFilter - Auto Filter
* pivotTables Pivot Table
*/
$xlsx->setProtectedSheet(["formatCells" => 0, "formatColumns" => 0, "formatRows" => 0, "autoFilter" => 1]) // Allowed Format Cell, Format Colums, Format Rows, Not Allowed Auto Filter
```
### JS array to Excel (AJAX)
```php

View File

@ -40,6 +40,8 @@ class SimpleXLSXGen
protected $keywords;
protected $category;
protected $lastModifiedBy;
protected bool $prtoectedWorkbook;
protected array $prtoectedSheet;
const N_NORMAL = 0; // General
const N_INT = 1; // 0
const N_DEC = 2; // 0.00
@ -102,6 +104,9 @@ class SimpleXLSXGen
$this->lastModifiedBy = 'Sergey Shuchkin <sergey.shuchkin@gmail.com>';
$this->application = __CLASS__;
$this->prtoectedWorkbook = false;
$this->prtoectedSheet = [];
$this->curSheet = -1;
$this->defaultFont = 'Calibri';
$this->defaultFontSize = 10;
@ -180,7 +185,7 @@ class SimpleXLSXGen
{SHEETVIEWS}
{COLS}
<sheetData>{ROWS}</sheetData>
{AUTOFILTER}{MERGECELLS}{HYPERLINKS}
{PROTECTED_SHEET}{AUTOFILTER}{MERGECELLS}{HYPERLINKS}
</worksheet>',
'xl/worksheets/_rels/sheet1.xml.rels' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">{HYPERLINKS}</Relationships>',
@ -199,6 +204,7 @@ class SimpleXLSXGen
'xl/workbook.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<fileVersion appName="{APP}"/>
{PROTECTED_WORKBOOK}
<sheets>
{SHEETS}
</sheets>
@ -340,12 +346,16 @@ class SimpleXLSXGen
$this->_writeEntry($fh, $cdrec, $cfilename, $template);
$entries++;
} elseif ($cfilename === 'xl/workbook.xml') {
$prtoectedWorkbook = '<workbookProtection lockStructure="1"/>';
if ($this->prtoectedWorkbook == false) {
$prtoectedWorkbook = '';
}
$s = '';
foreach ($this->sheets as $k => $v) {
$s .= '<sheet name="' . $this->esc($v['name']) . '" sheetId="' . ($k + 1) . '" r:id="rId' . ($k + 1) . '"/>';
}
$search = ['{SHEETS}', '{APP}'];
$replace = [$s, $this->esc($this->application)];
$search = ['{SHEETS}', '{APP}', '{PROTECTED_WORKBOOK}'];
$replace = [$s, $this->esc($this->application), $prtoectedWorkbook];
$template = str_replace($search, $replace, $template);
$this->_writeEntry($fh, $cdrec, $cfilename, $template);
$entries++;
@ -935,6 +945,15 @@ class SimpleXLSXGen
$REF = 'A1:A1';
}
$protectedSheet = '';
if (is_array($this->prtoectedSheet) and count($this->prtoectedSheet) > 0) {
$premissions = 'sheet="1" ';
foreach ($this->prtoectedSheet as $namePremissions => $valuePremissions) {
$premissions .= $namePremissions . '="' . (int)$valuePremissions . '" ';
}
$protectedSheet = '<sheetProtection ' . $premissions . '/>';
}
$AUTOFILTER = '';
if ($this->sheets[$idx]['autofilter']) {
$AUTOFILTER = '<autoFilter ref="' . $this->sheets[$idx]['autofilter'] . '" />';
@ -961,9 +980,8 @@ class SimpleXLSXGen
//restore locale
setlocale(LC_NUMERIC, $_loc);
return str_replace(
['{REF}', '{COLS}', '{ROWS}', '{AUTOFILTER}', '{MERGECELLS}', '{HYPERLINKS}', '{SHEETVIEWS}'],
['{REF}', '{COLS}', '{ROWS}', '{AUTOFILTER}', '{MERGECELLS}', '{HYPERLINKS}', '{SHEETVIEWS}', '{PROTECTED_SHEET}'],
[
$REF,
implode("\r\n", $COLS),
@ -971,7 +989,8 @@ class SimpleXLSXGen
$AUTOFILTER,
implode("\r\n", $MERGECELLS),
implode("\r\n", $HYPERLINKS),
$SHEETVIEWS
$SHEETVIEWS,
$protectedSheet
],
$template
);
@ -1078,9 +1097,15 @@ class SimpleXLSXGen
return $this;
}
public function setLastModifiedBy($lastModifiedBy)
public function setProtectedWorkBook()
{
$this->lastModifiedBy = $lastModifiedBy;
$this->prtoectedWorkbook = true;
return $this;
}
public function setProtectedSheet(array $prtoectedSheet)
{
$this->prtoectedSheet = $prtoectedSheet;
return $this;
}