Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace OpenSpout\Writer\Common\Entity;
6
 
7
use OpenSpout\Writer\AutoFilter;
8
use OpenSpout\Writer\Common\ColumnWidth;
9
use OpenSpout\Writer\Common\Manager\SheetManager;
10
use OpenSpout\Writer\Exception\InvalidSheetNameException;
11
use OpenSpout\Writer\XLSX\Entity\SheetView;
1441 ariadna 12
use OpenSpout\Writer\XLSX\Options\SheetProtection;
1 efrain 13
 
14
/**
15
 * External representation of a worksheet.
16
 */
17
final class Sheet
18
{
19
    public const DEFAULT_SHEET_NAME_PREFIX = 'Sheet';
20
 
21
    /** @var 0|positive-int Index of the sheet, based on order in the workbook (zero-based) */
22
    private readonly int $index;
23
 
24
    /** @var string ID of the sheet's associated workbook. Used to restrict sheet name uniqueness enforcement to a single workbook */
25
    private readonly string $associatedWorkbookId;
26
 
27
    /** @var string Name of the sheet */
28
    private string $name;
29
 
30
    /** @var bool Visibility of the sheet */
31
    private bool $isVisible;
32
 
33
    /** @var SheetManager Sheet manager */
34
    private readonly SheetManager $sheetManager;
35
 
36
    private ?SheetView $sheetView = null;
37
 
38
    /** @var 0|positive-int */
39
    private int $writtenRowCount = 0;
40
 
41
    private ?AutoFilter $autoFilter = null;
42
 
43
    /** @var ColumnWidth[] Array of min-max-width arrays */
44
    private array $COLUMN_WIDTHS = [];
45
 
46
    /** @var string rows to repeat at top */
47
    private ?string $printTitleRows = null;
48
 
1441 ariadna 49
    private ?SheetProtection $sheetProtection = null;
50
 
1 efrain 51
    /**
52
     * @param 0|positive-int $sheetIndex           Index of the sheet, based on order in the workbook (zero-based)
53
     * @param string         $associatedWorkbookId ID of the sheet's associated workbook
54
     * @param SheetManager   $sheetManager         To manage sheets
55
     */
56
    public function __construct(int $sheetIndex, string $associatedWorkbookId, SheetManager $sheetManager)
57
    {
58
        $this->index = $sheetIndex;
59
        $this->associatedWorkbookId = $associatedWorkbookId;
60
 
61
        $this->sheetManager = $sheetManager;
62
        $this->sheetManager->markWorkbookIdAsUsed($associatedWorkbookId);
63
 
64
        $this->setName(self::DEFAULT_SHEET_NAME_PREFIX.($sheetIndex + 1));
65
        $this->setIsVisible(true);
66
    }
67
 
68
    /**
69
     * @return 0|positive-int Index of the sheet, based on order in the workbook (zero-based)
70
     */
71
    public function getIndex(): int
72
    {
73
        return $this->index;
74
    }
75
 
76
    public function getAssociatedWorkbookId(): string
77
    {
78
        return $this->associatedWorkbookId;
79
    }
80
 
81
    /**
82
     * @return string Name of the sheet
83
     */
84
    public function getName(): string
85
    {
86
        return $this->name;
87
    }
88
 
89
    /**
90
     * Sets the name of the sheet. Note that Excel has some restrictions on the name:
91
     *  - it should not be blank
92
     *  - it should not exceed 31 characters
93
     *  - it should not contain these characters: \ / ? * : [ or ]
94
     *  - it should be unique.
95
     *
96
     * @param string $name Name of the sheet
97
     *
98
     * @throws InvalidSheetNameException if the sheet's name is invalid
99
     */
100
    public function setName(string $name): self
101
    {
102
        $this->sheetManager->throwIfNameIsInvalid($name, $this);
103
 
104
        $this->name = $name;
105
 
106
        $this->sheetManager->markSheetNameAsUsed($this);
107
 
108
        return $this;
109
    }
110
 
111
    /**
112
     * @return bool isVisible Visibility of the sheet
113
     */
114
    public function isVisible(): bool
115
    {
116
        return $this->isVisible;
117
    }
118
 
119
    /**
120
     * @param bool $isVisible Visibility of the sheet
121
     */
122
    public function setIsVisible(bool $isVisible): self
123
    {
124
        $this->isVisible = $isVisible;
125
 
126
        return $this;
127
    }
128
 
129
    /**
130
     * @return $this
131
     */
132
    public function setSheetView(SheetView $sheetView): self
133
    {
134
        $this->sheetView = $sheetView;
135
 
136
        return $this;
137
    }
138
 
139
    public function getSheetView(): ?SheetView
140
    {
141
        return $this->sheetView;
142
    }
143
 
144
    /**
145
     * @internal
146
     */
147
    public function incrementWrittenRowCount(): void
148
    {
149
        ++$this->writtenRowCount;
150
    }
151
 
152
    /**
153
     * @return 0|positive-int
154
     */
155
    public function getWrittenRowCount(): int
156
    {
157
        return $this->writtenRowCount;
158
    }
159
 
160
    /**
161
     * @return $this
162
     */
163
    public function setAutoFilter(?AutoFilter $autoFilter): self
164
    {
165
        $this->autoFilter = $autoFilter;
166
 
167
        return $this;
168
    }
169
 
170
    public function getAutoFilter(): ?AutoFilter
171
    {
172
        return $this->autoFilter;
173
    }
174
 
175
    /**
176
     * @param positive-int ...$columns One or more columns with this width
177
     */
178
    public function setColumnWidth(float $width, int ...$columns): void
179
    {
180
        // Gather sequences
181
        $sequence = [];
182
        foreach ($columns as $column) {
183
            $sequenceLength = \count($sequence);
184
            if ($sequenceLength > 0) {
185
                $previousValue = $sequence[$sequenceLength - 1];
186
                if ($column !== $previousValue + 1) {
187
                    $this->setColumnWidthForRange($width, $sequence[0], $previousValue);
188
                    $sequence = [];
189
                }
190
            }
191
            $sequence[] = $column;
192
        }
193
        $this->setColumnWidthForRange($width, $sequence[0], $sequence[\count($sequence) - 1]);
194
    }
195
 
196
    /**
197
     * @param float        $width The width to set
198
     * @param positive-int $start First column index of the range
199
     * @param positive-int $end   Last column index of the range
200
     */
201
    public function setColumnWidthForRange(float $width, int $start, int $end): void
202
    {
203
        $this->COLUMN_WIDTHS[] = new ColumnWidth($start, $end, $width);
204
    }
205
 
206
    /**
207
     * @internal
208
     *
209
     * @return ColumnWidth[]
210
     */
211
    public function getColumnWidths(): array
212
    {
213
        return $this->COLUMN_WIDTHS;
214
    }
215
 
216
    public function getPrintTitleRows(): ?string
217
    {
218
        return $this->printTitleRows;
219
    }
220
 
221
    public function setPrintTitleRows(string $printTitleRows): void
222
    {
223
        $this->printTitleRows = $printTitleRows;
224
    }
1441 ariadna 225
 
226
    /**
227
     * @return $this
228
     */
229
    public function setSheetProtection(SheetProtection $sheetProtection): self
230
    {
231
        $this->sheetProtection = $sheetProtection;
232
 
233
        return $this;
234
    }
235
 
236
    public function getSheetProtection(): ?SheetProtection
237
    {
238
        return $this->sheetProtection;
239
    }
1 efrain 240
}