RadSpreadsheet Cell Values for DateTime returning Number values (Telerik) - telerik

I have a CSV file with Dates ("MM/DD/YYYY"). I wish to parse these values within the active worksheet as string values, but they're converted to number format (i.e. "22532"). I'm unable to get these values in the original string format.
CellSelection selection = sheet.Cells[0, 0];
ICellValue value = selection.GetValue().Value;
Is it possible to ensure all cell values are in String format, representing exactly how they are in a CSV file?

You could use the following code:
CellSelection selection = sheet.Cells[0, 0];
string value = selection.GetValue().Value.GetResultValueAsString(new CellValueFormat("mm/d/yyyy"));
This will return a string formatted as a date. In the case with "22532" the result will be "09/8/1961".

This is not the exact answer you are looking for, but you can convert the int value to a datetime with the following code:
public static DateTime FromExcelSerialDate(this int serialDate)
{
if (serialDate > 59) serialDate -= 1; //Excel/Lotus 2/29/1900 bug
return new DateTime(1899, 12, 31).AddDays(serialDate);
}

Related

Find newest Revisions of Entities where timestamp is less than x

I've made a painting to illustrate my desired result:
painting of the result
I have a max timestamp that i pass to my backend and i want to return the newest versions of the Entities that have a timestamp that is less than the passed one.
The passed timestamp in my example is 180. I've kept the numbers low so that it is easier to visualize.
This is a combination of a horizontal and a vertical Query and i'm not sure how i can achieve that.
Any help would be appreciated!
It can be achieved with the following:
public List<Book> getBooksByMaxTimestamp(Date date) {
String formattedDate = simpleDateFormat.format(date);
System.out.println("Less than " + formattedDate);
AuditQuery maxRevisionNumberQuery = auditReader.createQuery().forRevisionsOfEntity(Book.class, true, false);
maxRevisionNumberQuery.add(AuditEntity.revisionProperty("timestamp").le(date.getTime()));
maxRevisionNumberQuery.addProjection(AuditEntity.revisionNumber().max());
int maxRevisionNumber = (int) maxRevisionNumberQuery.getSingleResult();
AuditQuery auditQuery = auditReader.createQuery().forEntitiesAtRevision(Book.class, maxRevisionNumber);
return auditQuery.getResultList();
}
First we get the max revision number that is in that range and after that we use that number to get the entities that we want.

flutter Sorting arraylist by Date

In My streamBuilder I have Array list contains dates having format(dd-MM-yyyy).
I want to arrange the list in ascending order.
The code I used in StreamBuilder after getting Datasnapshot
Map<dynamic, dynamic> data = snap.data.snapshot.value;
List item = [];
data.forEach(
(index, data) => item.add(
{"key": index, ...data}
)
);
item..sort((a,b)=>a['key'].compareTo(b['key']));
I am expecting result as
16-06-2020
17-06-2020
18-06-2020
19-06-2020
22-06-2020
04-07-2020
The result I am getting is
04-07-2020
16-06-2020
17-06-2020
18-06-2020
19-06-2020
22-06-2020
You'll need to parse your Strings to DateTimes. Since DateTime parse() method won't accept Strings in the format you provided, you can do something like this:
List<String> strings = ['04-07-2020', '17-06-2020', '16-06-2020', '19-06-2020', '18-06-2020', '22-06-2020'];
List<DateTime> dateTimes = [];
strings.forEach((string) {
var splitted = string.split('-');
var day = splitted[0];
var month = splitted[1];
var year = splitted[2];
var formatted = '$year-$month-$day';
dateTimes.add(DateTime.parse(formatted));
});
dateTimes.sort((a, b) => a.compareTo(b));
Just adapt it for your structure!
Storing the data as Timestamps in Firebase would make it much simpler, then you can sort it by this Timestamp.
If you need the dates formatted as a String (dd-MM-yyyy) you can just parse it to Datetime and use the Intl-Package to convert it to a formatted String.

Conversion failed when converting the varchar value 'Zon7' to data type int

