In my application, I have a json file which holds products.
In a loop I loop through every item and put them in cells.
$this->row = 4;
foreach ($this->json['products'] as $product) {
$this->template->getActiveSheet()->setCellValueByColumnAndRow(6, $this->row, $product);
$this->row++;
}
Afterwards, I am setting the style of the excel file
$this->template->getActiveSheet()->mergeCells('J6:K6');
Basically merging row J and K with each other but because of this the value of K gets removed. How can I set the value of K to the next row value dynamically after the merge of an inaccessible row?
#jahmic gives answer here
//There is a specific method to do this:
$objPHPExcel->getActiveSheet()->mergeCells('J6:K6');
//You can also use:
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('J6:K6');
//That should do the trick.
Link is
Merge Cell values with PHPExcel - PHP
I made the follow function for get a matrix from a worksheet.
private function getMatrixFromSheet($worksheet){
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell) {
if (!is_null($cell)) {
$matrix[$cell->getRow()][$cell->getColumn()] = $cell->getCalculatedValue();
}
}
}
return $matrix;
}
In this way if I want to access to the second row, second col cell, I can do $matrix[2]['B'].
I need to be able to access to the matrix indexes both as number in order to access to the previous cell as $matrix[2][2].
Is there a way to do this?
I try to convert $cell->getColumn() result in a integer, but unfortunately (int) "a" == 0
If you just want a numeric value for the column, then PHPExcel provides a static helper method to do precisely that
$columnIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn());
Which returns 1 for column A, 2 for column B, 27 for column AA, 28 for column AB, etc
However, you might find that you can simplify your function completely, because PHPExcel also provides the toArray() method to do exactly what you've written this method for:
private function getMatrixFromSheet($worksheet){
return $worksheet->toArray();
}
Note that row/column offsets for the matrix returned by toArray() are 0-based
I use PHPExcel to parse XLSX files. I have found that SUBTOTAL() cells are not ignored when aggregating values with functions such as SUM(). Is there a setting or something that needs to be set to force PHPExcel to not take cells with the SUBTOTAL formula into account when summing over it? Maybe some workaround if this is not possible?
In the end I wrote a function similar to the toArray() function to parse through the Excel sheet and return previously cached values (stored in the "v" tag in the sheet xml). This prevents PHPExcel from trying to calculate cell values. Such a function was not exposed by PHPExcel. Note that it uses the calculated value in the case where the cached value is not available. Practically speaking, the cached value should always be available as the sheet will be generated by Excel, which populates the cached value field.
function getCachedSheetData($sheet)
{
/* Usage
sheet: $excel_obj->getActiveSheet()
*/
// This function is similar to the toArray function, but returns previously cached values to prevent PHPExcel from calculating any formulas
$toreturn = array();
foreach ($sheet->getRowIterator() as $row)
{
$toreturn[] = array();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // This loops all cells, even if it is not set. By default, only cells that are set will be iterated.
foreach ($cellIterator as $cell)
{
if ($cell->getOldCalculatedValue() === null)
{
$toreturn[$cell->getRow()][$cell->getColumn()] = $cell->getValue();
}
else
{
$toreturn[$cell->getRow()][$cell->getColumn()] = $cell->getOldCalculatedValue();
}
}
}
return $toreturn;
}
If covering the full range of subtotals a simple workaround is to accept that all values will be SUMmed (as desired) and then also all the SUBTOTALs too - which should come to the same total anyway, so simply SUM and divide result by 2.
I'm trying to use PHPExcel to create a website that checks consistency of data in excel files.
I manage to load the excels but now I need to pass some checks on the data, and I can't figure out how. For example some of the checks would be:
Count number of empty cells in a specific column
Count number of non integer values in a specific column
Check that a column only has values that are contained in another one of the excels.
Are there some functions that do a kind of count where on the columns?
Or some way to iterate through the values to achieve this goals?
Thank you
Like Mark said, once I used PHPExcel to put the data into arrays I managed to do the checks myself. Here is an example of logging the empty cells of a column:
$n_row=0;
$divisasNull=0;
foreach ($sheetdata as $row) {
$n_column=0;
foreach ($row as $cell) {
if($n_column==6)
{
if($cell==null)
{
echo 'Error. Empty currency at row: '.$n_row.'<br>';
$divisasNull++;
}
}
$n_column++;
}
$n_row++;
}
echo 'Total errors: '.$divisasNull;
This may sound like a silly question and I'm not thinking hard enough,
Or its harder than i think...
Say i have a array of numbers like:
$table = array(
'5','2','1','4','4','4',
'1','2','4','2','1','1',
'3','4','3','1','4','4',
'1','4','2','S','4','4',
'1','2','4','2','1','1',
'5','2','6','4','8','1'
);
S = where I want to get either the row or column based on where "S" is.
I know i can get where S is.
by doing:
$start = array_search('S', $table);
The ''grid'' this array is based on,The S can can be anywhere.
And the grid itself can be different sizes length and width.
How would i go about getting the whole row or column.?
(IE:S is in column 3 : 4,2,1,S,2,4)
(IE:S is in row 3 : 1,4,2,S,4,4)
Just a hint in the right direction would be helpful, Don't need to go all out for me.
Or a idea on how to approach this.
Okay, I guess you have the width you want to use stored somewhere! (And you don't want to use 2-dimensional arrays, check the other answer if you don't care, a 2-dimensional array makes a lot more sense in this case).
If you want to get the line, use $start which is the position of S in the array and divide it by the width. Then you need to round down or up (depends on if you are starting to count at 0 or 1)
For column you need to do something similar, use $start % $width here. (add 1 if you start counting at 1).
Here is a hint:
$table[] = array();
$table[][0] = array(5,2,1,4,5);
$table[][1] = array(1,5,2,3,3);
$table[][2] = array(1,2,'s',4,5);
You will need an array of arrays to have an x/y search.
Your x is the nth element in the outer array.
Your y is the nth element in the inner array.
You can search your string like 's' in array and find column and row of that :
$cols=5; // your columns count
$row=1;
$i=1;
$array = array('1','2','3','4','5','6','7','s','9');
foreach($array as $value)
{
if($value=='s')
{
echo 'Column : '.$i .' || Row : ' .$row ;
}
if($i==$cols)
{
$i=0;
$row++;
}
$i++;
}