1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Shared; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Copyright (c) 2006 - 2016 PhpSpreadsheet. |
10
|
|
|
* |
11
|
|
|
* This library is free software; you can redistribute it and/or |
12
|
|
|
* modify it under the terms of the GNU Lesser General Public |
13
|
|
|
* License as published by the Free Software Foundation; either |
14
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This library is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19
|
|
|
* Lesser General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU Lesser General Public |
22
|
|
|
* License along with this library; if not, write to the Free Software |
23
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
24
|
|
|
* |
25
|
|
|
* @category PhpSpreadsheet |
26
|
|
|
* |
27
|
|
|
* @copyright Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) |
28
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
29
|
|
|
*/ |
30
|
|
|
class Xls |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Get the width of a column in pixels. We use the relationship y = ceil(7x) where |
34
|
|
|
* x is the width in intrinsic Excel units (measuring width in number of normal characters) |
35
|
|
|
* This holds for Arial 10. |
36
|
|
|
* |
37
|
|
|
* @param Worksheet $sheet The sheet |
38
|
|
|
* @param string $col The column |
39
|
|
|
* |
40
|
|
|
* @return int The width in pixels |
41
|
|
|
*/ |
42
|
7 |
|
public static function sizeCol($sheet, $col = 'A') |
43
|
|
|
{ |
44
|
|
|
// default font of the workbook |
45
|
7 |
|
$font = $sheet->getParent()->getDefaultStyle()->getFont(); |
46
|
|
|
|
47
|
7 |
|
$columnDimensions = $sheet->getColumnDimensions(); |
48
|
|
|
|
49
|
|
|
// first find the true column width in pixels (uncollapsed and unhidden) |
50
|
7 |
|
if (isset($columnDimensions[$col]) and $columnDimensions[$col]->getWidth() != -1) { |
|
|
|
|
51
|
|
|
// then we have column dimension with explicit width |
52
|
6 |
|
$columnDimension = $columnDimensions[$col]; |
53
|
6 |
|
$width = $columnDimension->getWidth(); |
54
|
6 |
|
$pixelWidth = Drawing::cellDimensionToPixels($width, $font); |
55
|
7 |
|
} elseif ($sheet->getDefaultColumnDimension()->getWidth() != -1) { |
56
|
|
|
// then we have default column dimension with explicit width |
57
|
1 |
|
$defaultColumnDimension = $sheet->getDefaultColumnDimension(); |
58
|
1 |
|
$width = $defaultColumnDimension->getWidth(); |
59
|
1 |
|
$pixelWidth = Drawing::cellDimensionToPixels($width, $font); |
60
|
|
|
} else { |
61
|
|
|
// we don't even have any default column dimension. Width depends on default font |
62
|
6 |
|
$pixelWidth = Font::getDefaultColumnWidthByFont($font, true); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// now find the effective column width in pixels |
66
|
7 |
|
if (isset($columnDimensions[$col]) and !$columnDimensions[$col]->getVisible()) { |
|
|
|
|
67
|
|
|
$effectivePixelWidth = 0; |
68
|
|
|
} else { |
69
|
7 |
|
$effectivePixelWidth = $pixelWidth; |
70
|
|
|
} |
71
|
|
|
|
72
|
7 |
|
return $effectivePixelWidth; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Convert the height of a cell from user's units to pixels. By interpolation |
77
|
|
|
* the relationship is: y = 4/3x. If the height hasn't been set by the user we |
78
|
|
|
* use the default value. If the row is hidden we use a value of zero. |
79
|
|
|
* |
80
|
|
|
* @param Worksheet $sheet The sheet |
81
|
|
|
* @param int $row The row index (1-based) |
82
|
|
|
* |
83
|
|
|
* @return int The width in pixels |
84
|
|
|
*/ |
85
|
7 |
|
public static function sizeRow($sheet, $row = 1) |
86
|
|
|
{ |
87
|
|
|
// default font of the workbook |
88
|
7 |
|
$font = $sheet->getParent()->getDefaultStyle()->getFont(); |
89
|
|
|
|
90
|
7 |
|
$rowDimensions = $sheet->getRowDimensions(); |
91
|
|
|
|
92
|
|
|
// first find the true row height in pixels (uncollapsed and unhidden) |
93
|
7 |
|
if (isset($rowDimensions[$row]) and $rowDimensions[$row]->getRowHeight() != -1) { |
|
|
|
|
94
|
|
|
// then we have a row dimension |
95
|
1 |
|
$rowDimension = $rowDimensions[$row]; |
96
|
1 |
|
$rowHeight = $rowDimension->getRowHeight(); |
97
|
1 |
|
$pixelRowHeight = (int) ceil(4 * $rowHeight / 3); // here we assume Arial 10 |
98
|
7 |
|
} elseif ($sheet->getDefaultRowDimension()->getRowHeight() != -1) { |
99
|
|
|
// then we have a default row dimension with explicit height |
100
|
2 |
|
$defaultRowDimension = $sheet->getDefaultRowDimension(); |
101
|
2 |
|
$rowHeight = $defaultRowDimension->getRowHeight(); |
102
|
2 |
|
$pixelRowHeight = Drawing::pointsToPixels($rowHeight); |
103
|
|
|
} else { |
104
|
|
|
// we don't even have any default row dimension. Height depends on default font |
105
|
5 |
|
$pointRowHeight = Font::getDefaultRowHeightByFont($font); |
106
|
5 |
|
$pixelRowHeight = Font::fontSizeToPixels($pointRowHeight); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// now find the effective row height in pixels |
110
|
7 |
|
if (isset($rowDimensions[$row]) and !$rowDimensions[$row]->getVisible()) { |
|
|
|
|
111
|
|
|
$effectivePixelRowHeight = 0; |
112
|
|
|
} else { |
113
|
7 |
|
$effectivePixelRowHeight = $pixelRowHeight; |
114
|
|
|
} |
115
|
|
|
|
116
|
7 |
|
return $effectivePixelRowHeight; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the horizontal distance in pixels between two anchors |
121
|
|
|
* The distanceX is found as sum of all the spanning columns widths minus correction for the two offsets. |
122
|
|
|
* |
123
|
|
|
* @param Worksheet $sheet |
124
|
|
|
* @param string $startColumn |
125
|
|
|
* @param int $startOffsetX Offset within start cell measured in 1/1024 of the cell width |
126
|
|
|
* @param string $endColumn |
127
|
|
|
* @param int $endOffsetX Offset within end cell measured in 1/1024 of the cell width |
128
|
|
|
* |
129
|
|
|
* @return int Horizontal measured in pixels |
130
|
|
|
*/ |
131
|
3 |
|
public static function getDistanceX(Worksheet $sheet, $startColumn = 'A', $startOffsetX = 0, $endColumn = 'A', $endOffsetX = 0) |
132
|
|
|
{ |
133
|
3 |
|
$distanceX = 0; |
134
|
|
|
|
135
|
|
|
// add the widths of the spanning columns |
136
|
3 |
|
$startColumnIndex = Cell::columnIndexFromString($startColumn) - 1; // 1-based |
137
|
3 |
|
$endColumnIndex = Cell::columnIndexFromString($endColumn) - 1; // 1-based |
138
|
3 |
|
for ($i = $startColumnIndex; $i <= $endColumnIndex; ++$i) { |
139
|
3 |
|
$distanceX += self::sizeCol($sheet, Cell::stringFromColumnIndex($i)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// correct for offsetX in startcell |
143
|
3 |
|
$distanceX -= (int) floor(self::sizeCol($sheet, $startColumn) * $startOffsetX / 1024); |
144
|
|
|
|
145
|
|
|
// correct for offsetX in endcell |
146
|
3 |
|
$distanceX -= (int) floor(self::sizeCol($sheet, $endColumn) * (1 - $endOffsetX / 1024)); |
147
|
|
|
|
148
|
3 |
|
return $distanceX; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the vertical distance in pixels between two anchors |
153
|
|
|
* The distanceY is found as sum of all the spanning rows minus two offsets. |
154
|
|
|
* |
155
|
|
|
* @param Worksheet $sheet |
156
|
|
|
* @param int $startRow (1-based) |
157
|
|
|
* @param int $startOffsetY Offset within start cell measured in 1/256 of the cell height |
158
|
|
|
* @param int $endRow (1-based) |
159
|
|
|
* @param int $endOffsetY Offset within end cell measured in 1/256 of the cell height |
160
|
|
|
* |
161
|
|
|
* @return int Vertical distance measured in pixels |
162
|
|
|
*/ |
163
|
3 |
|
public static function getDistanceY(Worksheet $sheet, $startRow = 1, $startOffsetY = 0, $endRow = 1, $endOffsetY = 0) |
164
|
|
|
{ |
165
|
3 |
|
$distanceY = 0; |
166
|
|
|
|
167
|
|
|
// add the widths of the spanning rows |
168
|
3 |
|
for ($row = $startRow; $row <= $endRow; ++$row) { |
169
|
3 |
|
$distanceY += self::sizeRow($sheet, $row); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// correct for offsetX in startcell |
173
|
3 |
|
$distanceY -= (int) floor(self::sizeRow($sheet, $startRow) * $startOffsetY / 256); |
174
|
|
|
|
175
|
|
|
// correct for offsetX in endcell |
176
|
3 |
|
$distanceY -= (int) floor(self::sizeRow($sheet, $endRow) * (1 - $endOffsetY / 256)); |
177
|
|
|
|
178
|
3 |
|
return $distanceY; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Convert 1-cell anchor coordinates to 2-cell anchor coordinates |
183
|
|
|
* This function is ported from PEAR Spreadsheet_Writer_Excel with small modifications. |
184
|
|
|
* |
185
|
|
|
* Calculate the vertices that define the position of the image as required by |
186
|
|
|
* the OBJ record. |
187
|
|
|
* |
188
|
|
|
* +------------+------------+ |
189
|
|
|
* | A | B | |
190
|
|
|
* +-----+------------+------------+ |
191
|
|
|
* | |(x1,y1) | | |
192
|
|
|
* | 1 |(A1)._______|______ | |
193
|
|
|
* | | | | | |
194
|
|
|
* | | | | | |
195
|
|
|
* +-----+----| BITMAP |-----+ |
196
|
|
|
* | | | | | |
197
|
|
|
* | 2 | |______________. | |
198
|
|
|
* | | | (B2)| |
199
|
|
|
* | | | (x2,y2)| |
200
|
|
|
* +---- +------------+------------+ |
201
|
|
|
* |
202
|
|
|
* Example of a bitmap that covers some of the area from cell A1 to cell B2. |
203
|
|
|
* |
204
|
|
|
* Based on the width and height of the bitmap we need to calculate 8 vars: |
205
|
|
|
* $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2. |
206
|
|
|
* The width and height of the cells are also variable and have to be taken into |
207
|
|
|
* account. |
208
|
|
|
* The values of $col_start and $row_start are passed in from the calling |
209
|
|
|
* function. The values of $col_end and $row_end are calculated by subtracting |
210
|
|
|
* the width and height of the bitmap from the width and height of the |
211
|
|
|
* underlying cells. |
212
|
|
|
* The vertices are expressed as a percentage of the underlying cell width as |
213
|
|
|
* follows (rhs values are in pixels): |
214
|
|
|
* |
215
|
|
|
* x1 = X / W *1024 |
216
|
|
|
* y1 = Y / H *256 |
217
|
|
|
* x2 = (X-1) / W *1024 |
218
|
|
|
* y2 = (Y-1) / H *256 |
219
|
|
|
* |
220
|
|
|
* Where: X is distance from the left side of the underlying cell |
221
|
|
|
* Y is distance from the top of the underlying cell |
222
|
|
|
* W is the width of the cell |
223
|
|
|
* H is the height of the cell |
224
|
|
|
* |
225
|
|
|
* @param Worksheet $sheet |
226
|
|
|
* @param string $coordinates E.g. 'A1' |
227
|
|
|
* @param int $offsetX Horizontal offset in pixels |
228
|
|
|
* @param int $offsetY Vertical offset in pixels |
229
|
|
|
* @param int $width Width in pixels |
230
|
|
|
* @param int $height Height in pixels |
231
|
|
|
* |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
7 |
|
public static function oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height) |
235
|
|
|
{ |
236
|
7 |
|
list($column, $row) = Cell::coordinateFromString($coordinates); |
237
|
7 |
|
$col_start = Cell::columnIndexFromString($column) - 1; |
238
|
7 |
|
$row_start = $row - 1; |
239
|
|
|
|
240
|
7 |
|
$x1 = $offsetX; |
241
|
7 |
|
$y1 = $offsetY; |
242
|
|
|
|
243
|
|
|
// Initialise end cell to the same as the start cell |
244
|
7 |
|
$col_end = $col_start; // Col containing lower right corner of object |
245
|
7 |
|
$row_end = $row_start; // Row containing bottom right corner of object |
246
|
|
|
|
247
|
|
|
// Zero the specified offset if greater than the cell dimensions |
248
|
7 |
|
if ($x1 >= self::sizeCol($sheet, Cell::stringFromColumnIndex($col_start))) { |
249
|
2 |
|
$x1 = 0; |
250
|
|
|
} |
251
|
7 |
|
if ($y1 >= self::sizeRow($sheet, $row_start + 1)) { |
252
|
|
|
$y1 = 0; |
253
|
|
|
} |
254
|
|
|
|
255
|
7 |
|
$width = $width + $x1 - 1; |
256
|
7 |
|
$height = $height + $y1 - 1; |
257
|
|
|
|
258
|
|
|
// Subtract the underlying cell widths to find the end cell of the image |
259
|
7 |
|
while ($width >= self::sizeCol($sheet, Cell::stringFromColumnIndex($col_end))) { |
260
|
7 |
|
$width -= self::sizeCol($sheet, Cell::stringFromColumnIndex($col_end)); |
261
|
7 |
|
++$col_end; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// Subtract the underlying cell heights to find the end cell of the image |
265
|
7 |
|
while ($height >= self::sizeRow($sheet, $row_end + 1)) { |
266
|
7 |
|
$height -= self::sizeRow($sheet, $row_end + 1); |
267
|
7 |
|
++$row_end; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
// Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell |
271
|
|
|
// with zero height or width. |
272
|
7 |
|
if (self::sizeCol($sheet, Cell::stringFromColumnIndex($col_start)) == 0) { |
273
|
|
|
return; |
274
|
|
|
} |
275
|
7 |
|
if (self::sizeCol($sheet, Cell::stringFromColumnIndex($col_end)) == 0) { |
276
|
|
|
return; |
277
|
|
|
} |
278
|
7 |
|
if (self::sizeRow($sheet, $row_start + 1) == 0) { |
279
|
|
|
return; |
280
|
|
|
} |
281
|
7 |
|
if (self::sizeRow($sheet, $row_end + 1) == 0) { |
282
|
|
|
return; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
// Convert the pixel values to the percentage value expected by Excel |
286
|
7 |
|
$x1 = $x1 / self::sizeCol($sheet, Cell::stringFromColumnIndex($col_start)) * 1024; |
287
|
7 |
|
$y1 = $y1 / self::sizeRow($sheet, $row_start + 1) * 256; |
288
|
7 |
|
$x2 = ($width + 1) / self::sizeCol($sheet, Cell::stringFromColumnIndex($col_end)) * 1024; // Distance to right side of object |
289
|
7 |
|
$y2 = ($height + 1) / self::sizeRow($sheet, $row_end + 1) * 256; // Distance to bottom of object |
290
|
|
|
|
291
|
7 |
|
$startCoordinates = Cell::stringFromColumnIndex($col_start) . ($row_start + 1); |
292
|
7 |
|
$endCoordinates = Cell::stringFromColumnIndex($col_end) . ($row_end + 1); |
293
|
|
|
|
294
|
|
|
$twoAnchor = [ |
295
|
7 |
|
'startCoordinates' => $startCoordinates, |
296
|
7 |
|
'startOffsetX' => $x1, |
297
|
7 |
|
'startOffsetY' => $y1, |
298
|
7 |
|
'endCoordinates' => $endCoordinates, |
299
|
7 |
|
'endOffsetX' => $x2, |
300
|
7 |
|
'endOffsetY' => $y2, |
301
|
|
|
]; |
302
|
|
|
|
303
|
7 |
|
return $twoAnchor; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.