Web forum is in read-only mode. Login as active registered customer for write access
  Forum Search   New Posts New Posts

Generic dataset - date formating

 Post Reply Post Reply
Author
Sriram Ambuga View Drop Down
New Member
New Member
Avatar

Joined: 14 Oct 2019
Posts: 1
Post Options Post Options   Quote Sriram Ambuga Quote  Post ReplyReply Direct Link To This Post Topic: Generic dataset - date formating
    Posted: 14 Oct 2019 at 2:44pm
Hi,

We have created a report template on similar lines as Generic Reports 2.template.xls provided in the samples - except the formatting part. In the sample file the formatting for date is applied based on the column name - but in our case since the report template is used across different datasets we cannot have condition based formatting i.e the <#if(<#columns.name>="OrderDate";<#format cell(longdate)>;)> is not possible. 

When we generate the report now it displays date as numbers (and the excel column type is general). Is there a way to get the column type so we can apply the format conditions? Or is there any other method we can use to get the date format working.

The same is the case for currency (but less critical).

Thanks.
Back to Top
Adrian Gallero View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 1251
Post Options Post Options   Quote Adrian Gallero Quote  Post ReplyReply Direct Link To This Post Posted: 18 Oct 2019 at 12:34pm
Hi,
Thanks for the reporting this. Indeed you are right, there is no easy way to get the datatype of the value you are entering. But there is a simple workaround: Use a user-defined function.

In the Generic Reports 2 demo you can add this class:
   public class DataTypeImp: TFlexCelUserFunction
    {
        public override object Evaluate(object[] parameters)
        {
            if (parameters == null || parameters.Length != 1)
            {
                throw new Exception("DataType must be called with 1 parameter.");
            }

            switch (parameters[0])
            {
                case double d:
                    return "double";
                case DateTime dt:
                    return "datetime";
                default:
                    return "";
            }
        }
    }

Then, add the line:
               Report.SetUserFunction("datatype", new DataTypeImp());

                Report.Run(DataPath + "Generic Reports 2.template.xls", saveFileDialog1.FileName);

And in the template use:
<#if(<#datatype(<#val>)>="datetime";<#format cell(longdate)>;)>

I've tried it here and it works as expected. I am undecided yet if this would be a good thing to add directly to FlexCel or not; the main problem being that the datatypes are unlimited. (In this example I just report doubles and datetimes, but I won't report for example an int).

But I'll see how we can make other cases like this simpler: At least we should change the Generic Reports 2 demo to include the UDF.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down