1 | <?php |
||
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) |
|
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) |
|
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) |
|
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) |
|
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.