Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to provide custom fallback value for source generators #67

Closed
Ashymonth opened this issue Aug 16, 2024 · 4 comments
Closed
Labels
enhancement New feature or request
Milestone

Comments

@Ashymonth
Copy link

Ashymonth commented Aug 16, 2024

I'd like to have the ability to provide my own custom default value. For example, I need to make a report. I have a rule that if a number is null, then I need to fill the cell with a "-" character. But currently, I can't do it. If I set my number property to string, then in the Excel file it will also be a string. So I can't have all options for numbers that Excel has. But if my property is Nullable then I can't provide "-" character for it.

@Ashymonth Ashymonth changed the title Add options to provide custom fallback value Add options to provide custom fallback value for source generators Aug 16, 2024
@sveinungf
Copy link
Owner

You're right, there is no way to do this right now when using the source generator. To do this right now you will have to map your type to DataCells manually and then call AddRowAsync.

For the source generator I'm considering how to offer more flexibility on how properties are mapped to cells. What I currently have in mind is to be able to define a mapping class with a specific shape, and then instruct the source generator to use it by using an attribute. For example like this:

public class MyObject
{
    [CellValueMapper(typeof(NullToDashMapper))]
    public int? Number { get; set; }
}

public class NullToDashMapper : ICellValueMapper<int?>
{
    public DataCell MapToCell(int? value)
    {
        return value is null ? new DataCell("-") : new DataCell(value.Value);
    }
}

This is not possible right now, but I think that could solve your use case and also many other possible use cases.

@sveinungf sveinungf added the enhancement New feature or request label Aug 16, 2024
@Ashymonth
Copy link
Author

What do you think about providing style in this way? If I need to add style to a cell, I need to pass the StyleId for it. Currently, there is no public API to get a style. Should we add an API to get the StyleId from the SpreadSheet and create an interface method like public StyledCell MapToCell(SpreadSheet sheet, int? value)? Or something different?

@sveinungf
Copy link
Owner

I'm currently working on #44 which will add support for named styles, and also a public API to get a style by its name. A mapping class like I suggested above could potentially handle styling by referencing style names. Having access to the Spreadsheet instance in the mapping class is something I would like to avoid, because then you could call methods that I think we all agree should never be called in that context.

@sveinungf sveinungf added this to the v1.17.0 milestone Sep 6, 2024
@sveinungf
Copy link
Owner

This has been implemented in version 1.17.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants