How to get particular row's column count in PHPExcel? - 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
}

Related

SQLite3 split date while creating index

I'm using a SQLite3 database, and I have a table that looks like this:
The database is quite big and running queries is very slow. I'm trying to speed up the process by indexing some of the columns. One of the columns that I want to index is the QUOTE_DATETIME column.
Problem: I want to index by date (YYYY-MM-DD) only, not by date and time (YYYY-MM-DD HH:MM:SS), which is the data I currently have in QUOTE_DATETIME.
Question: How can I use CREATE INDEX to create an index that uses only dates in the format YYYY-MM-DD? Should I split QUOTE_DATETIME into 2 columns: QUOTE_DATE and QUOTE_TIME? If so, how can I do that? Is there an easier solution?
Thanks for helping! :D
Attempt 1: I tried running CREATE INDEX id ON DATA (date(QUOTE_DATETIME)) but I got the error Error: non-deterministic functions prohibited in index expressions.
Attempt 2: I ran ALTER TABLE data ADD COLUMN QUOTE_DATE TEXT to create a new column to hold the date only. And then INSERT INTO data(QUOTE_DATE) SELECT date(QUOTE_DATETIME) FROM data. The date(QUOTE_DATETIME) should convert the date + time to only date, and the INSERT INTO should add the new values to QUOTE_DATE. However, it doesn't work and I don't know why. The new column ends up not having anything added to it.
Expression indexes must not use functions that might change their return value based on data not mentioned in the function call itself. The date() function is such a function because it might use the current time zone setting.
However, in SQLite 3.20 or later, you can use date() in indexes as long as you are not using any time zone modifiers.
INSERT adds new rows. To modify existing rows, use UPDATE:
UPDATE Data SET Quote_Date = date(Quote_DateTime);

Merging two tables and returning value through r script

I am attempting to add a dynamic column to a table in spotfire that is updated using r-script/data functions in order to handle different variable types. When you just insert columns, it does not allow you to change the column from a text value to a string value.
The basic code structure is create a new table by merging the base table with the information table, select a column header to populate the new column from, and return the calculated column values to the base table. Parameters are as follows:
Input Parameters:
Name Type
columnMatch Value
baseTable Table
infoTable Table
Output Parameters (to be added to baseTable)
Name Type
outputColumn Column
Script
newTable <- merge(baseTable,infoTable, by = "uniqueIdentifier")
cnames <- colnames(newTable)
outputColumn <- newTable[,match(colorSelection, cnames, nomatch=1)]
outputColumn
The issue thatI am having is as follows:
The code is not returning the correct value for the correct uniqueIdentifier. Is there a way that I can make the values line up, or sort the table in order to return the correct value for the correct uniqueIdentifier?
Thanks!
Jordan
EDIT: found out how to dynamically refer to column number using match function.

PHPExcel 1.8 VLOOKUP "#N/A"

I have my excel sheet and I need to print one cell that contain VLOOKUP to the screen in HTML table. I have the following code:
$test = $objPHPExcel->getSheetByName('test')->toArray(null,true,false,true);
$cell = $test[1][B];
echo $cell;
Result is always: #N/A
Im using excel 2013, xlsx and xlsm extensions.
If i try any other formula will work fine. Only VLOOKUP is giving error for me.
What can I do ?
I dont know if this will help possible debugging, but in my own testing before I found this thread I discovered that I have the same problem but only when returning strings, I can return integers just fine.
Some of the code that I was testing with:
$objPHPExcel->getActiveSheet()->setCellValue('A150', '=VLOOKUP(100,EVENT!A30:B130,2,FALSE)');
$test = $objPHPExcel->getActiveSheet()->getCell('A150')->getCalculatedValue();
echo $test;
In my excel sheet columns A and B are both formatted as 'General.'
Column A is a list of integers in ascending order, and column B has a mix of strings and integers. I found that when the column B cell has an integer in it, the VLOOKUP function works just fine and returns the value of the column B cell. But when the column B cell has a string in it, I also just end up with the #N/A

PHPExcel - Reading Date values

I am using PHPExcel library to read spread sheet data. This library is giving me trouble reading the 'DATE' values.
Question:
How to instruct the PHPExcel to read the 'DATE' values properly even if 'setReadDataOnly' is set as 'false'?
Description:
PHPExcel version: 2.1, Environment: Ubuntu 12
Here is the code block:
$objReader = \PhpExcel\PHPExcel_IOFactory::createReaderForFile($spreadSheetFullFilePathString);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($spreadSheetFullFilePathString);
$objWorksheetObject = $objPHPExcel->getActiveSheet();
Am getting 'integer' values for the 'DATE' column values by default.
I found that, the line '$objReader->setReadDataOnly(true)' is causing the trouble.
So, Changed it as '$objReader->setReadDataOnly(false)'.
I am getting the Date column value properly after this change. But now the reader is reading ALL the ROWS + COLUMNS found in the excel.
Any help would be greatly appreciated.
If $objReader->setReadDataOnly(true);, then the only reader that can identify dates is the Gunumeric Reader. A date value can only be identified by the number format mask, and setReadDataOnly(true) tells the Reader not to read the formatting, so dates cannot subsequently be identified (Gnumeric is the exception, because it was written after the problem was identified, but none of the other Readers have yet been modified to read this information regardless of the setReadDataOnly value. If you need to identify dates, then the only option at this point is $objReader->setReadDataOnly(false);
However, for date cells, you shouldn't get an integer value; you should get a float: it's the decimal that identifies the time part of the Excel datetime serialized value.
If you know which cells contain dates, then you can convert them to unix timestamps or PHPExcel DateTime objects using the helper functions defined in the PHPExcel_Shared_DateTime class (and can then use all the standard PHP date handling functions).
You say that the reader is now reading ALL the ROWS + COLUMNS found in the excel: it should, irrespective of the setReadDataOnly value. If you don't want to read all the rows and columns, then you need to set a Read Filter.

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.

Resources