I'm getting this error :
Conversion failed when converting the varchar value 'Zon7' to data
type int.
establishments = GetEstablishments(waters.Select(t => ReplaceZonToEmptyString(t.IdGeographie)));
public static int ReplaceZonToEmptyString(string zoneId)
{
zoneId.Replace("Zon", string.Empty);
var sbInput = zoneId.Replace(" ", string.Empty);
return Convert.ToInt32(sbInput.ToString());
}
public static IQueryable<Etablissement> GetEstablishments(IQueryable<int> ids)
{
return from zone in entities.Zones
where ids.Contains(zone.IdZone)
select zone.IdBatimentNavigation.IdEtablissementNavigation;
}
var result = establishments.ToList();
in database i have a column of type varchar the column name is 'IdGeographie' with values that start with 'Zon', something like this "ZonXXXX"
You are trying to compare a VARCHAR column with values of type int. One of these values will have to change and since it con not be the SQL column, it has to be the compare value:
public static string ReplaceZonToEmptyString(string zoneId)
{
var sbInput = new StringBuilder(zoneId);
sbInput.Replace("Zon", string.Empty);
sbInput.Replace(" ", string.Empty);
return sbInput.ToString();
}
public static IQueryable<Etablissement> GetEstablishments(IQueryable<string> ids)
{
return from zone in entities.Zones
where ids.Contains(zone.IdZone)
select zone.IdBatimentNavigation.IdEtablissementNavigation;
}
If the signature of the methods can't change, you have to do the conversion within GetEstablishments:
public static IQueryable<Etablissement> GetEstablishments(IQueryable<int> ids)
{
var textIds = ids.Select(id => id.ToString());
return from zone in entities.Zones
where textIds.Contains(zone.IdZone)
select zone.IdBatimentNavigation.IdEtablissementNavigation;
}
Note that in waters.Select(t => ReplaceZonToEmptyString(t.IdGeographie)), the value waters must be a materialized list of values (i.e. not another EF query), since your replace operation can not work within Entity Framework (in either of the options).
What do you think the following code will do:
string original = "Hello World!";
string changed = original.Recplace("o", "xx");
original won't be changed, changed will equal "Hellxx, Wxxrld".
So you should change your ReplaceZonToEmptyString:
public static int ExtractZoneNr(string zoneId)
{
string idWithoutZon = zoneId.Replace("Zon", string.Empty);
string sbInput = idWithoutZon.Replace(" ", string.Empty);
return Int32.Parse(sbInput);
}
Alas this will only work on IEnumerable, not on IQueryable
A better solution:
This solution only works if IdGeographie is the three letters "Zon" followed by the string representation of the IdZone and nothing else. So no spaces, no leading zeroes etc: "Zon4" and not "Zon004", nor "Zon 4"
You have two IQueryables one for waters and one for zones:
IQuerybale<Water> waters = ... // probably entities.Waters
IQueryable<Zone> zones = entities.Zones
Every Zone contains an int property IdZone, and a property .IdBatimentNavigation.IdEtablissementNavigation, which seems to be of type Etablissement
Furhermore every Water has a string property GeographieId in the format "ZonX" where X is an integer number.
Now you want to query the IdBatimentNavigation.IdEtablissementNavigation of all Zones with an IdZone that equals the X part of one or more of the Waters
For example: if you have the following Waters
[0] GeographieId = "Zon10"
[1] GeographieId = "Zon42"
[2] GeographieId = "Zon7"
And you have Zones with IdZone: 4, 42, 30, 7, 22.
Then as a result you want the IdBatimentNavigation.IdEtablissementNavigation of the Zones with IdZone: 42 any 7 (in any order)
Why not join waters and zones?
var result = zones.
.Select(zone => new
{
GeographieId = "Zon" + zone.IdZone.ToString(),
Etablissement = zoneIdBatimentNavigation.IdEtablissementNavigation,
})
.Join(waters, // join with waters
zone => zone.GeographieId, // from zone take the GeoGraphieId
water => water, // from waters take idGeographie
(zone, water) => zone.Etablissement); // when thay match return
A better solution would be to try to remove the "Zon" part from IdGeographie, and Parse the remaining XXXX to an int. Alas there is no function that can perform the parsing AsQueryable.

Getting date value from an array hash

I have a hash that looks like
tagArray = { :"2014Date"=>["11/22/2014"], :"2015Date"=>["03/21/2015"] }
Since i already know for a given key, there is only one element in the array of 'values', how can you get just the value not as an array?
value = tagArray[:"2015Date"]
=>
value = ["04/12/2015"]
You can just index the array then and get the date like
value = value.fetch(0).to_s
=>
value = "04/12/2015"
However I am looking for a more elegant way of doing it.
Note: I am using Ruby 2.2, so need to 'strp' the date first to change it from mm/dd/yyyy format which is the end goal.
This is a bit simpler:
value = tagArray[:"2015Date"].last
=>
value = "03/21/2015"

Insert formatted values as currency type while using EPPlus

I am using format:
type ="$###,###,##0.00"
for currency and assigning the format type to the worksheet cells
eg.
wrkSheet.Cells[0].Style.Numberformat.Format = formatType;
But this is inserted as text type in excel.
I want this to be inserted as Currency or Number in order to continue to do analysis on the values inserted (sort, sum etc).
Currently as it is text type validations do not hold correct.
Is there any way to force the type in which the formatted values can be inserted?
Your formatting is correct. You needs to covert values to its native types
Use this code, it should work:
using (var package = new ExcelPackage())
{
var worksheet = package.Workbook.Worksheets.Add("Sales list - ");
worksheet.Cells[1, 1].Style.Numberformat.Format = "$###,###,##0.00";
worksheet.Cells[1, 1].Value =Convert.ToDecimal(24558.4780);
package.SaveAs(new FileInfo(path));
}
Indices start from 1 in Excel.
This code
using (var package = new ExcelPackage())
{
var worksheet = package.Workbook.Worksheets.Add("Sales list - ");
worksheet.Cells[1, 1].Style.Numberformat.Format = "$###,###,##0.00";
worksheet.Cells[1, 1].Value = 24558.4780;
package.SaveAs(new FileInfo(path));
}
produces $24 558,48 for me

Resources