Get calculated height of a PHPSpreadsheet row - php

I'm trying to get the calculated height of a row in PHPSpreadsheet but whenever I do something like
$spreadsheet->getActiveSheet()->getRowDimension($row)->getRowHeight();
I always get a -1 value which is correct based on their documentation as that is the default value. I need to get the calculated height in pt though as I need to do some calculations. I have a cell that is set to wrap text, and spans multiple lines. Anyone knows how to get the calculated height of a row?

-1 is the value of auto height. Means the value will calculated automatically. If you need a number of height. I would suggest to calc the height over the value of the cell. You must do following: Find newlines in the content and multiply them with 12.75 pts (standard height). Some informations you can find here to.

Related

Adding Percentage Number Format in PHP Excel

When I set format of a cell as %, it automatically multiplies the value with 100 & shows the value. So 94 becomes 9400%.
I want to apply % format on a column but want to keep the values same as before. I just want % sign to be shown along with the value. My Value should be 94% . To achieve this, I used following format code
$this->_xls_current_sheet->getStyle($cell_coordinates)->getNumberFormat()->applyFromArray(
array(
'code' => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE
));
If you store 0.5 in a cell, and format it as a percentage, then it will appear as 50%.... if you store 50 in a cell, and format it as a percentage, then it will appear as 5000% that is how it should be.
If you enter 50 as a number in the MS Excel GUI, and then set the format mask manually to a percentage, you will get a displayed value of 5000%. However, if you enter 50% in the MS Excel GUI, then MS Excel divides the value by 100 to store 0.5 in the cell, and formats it as a percentage (two actions in one step). If you set 50% in this way in MS Excel, and then change the format mask from percentage to number, then you will see that MS Excel has done this division for you.
PHPExcel doesn't do this (by default)... it expects you to set the correct value and the format as two separate steps. So PHPExcel expects you to store the correct value in the cell (0.5) in the first place, when you want to format it as a percentage.... it will not magically change the value in any way, it doesn't multiply it at all.
Note that if you are using the advanced value binder (rather than the default value binder), and set the cell value to 50%, then PHPExcel will divide by 100 and set to a percentage automatically, in the same way as the MS Excel GUI.

PHPExcel get cell from x and y

I'm making a program and i'm using PHPExcel. Inside the program you can select width and height. (width is the green row, and blue is the height)
All the white numbers are prices.
Lets say i insert 1400 width and 1600 height. the price should be 640. How can i achieve this?
The easiest option is to find which column contains 1400 in Row 1 (D in your example), and which row contains 1600 in column A (5 in your example), and then use that to read the value at that cell (D5)
A somewhat cleverer way is to use an Excel Formula:
=INDIRECT(CONCATENATE(SUBSTITUTE(ADDRESS(1,MATCH(1400,A1:E1,0),4),1,""),MATCH(1600,A1:A17,0)))
substituting the appropriate lookup values for 1400 and 1600, and adjusting the MATCH ranges to the full size for your width and height lookups.... In MS Excel itself, you could use row and column ranges (A:A) and (1:1), but the PHPExcel calculation engine won't handle row and column ranges correctly, but it will work with cell ranges
As an explanation for the above formula:
MATCH(1400,A1:E1,0)
Does a lookup for value 1400 in the cell range A1:E1 (looking for a match for your width). The returned value for MATCH() is a numeric offset in that row for the column where it is found. In this case, it will return 4.
SUBSTITUTE(ADDRESS(1,<4>,4),1,"")
Converts the column number to a column letter... e.g converts 4 to D. if your range didn't start at A, then you'd have to add an offset to get the correct letter
MATCH(1600,A1:A17,0)
Similar to the previous MATCH, but this time searching in your heights column range (A1:A17) for the value 1600. It will return the offset within that range where it finds the value. Again, if your range doesn't start in row 1, then you'd need to add an offset. In your case, it will return 5.
CONCATENATE(<D>,<5>)
Concatenates the calculated column and row values together to give a cell address (D5)
INDIRECT(<D5>)
Reads the value at the cell address that we've calculated (D5) giving the value 640

PHPExcel Orphan/Widow, getRowDimension

I am having an issue with saving a PDF file from PHPExcel and it creating widow/orphans with my data.
I know of the function to create page breaks,
$objPHPExcel->getActiveSheet()->setBreak( 'A10' , PHPExcel_Worksheet::BREAK_ROW );
but I would like to know if there is a function to know the current height of a range of cells.
I have found references to "getRowDimension" in functions like
$objPHPExcel->getActiveSheet()->getRowDimension('10')->setRowHeight(100);
but I cannot find any documentation on that function. Does anyone know if it can calculate the height of the chosen row and/or take a range of rows?
Also, is there a function to calculate the usable space between the header and footer of each page? Or should I calculate that from the page margin?
Thank you,
Nick
$objPHPExcel->getActiveSheet()
->getRowDimension('10')
->getRowHeight();
Will return the row height for the specified row. This is the height in "points", where 1 point is the equivalent of about 1/72 of an inch (or about 0.35mm). A value of 0 indicates a hidden row; while a value of -1 is the default value, which is 12.75pt (about 1/6 of an inch).
The PHPExcel_Shared_Drawing class contains a number of functions for converting between points and other units of measure, such as pointsToPixels() and pixelsToPoints(), and pixelsToEMU() and EMUToPixels() (English Metric Units).
The whole issue of Microsoft's units of measure is really a confusing minefield, but there's a useful blog post here discussing the different units of measure use in MS Office

Break table if height is greater than value

I'm working on a script that generates a PHP table containing some values from a MySQL database. The script will be displayed on a TV, so I need to break it if its height is greater than 1280 (I've got this value by completing it with values 'till it fits perfect, then I've used jQuery alert to display its height) and I will use jquery.cycle.all plugin to create a slideshow from the tables.
My solution was to use a PHP variable and increment it on each generated then if I have let's say 13 rows, it echoes </table><table>, but the cells must have an exact width so it may add a new line of text (if I have a longer text) on a cell and the table will have more than 1280px height.
Does anybody have any idea about how I can solve this?
Possible solutions:
Use the css overflow: hidden to prevent content going to the next line and just hide it
Use javascript to iterate over each row, calculate the height of the row and remove rows if the total height exceeds 1280px.
Use PHP to calculate the length of the text in actual px and how many rows would be used, but that is quite complex.

php_excel07- how to fix min heigth and mak to grow height based on the contents of a cell

I have to Export data to xlsx, where the exported file should be in a pre defined format. Becoz of these i am using "PHPexcel". And i created a template of some format with each row with some specified height.
Actually in each cell i will be writing the data dynamically. so if the text is larger, then the row height in not increased.
So is ther anyway so tat row height is increased to fit the cell text.
To set a row to autofit:
$objPHPExcel->getActiveSheet()->getRowDimension(10)->setAutoSize(true);
And then set the individual cells to allow wrapping
$objPHPExcel->getActiveSheet()->getStyle('A10:D10')->getAlignment()->setWrapText(true);

Categories