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

Support for Dictionaries with attributes in LoadFromCollection #969

Open
swmal opened this issue Jul 14, 2023 · 4 comments
Open

Support for Dictionaries with attributes in LoadFromCollection #969

swmal opened this issue Jul 14, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@swmal
Copy link
Contributor

swmal commented Jul 14, 2023

Via the attributes we could define a set of keys that should/could be present in a dictionary on each object in the list. Each key will become a column in the order they are defined.

Could look something like this:

[EPPlusTableColumn(Order=3, Keys=new string[]{ "Foo", "Bar" })]
public Dictionary<string, int> Quarters { get; set; }

Complex types as the value of the dictionary could be supported following the logic of nested classes.

@swmal swmal added the enhancement New feature or request label Jul 14, 2023
@grosch-intl
Copy link

So what you showed would not work as that's compile time. The set of keys for the Quarters dictionary would be determined at runtime, and is stored in a HashSet<string>.

@grosch-intl
Copy link

Here's a better example of what I'm doing:

        var headers = new List<string> { "Barcode", "Current Org Level 3", "Current Org Level 4", "Current Org Level 5" };
        headers.AddRange(data.Dates);   // <=- This is a sorted list of date strings that are dynamically generated.

        for (var i = 0; i < headers.Count; i++) {
            var column = i + 1;

            ws.Cells[1, column].Value = headers[i];
            ws.Cells[1, column].Style.Font.Bold = true;
        }

        var row = 2;

        foreach (var (barcode, value) in data.Entries) {
            var column = 1;

            var range = ws.Cells[row, column++];
            range.Value = barcode;
            range.Style.Font.Bold = true;

            ws.Cells[row, column++].Value = value.ThreeName;
            ws.Cells[row, column++].Value = value.FourName;
            ws.Cells[row, column++].Value = value.FiveName;

            // This is a Dictionary<string, int> where keys will be from the date list above.
            //                       👇
            foreach (var date in data.Dates) {
                try {
                    if (value.Values.TryGetValue(date, out var result) is false)
                        continue;

                    ws.Cells[row, column].Value = result;
                } finally {
                    column++;
                }
            }

            row++;
        }

@swmal
Copy link
Contributor Author

swmal commented Aug 8, 2023

Thanks, let's see if I understand this correctly:

  1. We need a new type of attribute that should have a property/member that defines the headers in the order they should appear as columns in the spreadsheet (corresponding to your headers variable above).
  2. This attribute should only be used on properties that are of the Dictionary<string,*> type. If used on another type of member an exception will be thrown when LoadFromCollection is called.
  3. When loading the data into the range we use the value returned by each key if present in the Dictionary.
  4. The result will be that cells that corresponds to a key that wasn't present in the Dictionary will be empty or contain the value returned if the key was present in the Dictionary.

@grosch-intl
Copy link

grosch-intl commented Aug 8, 2023 via email

swmal added a commit that referenced this issue Nov 8, 2023
swmal added a commit that referenced this issue Nov 22, 2023
JanKallman pushed a commit that referenced this issue Nov 22, 2023
* #969, #1058 - implemented EPPlusDictionaryColumnAttribute for LoadFromCollection with attributes

* #969, #1058 - added support for Dictionary property in LoadFromCollection

* #1058, #969 - LoadFromCollection, support for Dictionary property

* #1058, #969 - added testclass

* #1058, #969 - added unit test for reading column headers/keys from attribute
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