This repository was archived by the owner on May 26, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 639
/
Copy pathWorksheet.php
226 lines (197 loc) · 5.34 KB
/
Worksheet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace Box\Spout\Writer\Common\Entity;
/**
* Class Worksheet
* Entity describing a Worksheet
*/
class Worksheet
{
/** @var string Path to the XML file that will contain the sheet data */
private $filePath;
/** @var resource|null Pointer to the sheet data file (e.g. xl/worksheets/sheet1.xml) */
private $filePointer;
/** @var Sheet The "external" sheet */
private $externalSheet;
/** @var int Maximum number of columns among all the written rows */
private $maxNumColumns;
/** @var int Index of the last written row */
private $lastWrittenRowIndex;
/** @var array Array of the column widths */
protected $columnWidths;
/** @var int Width calculation style */
protected $widthCalcuationStyle;
/** @var int Fixed sheet width for fixed width calculation style */
protected $fixedSheetWidth;
public const W_FULL = 1;
public const W_FIXED = 2;
public const W_NONE = 0;
public const DEFAULT_COL_WIDTH = 30;
public const DEFAULT_FIXED_WIDTH = 320;
/**
* Worksheet constructor.
*
* @param string $worksheetFilePath
* @param Sheet $externalSheet
*/
public function __construct($worksheetFilePath, Sheet $externalSheet)
{
$this->filePath = $worksheetFilePath;
$this->filePointer = null;
$this->externalSheet = $externalSheet;
$this->maxNumColumns = 0;
$this->lastWrittenRowIndex = 0;
$this->columnWidths = [];
$this->widthCalcuationStyle = 0;
}
/**
* @return string
*/
public function getFilePath()
{
return $this->filePath;
}
/**
* @return resource
*/
public function getFilePointer()
{
return $this->filePointer;
}
/**
* @param resource $filePointer
*/
public function setFilePointer($filePointer)
{
$this->filePointer = $filePointer;
}
/**
* @return Sheet
*/
public function getExternalSheet()
{
return $this->externalSheet;
}
/**
* @return int
*/
public function getMaxNumColumns()
{
return $this->maxNumColumns;
}
/**
* @return array
*/
public function getColumnWidths()
{
return $this->columnWidths;
}
/**
* Gets the calculated max column width for the specified index
* @param int $zeroBasedIndex
* @return int
*/
public function getMaxColumnWidth($zeroBasedIndex)
{
if (isset($this->columnWidths[$zeroBasedIndex])) {
return $this->columnWidths[$zeroBasedIndex];
}
$this->columnWidths[$zeroBasedIndex] = self::DEFAULT_COL_WIDTH;
return $this->columnWidths[$zeroBasedIndex];
}
/**
* Sets the calculated max column width for the specified index
* @param int $zeroBasedIndex
* @param int $value Value to set to
* @return void
*/
public function setMaxColumnWidth($zeroBasedIndex, $value)
{
$curSize = $this->columnWidths[$zeroBasedIndex] ?? 0;
if ($curSize < $value) {
$this->columnWidths[$zeroBasedIndex] = $value;
}
}
/**
* Automatically calculates and sets the max column width for the specified cell
* @param Cell $cell The cell
* @param Style $style Row/Cell style
* @param int $zeroBasedIndex of cell
* @return void
*/
public function autoSetWidth($cell, $style, $zeroBasedIndex)
{
$size = 1 + strlen($cell->getValue());//ensure we have at least 1 space
$size *= $style->isFontBold() ? 1.2 : 1.0;
$this->setMaxColumnWidth($zeroBasedIndex, $size);
}
/**
* Gets the fixed sheet width or returns the default if not available
* @return int
*/
public function getFixedSheetWidth()
{
if (!$this->fixedSheetWidth) {
return Worksheet::DEFAULT_FIXED_WIDTH;
}
return $this->fixedSheetWidth;
}
/**
* Sets the fixed sheet width
* @param int $width
* @return void
*/
public function setFixedSheetWidth($width)
{
$this->fixedSheetWidth = $width;
}
/**
* @param int $maxNumColumns
*/
public function setMaxNumColumns($maxNumColumns)
{
$this->maxNumColumns = $maxNumColumns;
}
/**
* Set the with calculation style for this sheet.
* 1=FullExpand,2=FixedWidth,0=None
*
* @return Worksheet Enable method chaining for easy set width
*/
public function setWidthCalculation($widthStyle)
{
$this->widthCalcuationStyle = $widthStyle;
return $this;
}
/**
* Get the with calculation style for this sheet.
* 1=FullExpand,2=FixedWidth,0=None
*
* @return void
*/
public function getWidthCalculation()
{
return $this->widthCalcuationStyle;
}
/**
* @return int
*/
public function getLastWrittenRowIndex()
{
return $this->lastWrittenRowIndex;
}
/**
* @param int $lastWrittenRowIndex
*/
public function setLastWrittenRowIndex($lastWrittenRowIndex)
{
$this->lastWrittenRowIndex = $lastWrittenRowIndex;
}
/**
* @return int The ID of the worksheet
*/
public function getId()
{
// sheet index is zero-based, while ID is 1-based
return $this->externalSheet->getIndex() + 1;
}
}