ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Excel5.php
Go to the documentation of this file.
1 <?php
29 if (!defined('PHPEXCEL_ROOT')) {
33  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
34 }
35 
37 require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
38 
40 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/Drawing.php';
41 
50 {
60  public static function sizeCol($sheet, $col = 'A')
61  {
62  // default font size of workbook
63  $fontSize = $sheet->getParent()->getDefaultStyle()->getFont()->getSize();
64 
65  $columnDimensions = $sheet->getColumnDimensions();
66 
67  // first find the true column width in pixels (uncollapsed and unhidden)
68  if ( isset($columnDimensions[$col]) and $columnDimensions[$col]->getWidth() != -1 ) {
69 
70  // then we have column dimension with explicit width
71  $columnDimension = $columnDimensions[$col];
72  $width = $columnDimension->getWidth();
73  $pixelWidth = PHPExcel_Shared_Drawing::cellDimensionToPixels($width, $fontSize);
74 
75  } else if ($sheet->getDefaultColumnDimension()->getWidth() != -1) {
76 
77  // then we have default column dimension with explicit width
78  $defaultColumnDimension = $sheet->getDefaultColumnDimension();
79  $width = $defaultColumnDimension->getWidth();
80  $pixelWidth = PHPExcel_Shared_Drawing::cellDimensionToPixels($width, $fontSize);
81 
82  } else {
83  $pixelWidth = (int) 64 * $fontSize / 11; // here we interpolate from Calibri 11
84  }
85 
86  // now find the effective column width in pixels
87  if (isset($columnDimensions[$col]) and !$columnDimensions[$col]->getVisible()) {
88  $effectivePixelWidth = 0;
89  } else {
90  $effectivePixelWidth = $pixelWidth;
91  }
92 
93  return $effectivePixelWidth;
94  }
95 
105  public static function sizeRow($sheet, $row = 1)
106  {
107  $rowDimensions = $sheet->getRowDimensions();
108 
109  // first find the true row height in pixels (uncollapsed and unhidden)
110  if ( isset($rowDimensions[$row]) and $rowDimensions[$row]->getRowHeight() != -1) {
111 
112  // then we have a row dimension
113  $rowDimension = $rowDimensions[$row];
114  $rowHeight = $rowDimension->getRowHeight();
115  $pixelRowHeight = (int) ceil(4 * $rowHeight / 3); // here we assume Arial 10
116 
117  } else if ($sheet->getDefaultRowDimension()->getRowHeight() != -1) {
118 
119  // then we have a default row dimension with explicit height
120  $defaultRowDimension = $sheet->getDefaultRowDimension();
121  $rowHeight = $defaultRowDimension->getRowHeight();
122  $pixelRowHeight = PHPExcel_Shared_Drawing::pointsToPixels($rowHeight);
123 
124  } else {
125  $pixelRowHeight = 20; // here we assume Calibri 11
126  }
127 
128  // now find the effective row height in pixels
129  if ( isset($rowDimensions[$row]) and !$rowDimensions[$row]->getVisible() ) {
130  $effectivePixelRowHeight = 0;
131  } else {
132  $effectivePixelRowHeight = $pixelRowHeight;
133  }
134 
135  return $effectivePixelRowHeight;
136  }
137 
149  public static function getDistanceX(PHPExcel_Worksheet $sheet, $startColumn = 'A', $startOffsetX = 0, $endColumn = 'A', $endOffsetX = 0)
150  {
151  $distanceX = 0;
152 
153  // add the widths of the spanning columns
154  $startColumnIndex = PHPExcel_Cell::columnIndexFromString($startColumn) - 1; // 1-based
155  $endColumnIndex = PHPExcel_Cell::columnIndexFromString($endColumn) - 1; // 1-based
156  for ($i = $startColumnIndex; $i <= $endColumnIndex; ++$i) {
157  $distanceX += self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($i));
158  }
159 
160  // correct for offsetX in startcell
161  $distanceX -= (int) floor(self::sizeCol($sheet, $startColumn) * $startOffsetX / 1024);
162 
163  // correct for offsetX in endcell
164  $distanceX -= (int) floor(self::sizeCol($sheet, $endColumn) * (1 - $endOffsetX / 1024));
165 
166  return $distanceX;
167  }
168 
180  public static function getDistanceY(PHPExcel_Worksheet $sheet, $startRow = 1, $startOffsetY = 0, $endRow = 1, $endOffsetY = 0)
181  {
182  $distanceY = 0;
183 
184  // add the widths of the spanning rows
185  for ($row = $startRow; $row <= $endRow; ++$row) {
186  $distanceY += self::sizeRow($sheet, $row);
187  }
188 
189  // correct for offsetX in startcell
190  $distanceY -= (int) floor(self::sizeRow($sheet, $startRow) * $startOffsetY / 256);
191 
192  // correct for offsetX in endcell
193  $distanceY -= (int) floor(self::sizeRow($sheet, $endRow) * (1 - $endOffsetY / 256));
194 
195  return $distanceY;
196  }
197 
250  public static function oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height)
251  {
252  list($column, $row) = PHPExcel_Cell::coordinateFromString($coordinates);
253  $col_start = PHPExcel_Cell::columnIndexFromString($column) - 1;
254  $row_start = $row - 1;
255 
256  $x1 = $offsetX;
257  $y1 = $offsetY;
258 
259  // Initialise end cell to the same as the start cell
260  $col_end = $col_start; // Col containing lower right corner of object
261  $row_end = $row_start; // Row containing bottom right corner of object
262 
263  // Zero the specified offset if greater than the cell dimensions
264  if ($x1 >= self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_start))) {
265  $x1 = 0;
266  }
267  if ($y1 >= self::sizeRow($sheet, $row_start + 1)) {
268  $y1 = 0;
269  }
270 
271  $width = $width + $x1 -1;
272  $height = $height + $y1 -1;
273 
274  // Subtract the underlying cell widths to find the end cell of the image
275  while ($width >= self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_end))) {
276  $width -= self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_end));
277  ++$col_end;
278  }
279 
280  // Subtract the underlying cell heights to find the end cell of the image
281  while ($height >= self::sizeRow($sheet, $row_end + 1)) {
282  $height -= self::sizeRow($sheet, $row_end + 1);
283  ++$row_end;
284  }
285 
286  // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
287  // with zero height or width.
288  if (self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_start)) == 0) {
289  return;
290  }
291  if (self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_end)) == 0) {
292  return;
293  }
294  if (self::sizeRow($sheet, $row_start + 1) == 0) {
295  return;
296  }
297  if (self::sizeRow($sheet, $row_end + 1) == 0) {
298  return;
299  }
300 
301  // Convert the pixel values to the percentage value expected by Excel
302  $x1 = $x1 / self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_start)) * 1024;
303  $y1 = $y1 / self::sizeRow($sheet, $row_start + 1) * 256;
304  $x2 = $width / self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_end)) * 1024; // Distance to right side of object
305  $y2 = $height / self::sizeRow($sheet, $row_end + 1) * 256; // Distance to bottom of object
306 
307  $startCoordinates = PHPExcel_Cell::stringFromColumnIndex($col_start) . ($row_start + 1);
308  $endCoordinates = PHPExcel_Cell::stringFromColumnIndex($col_end) . ($row_end + 1);
309 
310  $twoAnchor = array(
311  'startCoordinates' => $startCoordinates,
312  'startOffsetX' => $x1,
313  'startOffsetY' => $y1,
314  'endCoordinates' => $endCoordinates,
315  'endOffsetX' => $x2,
316  'endOffsetY' => $y2,
317  );
318 
319  return $twoAnchor;
320  }
321 
322 }