php - PHPExcel setReadDataOnly(true) is not working with ODS - phpexcel

Enter 12:00 in an ODS cell and get default Time format, setReadDataOnly(true) then getValue(), the value is 25569.5, if format the cell as number then getValue(), the value is 0.5. How can I see 12:00 in ODS and getValue()= 0.5?

You can't see the date/time in ODS (or xlsx or xls, etc) using PHPExcel if you've used setReadDataOnly(true)....
The whole point of setReadDataOnly(true) is that it reads only the raw content of a cell, without any of the formatting information, such as that which identifies the cell as containing a date/time value.
Excel stores date/time values as a serialized timestamp, the number of days since a base start date (1st January 1900 or 1st January 1904, depending on the calendar setting), as a floating point value.
What identifies this as a date/time value is the number format mask, which specifies how that float should be displayed.
PHPExcel replicates this behaviour, and if you load the spreadsheet with setReadDataOnly(false), then getValue() will return the serialized timestamp, while getFormattedValue() will apply the number format mask and return a formatted date/time string.
Conversely, if you load the file with setReadDataOnly(true), there is no way that the code can identify this float as a date/time value, or know how it should be formatted other than as a float, because you elected not to load the formatting information that woul allow this identification too be made.
TL/DR Don't use setReadDataOnly(true) if you want PHPExcel to be able to identify date/time values as dates/times

Related

How to convert character type date to date type in SAS

I have a character variable in the form of "Jan-17" in my table and would like to convert it to date type. Does anyone what kind of date form this is and how I could go about converting it? Thank you.
SAS date variables are stored as number of days since Jan 1, 1960. For a variable that is Month and Year only, you'd need to pick a day - many choose the 1st, some the 15th or 30th/31st, depending on your business rules.
Either way, you can use the input function as a starting place. You need to know the informat - the instructions for how to translate characters to numbers. In your case, MONYY6. will likely work, though you can also try ANYDATEDTE. to let SAS guess the date informat to use.
Then, if needed, you can adjust the date it chose based on your business rules using the intnx function, which allows you to adjust it to the 15th or end of month if you prefer.
Use the INPUT() function with the correct informat.
data have;
date_char = 'Jan-17';
run;
data want;
set have;
date_dt = input(date_char, monyy6.);
format date_dt date9.;
run;
01JAN2017

Values in column change when reading in excel file

I am trying to read in an excel file using the readXL package with a column of time stamps. For this particular file, they are randomly distributed times, so they make look like 00:01, 00:03, 00:04, 00:08, 00:10, etc. so I need these timestamps to read into R correctly. The time stamps turn into random decimals.
I looked in the excel file (which is outputted from a different program) and it appears the column type within excel is "custom". When I convert that "custom" column to "text", it shows me the decimals that are actually stored and reading into R. Is there a way to load in the timestamps instead of the decimals?
I have already tried to using col_types to make it text or integers, but it is still reading the numbers in as decimals and not the timestamps.
df<-
readxl::read_xlsx(
"./Data/LAFLAC_60sec_9999Y1_2019-06-28.xlsx",
range = cell_cols("J:CE"),
col_names = T
)
The decimals are a representation in day after midnight. So 1:00 am is .041667, or 1/24.
In R, you can convert those numbers back into timestamps in a variety of ways.
Try this page for more info
https://stackoverflow.com/a/14484019/6912825

Date column produces unknown numbers in r

I wrote a data frame in CSV format with R and opened it with Excel. Then I converted it to an Excel Workbook and made some edit on it.
When I imported that Excel file in R again, the date column looked like this. (Few are in numbers and few are dates.)
Date
39387
39417
15/01/2007
16/01/2007
I tried to change the format with Excel but failed. General or number option in Excel format generate the number like I mentioned which is in no way related to the date.
It seems all four of your example are in respect of dates in January (11th and 12th for the first two), that the Excel involved has been configured to expect MDY (rather than DMY as popular in UK for example) and its date system to ‘1900’, and that the CSV has written the dates as 'conventional' dates rather than date index numbers.
So what Excel saw first was:
11/01/2017
12/01/2017
15/01/2017
16/01/2017
intended to represent the 11th, 12th, 15th and 16th of January. However, because expecting MDY Excel has interpreted (coercing the Text to date index) the first two entries as November 1 and December 1. For ‘months’ 15 and 16 it did no interpretation and just reported the text as in the CSV.
The coercion is automatic with no option to turn it off (nor 'reversed' with a change of format). Various ways to address this (common) issue are mentioned in the link kindly provided by #Gerard Wilkinson. I am not providing a full solution here since (a) some things are under user control (eg the ‘1904’ system is an option, as is the choice whether MDY or DMY) and (b) the preferred route will depend to some extent on how often required and what the user is most familiar with.

PHPExcel get display value of unknown type

It is possible to use getValue(), getCalculatedValue() and getOldCalculatedValue() to retrieve the value of a cell in phpexcel.
Is there a way to determine programatically the content type of the cell and apply the corresponding correct method. I need to use this in a general way. i.e. to display the same value as when opening excel.
I know there is something called getDataType() but not sure how to apply it in this case (not in documentation). In my experience sometimes only one of these three retrieves the correct value.
(i.e. sometimes getOldCalculatedValue works but not getCalculatedValue for a formula for example. other times only getvalue works, etc.)
getOldCalculatedValue() is used to retrieve the result of a previous calculation in MS Excel itself; and should not be relied on, because it is possible to disable autocalculate in MS Excel, which can leave this field empty, or even with an incorrect value. It is used within PHPExcel as a "fallback" for cell formulae that are reliant on external spreadsheet data, but it still shouldn't be trusted as an absolute.
getValue() returns the "raw" value of the cell. The returned value may require "interpretation". A cell containing a date and/or time is simply a float value in MS Excel, so it will return that float (e.g. 42017.7916666667 instead of a human-readable date/time like 13-Jan-2015 19:00;
and it will return the actual formula if a cell contains a formula (e.g. =TODAY()); or 0.8 for a value that might be formatted as a percentage and that appears as 80% in MS Excel itself.
getCalculatedValue() will attempt to execute a formula calculation if a cell contains a formula, and return the result of that calculation. If the cell doesn't contain a formula, then it will return the "raw" value, in the same way as getValue(). While PHPExcel has a fairly good calculation engine, it isn't perfect (it can't handle 3d cell ranges or array formulae for example), so it is possible for some formulae to fail. Likewise, formulae containing references to external resources may also fail, and while PHPExcel will attempt to use the getOldCalculatedValue() in that circumstance, it isn't (as mentioned above) guaranteed to maintain the correct result.
getFormattedValue() will execute getCalculatedValue(), and then apply any number formatting mask that applies to that cell against the result, so that (for example) a float with a date mask will be displayed as a date.
However, if you've loaded a spreadsheet file with readDataOnly(true), then that tells PHPExcel not to load any formatting, including number format masks, so it will not be able to format the result.
When you access MS Excel itself, then the closest result to the values displayed in MS Excel itself will be getFormattedValue()

Parsing out time-of-day from a date to a separate column

I have, per cell, a date value in the format 2013-01-05 11:21.
Is there a way to separate the time of day (ie 11:21) and put it in a new column, without having to manually cut and paste?
I have a lot of date values in one column, and I want to separate the time-of-day portion of these dates into a new adjacent column.
Yes - the TIMEVALUE function should do this. You may need to format the result cells (in my examle: B1:B8) as time values. Using cell formatting, you can set the output to a hh:mm syntax, too.

Resources