PHPExcel get cell from x and y - php

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

Related

Get calculated height of a PHPSpreadsheet row

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.

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.

How do I get decimal values?

I have two columns on my database, sickleave and vacationleave. It has to be a decimal but for some reason I only get the whole number. For example, I input 1.50, when I press update I get the value 2. I tried setting the column type to decimal but I still get the same result.
Here's my database schema:
From the mysql documentation:
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments are as follows:
M is the maximum number of digits (the precision). It has a range of 1 to 65.
D is the number of digits to the right of the decimal point (the
scale). It has a range of 0 to 30 and must be no larger than M.
So in your case: hit change button there and in the Length/value column there should be: 11,2 with no parentheses or anything else when you hit save
In case you want to do it manually there's a discussion of it here: MySQL - How do I update the decimal column to allow more digits?
And here's the alter: ALTER TABLE YourTableName MODIFY COLUMN column_name DECIMAL(11,2);
Tried altering it manually using:
ALTER TABLE YourTableName MODIFY COLUMN price DECIMAL(4,2);
Thanks #user3647971

merge adjacent html table cells based on class or value

I have a html table with a variable number of cells.
For the sake of this example i will say 20x20 (although it's quite bigger).
This is generated via php and the dataset to populate the table has been pulled from mysql.
Each one of these cells has a numeric value, and it needs to specifically be placed where is is. So if cell A(10,15) has a value of 100, that 100 needs to specifically be on 10,15-
And while the table is being generated i have no way to analyze the positioning.
Now, many of these cells, have other adjacent cells with the same value. Either horizontally or vertically.
What i need to do is merge adjacent cells of a this table that have the same value.
This could be horizontal, vertical, or both, but still keeping it a rectangle- Nothing too funky.
For example if i have
0 1 1 1 0
2 2 5 0 4
5 5 5 1 4
i need to modify the colspan and/or rowspan based on value-
To be noted is that upon generation i can actually define classes or ids for each one
of these cells. Also during generation i can identify wether there will be more than one in a series in a row, but i have no way of knowing wether there will be one on the row below.
ps: i did do a bit a research and found this thread.
Complex table merging javascript & jquery algorithm
modified the jsfiddle example to affect both colspan and rowspan of the cell but it seems to flip out when it needs to merge more that two cells-
What could be a suggested approach on the matter?
Thank you in advance
Do it in two passes (O(n2)), first traverse your array in the width and detect the number of adjacent cells (and set zero for a cell after the same one) :
0(1) 1(3) 1(0) 1(0) 0(1)
2(2) 2(0) 5(1) 0(1) 4(1)
5(3) 5(0) 5(0) 1(1) 4(1)
Then, traverse it vertically, and find cells where both numbers are the same (and the count > 0), you'll get :
0(1,1) 1(3,1) 1(0,0) 1(0,0) 0(1,1)
2(2,1) 2(0,0) 5(1,0) 0(1,1) 4(1,2)
5(3,1) 5(0,0) 5(0,0) 1(1,1) 4(1,0)
Now, the first number of the pair is the colspan, the second is the rowspan. If one of the number is 0, don't output it.
0 1---- 0
2-- 5 0 4
5---- 1 |

PCharts labeling X-axis

Would it be possible to just label the x axis by the minimum value and the maximum value instead of having values inbetween?
More info on what I'm graphing:
I have a large dataset of dates that I am graphing. Currently I am scaling it to a scale of 0-100 to graph it. I just want to label 0 as the lowest date and 100 as the highest date.
Look at setFixedScale()
$Test->setFixedScale(0,100,1);
That will show 0 as the min and then 100 as the max

Categories