123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- class PHPExcel_Shared_Excel5
- {
-
- public static function sizeCol($sheet, $col = 'A')
- {
-
- $font = $sheet->getParent()->getDefaultStyle()->getFont();
- $columnDimensions = $sheet->getColumnDimensions();
-
- if (isset($columnDimensions[$col]) and $columnDimensions[$col]->getWidth() != -1) {
-
- $columnDimension = $columnDimensions[$col];
- $width = $columnDimension->getWidth();
- $pixelWidth = PHPExcel_Shared_Drawing::cellDimensionToPixels($width, $font);
- } elseif ($sheet->getDefaultColumnDimension()->getWidth() != -1) {
-
- $defaultColumnDimension = $sheet->getDefaultColumnDimension();
- $width = $defaultColumnDimension->getWidth();
- $pixelWidth = PHPExcel_Shared_Drawing::cellDimensionToPixels($width, $font);
- } else {
-
- $pixelWidth = PHPExcel_Shared_Font::getDefaultColumnWidthByFont($font, true);
- }
-
- if (isset($columnDimensions[$col]) and !$columnDimensions[$col]->getVisible()) {
- $effectivePixelWidth = 0;
- } else {
- $effectivePixelWidth = $pixelWidth;
- }
- return $effectivePixelWidth;
- }
-
- public static function sizeRow($sheet, $row = 1)
- {
-
- $font = $sheet->getParent()->getDefaultStyle()->getFont();
- $rowDimensions = $sheet->getRowDimensions();
-
- if (isset($rowDimensions[$row]) and $rowDimensions[$row]->getRowHeight() != -1) {
-
- $rowDimension = $rowDimensions[$row];
- $rowHeight = $rowDimension->getRowHeight();
- $pixelRowHeight = (int) ceil(4 * $rowHeight / 3);
- } elseif ($sheet->getDefaultRowDimension()->getRowHeight() != -1) {
-
- $defaultRowDimension = $sheet->getDefaultRowDimension();
- $rowHeight = $defaultRowDimension->getRowHeight();
- $pixelRowHeight = PHPExcel_Shared_Drawing::pointsToPixels($rowHeight);
- } else {
-
- $pointRowHeight = PHPExcel_Shared_Font::getDefaultRowHeightByFont($font);
- $pixelRowHeight = PHPExcel_Shared_Font::fontSizeToPixels($pointRowHeight);
- }
-
- if (isset($rowDimensions[$row]) and !$rowDimensions[$row]->getVisible()) {
- $effectivePixelRowHeight = 0;
- } else {
- $effectivePixelRowHeight = $pixelRowHeight;
- }
- return $effectivePixelRowHeight;
- }
-
- public static function getDistanceX(PHPExcel_Worksheet $sheet, $startColumn = 'A', $startOffsetX = 0, $endColumn = 'A', $endOffsetX = 0)
- {
- $distanceX = 0;
-
- $startColumnIndex = PHPExcel_Cell::columnIndexFromString($startColumn) - 1;
- $endColumnIndex = PHPExcel_Cell::columnIndexFromString($endColumn) - 1;
- for ($i = $startColumnIndex; $i <= $endColumnIndex; ++$i) {
- $distanceX += self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($i));
- }
-
- $distanceX -= (int) floor(self::sizeCol($sheet, $startColumn) * $startOffsetX / 1024);
-
- $distanceX -= (int) floor(self::sizeCol($sheet, $endColumn) * (1 - $endOffsetX / 1024));
- return $distanceX;
- }
-
- public static function getDistanceY(PHPExcel_Worksheet $sheet, $startRow = 1, $startOffsetY = 0, $endRow = 1, $endOffsetY = 0)
- {
- $distanceY = 0;
-
- for ($row = $startRow; $row <= $endRow; ++$row) {
- $distanceY += self::sizeRow($sheet, $row);
- }
-
- $distanceY -= (int) floor(self::sizeRow($sheet, $startRow) * $startOffsetY / 256);
-
- $distanceY -= (int) floor(self::sizeRow($sheet, $endRow) * (1 - $endOffsetY / 256));
- return $distanceY;
- }
-
- public static function oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height)
- {
- list($column, $row) = PHPExcel_Cell::coordinateFromString($coordinates);
- $col_start = PHPExcel_Cell::columnIndexFromString($column) - 1;
- $row_start = $row - 1;
- $x1 = $offsetX;
- $y1 = $offsetY;
-
- $col_end = $col_start;
- $row_end = $row_start;
-
- if ($x1 >= self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_start))) {
- $x1 = 0;
- }
- if ($y1 >= self::sizeRow($sheet, $row_start + 1)) {
- $y1 = 0;
- }
- $width = $width + $x1 -1;
- $height = $height + $y1 -1;
-
- while ($width >= self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_end))) {
- $width -= self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_end));
- ++$col_end;
- }
-
- while ($height >= self::sizeRow($sheet, $row_end + 1)) {
- $height -= self::sizeRow($sheet, $row_end + 1);
- ++$row_end;
- }
-
-
- if (self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_start)) == 0) {
- return;
- }
- if (self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_end)) == 0) {
- return;
- }
- if (self::sizeRow($sheet, $row_start + 1) == 0) {
- return;
- }
- if (self::sizeRow($sheet, $row_end + 1) == 0) {
- return;
- }
-
- $x1 = $x1 / self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_start)) * 1024;
- $y1 = $y1 / self::sizeRow($sheet, $row_start + 1) * 256;
- $x2 = ($width + 1) / self::sizeCol($sheet, PHPExcel_Cell::stringFromColumnIndex($col_end)) * 1024;
- $y2 = ($height + 1) / self::sizeRow($sheet, $row_end + 1) * 256;
- $startCoordinates = PHPExcel_Cell::stringFromColumnIndex($col_start) . ($row_start + 1);
- $endCoordinates = PHPExcel_Cell::stringFromColumnIndex($col_end) . ($row_end + 1);
- $twoAnchor = array(
- 'startCoordinates' => $startCoordinates,
- 'startOffsetX' => $x1,
- 'startOffsetY' => $y1,
- 'endCoordinates' => $endCoordinates,
- 'endOffsetX' => $x2,
- 'endOffsetY' => $y2,
- );
- return $twoAnchor;
- }
- }
|