-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
Source generator - Reference named styles #44
Comments
This has been implemented in version 1.17.0. |
It would be nicer if the style names weren't forced to be String values -- which encourages use of magic strings by devs. But, a custom Enum would be really nice.... you could implement this with a Generic overload that simply uses the |
I see your point. However since the named styles also can be shown in the Excel UI, I want to be able to use any character in the name. If the name was based on e.g. an Enum, then having a white-space in the name would be problematic. If you want to use an Enum instead, perhaps you can do it by adding your own extension method? E.g.: public static StyleId AddStyle<T>(this Spreadsheet spreadsheet, Style style, T name)
{
return spreadsheet.AddStyle(style, name.ToString());
} For the public class MyClass
{
[CellStyle(nameof(MyEnum.Bold))]
public int MyValue { get; set; }
} |
Yeah. that works, but having to use I was thinking about it some more and realized that the main issue is that your Attribute classes are sealed so we cannot override to augment with our own custom functionality. For example, this same issue exists with Microsoft's own So what are your thoughts on un-sealing the attributes to make it more flexible for consumers? [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public MyAppCellStyleAttribute : CellStyleAttribute
{
public MyAppCellStyleAttribute(CellStyleEnum styleEnum) : base(styleEnum.ToString())
{ }
} |
Unfortunately I don't think it would be trivial to make that work. A source generator only "looks" at the code during compilation, it doesn't execute the code. In your example the source generator could see there is a call to I would guess deriving from |
Ahhh, ok yes we haven't embraced using source generators yet so I often don't think about the implications there. In our current feature I'm working on, we've unfortunately had to stop using the source generation approach in some cases with |
Background and motivation
Right now there is no way to use any styling with the source generator. Enabling styling by only using attributes (for example as suggested in #31 and #43) is an approach that would be easy to use, but requires a lot of work (and potentially many attributes) before all parts of styling would be supported.
If there would be a way to first add named styles to a spreadsheet, then these could be referenced with attributes on properties. This would be a way to have all supported forms of styling with the source generator.
API proposal
API usage
The text was updated successfully, but these errors were encountered: