SUM of dynamic row in phpexcel - phpexcel

I am the biginner of phpexcel I am facing problem to sum up the dynamic row.
Normally the code will be
$objPHPExcel->getActiveSheet()->SetCellValue(AB45,"=SUM(AB25:AB44)" );
how about the dynamic row?? the below is my code
$objPHPExcel->getActiveSheet()->SetCellValue('AB'.$col, "=SUM(AB25:AB".($col).")");
Appreaciate your help~

Related

How to get particular row's column count in PHPExcel?

Im working in codeigniter framework for reading excel sheet datas im using PHPExcel library.
So, In my controller im calling the PHPExcel library function like below :
$reader = PHPExcel_IOFactory::load($targetFile);
$data = $reader->setActiveSheetIndex(0);
$totalrows = $data->getHighestDataRow();
Im getting datas of excel sheet. Im iterating to get particular column values. I want to get each rows column count.
for($i=2; $i<=$totalrows; $i++){
// Here i want to get total column count or column name like A or B
$worksheet->getCellRow($i)->getValue(); // this way i tried but its not working
}

How to call getDefaultRowHeightByFont() function correctly in phpspreadsheet

I am writing to excel row by row and try to get current row height after write to each row.
I tried use getDefaultRowHeightByFont() to get default row height based on font type.
$font_type='times new roman';
$sheet->getDefaultRowHeightByFont(\PhpOffice\PhpSpreadsheet\Style\Font.$font_type)
The row height should be exact value that we get when manually check for row height in excel sheet. But, I got the error
Call to undefined method PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::getDefaultRowHeightByFont()
How to call that function correctly?
Thanks in advance.
Able to make it run.
use PhpOffice\PhpSpreadsheet\Shared\Font as SharedFont;
$default_rowheight=SharedFont::getDefaultRowHeightByFont($spreadsheet->getDefaultStyle()->getFont());
But, this method only show row height for default font style in that sheet. So, we could not find accurate row height for row with different font style from default.

How to add a new column that is based on change of an existing column

I am a beginner of R and I try to use it as much as possible to advance.
I want to add a new column to a existing csv file. This new column is the daily change of the 9th column. I wrote a code as follows:
for (i in nrow(period)) {
period$changeyr3<-period[i+1,9]-period[i,9]
}
changeyr3 is the name of the new column and I got all NAs.
Can you please help me?
Linda
You'll need to do this.
for( i in 1:nrow(period)){
period$changeyr3[i] <- period[i+1,9] - period[i,9]
}
This should work. In what you were doing, you were setting the entire column's values to each time. Also, your last value will still be NA.

Chart doesn't update when adding new rows to an existing Excel table (not without having to use named ranges)

I'm really new to the use of closedXMl and Excel too(at least for this purpose) so sorry if I'm asking silly questions.
I know that closedXML doesn't support charts yet so the only thing that came to mind to get around this was to create my chart using an excel table . That way I thought ClosedXML would update the ranges when I inserted new rows and the chart would pick up on it. Well , it didn't. At least not when I add the rows from code using the closedXML library.
What is curious is that adding new rows from inside excel automatically updates the chart but if I want to get that same result from code, I have to use OFFSET formula along with named ranges and then set the chart source data to these named ranges.
That's why I'd like to know if if there is anything wrong with the code I use to insert new rows:
Dim ruta As String = Server.MapPath("~/Templates/MyTemplate.xlsx")
Dim wb As New XLWorkbook(ruta)
Dim ws = wb.Worksheet(1)
Dim tblData = ws.Table("Table1")
Dim year As Integer = 2000
For i As Integer = 1 To 13
With tblData.DataRange.LastRow()
.Field("Year").SetValue(year)
.Field("Sales").SetValue(CInt(Math.Floor((2000 - 500 + 1) * Rnd())) + 500)
End With
tblData.DataRange.InsertRowsBelow(1)
year = year + 1
Next
tblData.LastRow.Delete()
As you can see the code is very simple and so is the template , that consists of only two columns : "Year"(table1[Year]) and "Sales"(Table1[Sales]
I don't think this has anything to do with my template because as I told you, adding new rows directly from excel works as expected and it is only when I generate the table from code that the chart series doesn't include the new row that were added
Being necessary to manually add the new ranges(Sheet1!Table1[Sales] and Sheet1!Table1[Year]) as it only includes the first row(the one added by default when you insert a table)
Any help would be much appreciated
P.S. Here is a link to a rar containing the full code as well as the excel template(\Templates\MyTemplate.xlsx)
If the problem is that your table doesn't recognise the additional rows, try adding this after the last row delete:
tblData.Resize tblData.Range(1, 1).CurrentRegion
That should resize the table. Then hopefully your table operations should work.

PHPExcel - reads past last row

I'm somewhat perplexed by the behaviour of PHPExcel with some files I have.
The files contain approx. 3000 rows of data, but according to PHPExcel, the last row used is 65535. I tried and cut a few lines from the file and pasted them into a fresh file, to no avail. It doesn't matter whether it is Excel5 or Excel2007 format, always the same result.
Any ideas to find the error here?
CODE:
$cr=$xls->getActiveSheet()->getHighestRow();
$cn=PHPExcel_Cell::columnIndexFromString($xls->getActiveSheet()->getHighestColum‌​n());
for ($z=2; $z<=$cr; $z++) {
$class=($z%2)?"odd":"even";
?>
<tr class="<?php echo $class; ?>">
<?php for ($s=0; $s<$cn; $s++) {
$tmp=$xls->getActiveSheet()->getCellByColumnAndRow($s,$z)->getValue();?>
<td><?php echo $tmp; ?></td>
<?php } ?> </tr>
<?php } ?>
All it takes is a blank cell, and the last col/row can be a lot higher than you expect. Even print layout or styles can falsify the stored highest values. Open the file in MS Excel and press Ctrl-End, and it will take you to the last cell that Excel recognises.
In addition to getHighestRow() and getHighestColum() there's also a (slower) getHighestDataRow() and getHighestDataColumn() method that identify the highest row and column that actually contain cell data.
$objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
or
$objPHPExcel->setActiveSheetIndex(0)->calculateWorksheetDimension();
which returns a range as a string like A1:AC2048
although trailing blank rows and columns are included in these.
or you can use the iterators to loop through the existing rows and columns to get each cell within the worksheets used range. See /Tests/28iterator.php in the production distribution for an example. The iterators can be set to ignore blanks.
Source:
How to find out how many rows and columns to read from an Excel file with PHPExcel?
Mark Baker is right. So I fixed issue at my end by following steps:
do ctrl+end from the first blank cell after last row which have data.
e.g. so if I have last row of data is A3 to D3. I will do ctrl_end in cell A4
This will highlight you the numbers of blank rows
right click on selected rows(rows selected by following first step) and select "Delete".
it will open popup. select "Entire row" and click ok button
So whatever your code you have to load excel data, it will only reads those rows which have data.
This is no error. An "empty" spreadsheet still contains lots of empty cells. Excel usually has 65536 lines and 256 coloumns.

Resources