Skip to content

Async implementation of freezing top row - #684

Merged
shps951023 merged 4 commits into
mini-software:masterfrom
BaatenHannes:master
Nov 2, 2024
Merged

Async implementation of freezing top row#684
shps951023 merged 4 commits into
mini-software:masterfrom
BaatenHannes:master

Conversation

@BaatenHannes

Copy link
Copy Markdown
Contributor

See issue #678.
Added async implementation of PR #620 Freeze top row.

@meld-cp

meld-cp commented Oct 30, 2024

Copy link
Copy Markdown
Contributor

Hi @BaatenHannes, thanks for the PR. :)

I think to prevent repeating the Freeze pane code for the Async versions we can do the following:

  • add the following methods to \src\MiniExcel\OpenXml\ExcelOpenXmlSheetWriter.DefaultOpenXml.cs:
        private string GetSheetViews()
        {
            // exit early if no style to write
            if (_configuration.FreezeRowCount <= 0 && _configuration.FreezeColumnCount <= 0)
            {
                return string.Empty;
            }

            var sb = new StringBuilder();

            // start sheetViews
            sb.Append(WorksheetXml.StartSheetViews);
            sb.Append(WorksheetXml.StartSheetView());

            // Write panes
            sb.Append(GetPanes());

            // end sheetViews
            sb.Append(WorksheetXml.EndSheetView);
            sb.Append(WorksheetXml.EndSheetViews);

            return sb.ToString();
        }

        private string GetPanes()
        {

            var sb = new StringBuilder();

            string activePane;
            if (_configuration.FreezeColumnCount > 0 && _configuration.FreezeRowCount > 0)
            {
                activePane = "bottomRight";
            }
            else if (_configuration.FreezeColumnCount > 0)
            {
                activePane = "topRight";
            }
            else
            {
                activePane = "bottomLeft";
            }
            sb.Append(
                WorksheetXml.StartPane(
                    xSplit: _configuration.FreezeColumnCount > 0 ? _configuration.FreezeColumnCount : (int?)null,
                    ySplit: _configuration.FreezeRowCount > 0 ? _configuration.FreezeRowCount : (int?)null,
                    topLeftCell: ExcelOpenXmlUtils.ConvertXyToCell(
                        _configuration.FreezeColumnCount + 1,
                        _configuration.FreezeRowCount + 1
                    ),
                    activePane: activePane,
                    state: "frozen"
                )
            );

            // write pane selections
            if (_configuration.FreezeColumnCount > 0 && _configuration.FreezeRowCount > 0)
            {
                // freeze row and column
                /*
                 <selection pane="topRight" activeCell="B1" sqref="B1"/>
                 <selection pane="bottomLeft" activeCell="A3" sqref="A3"/>
                 <selection pane="bottomRight" activeCell="B3" sqref="B3"/>
                 */
                var cellTR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount + 1, 1);
                sb.Append(WorksheetXml.PaneSelection("topRight", cellTR, cellTR));

                var cellBL = ExcelOpenXmlUtils.ConvertXyToCell(1, _configuration.FreezeRowCount + 1);
                sb.Append(WorksheetXml.PaneSelection("bottomLeft", cellBL, cellBL));

                var cellBR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount + 1, _configuration.FreezeRowCount + 1);
                sb.Append(WorksheetXml.PaneSelection("bottomRight", cellBR, cellBR));
            }
            else if (_configuration.FreezeColumnCount > 0)
            {
                // freeze column
                /*
                   <selection pane="topRight" activeCell="A1" sqref="A1"/>
                */
                var cellTR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount, 1);
                sb.Append(WorksheetXml.PaneSelection("topRight", cellTR, cellTR));

            }
            else
            {
                // freeze row
                /*
                    <selection pane="bottomLeft"/>
                */
                sb.Append(WorksheetXml.PaneSelection("bottomLeft", null, null));

            }

            return sb.ToString();

        }
  • remove the WriteSheetViews and WritePanes methods from \src\MiniExcel\OpenXml\ExcelOpenXmlSheetWriter.cs

  • replace the 3 WriteSheetViews(writer); calls in \src\MiniExcel\OpenXml\ExcelOpenXmlSheetWriter.cs with writer.Write(GetSheetViews());

  • replace the await WritePanesAsync(writer); calls in this PR with await writer.WriteAsync(GetSheetViews());

  • remove the WriteSheetViewsAsync(MiniExcelAsyncStreamWriter writer) and WritePanesAsync(MiniExcelAsyncStreamWriter writer) methods in this PR

This will ensure that the sync and async writers are using the same code to build the panes.

@BaatenHannes

Copy link
Copy Markdown
Contributor Author

Hi @meld-cp, good point, didn't notice the DefaultOpenXml class.
I refactored it to prevent the code duplication.

@shps951023

Copy link
Copy Markdown
Member

@BaatenHannes 👍👍👍 Merged, could we invite you to our team?

@shps951023
shps951023 merged commit 92b295d into mini-software:master Nov 2, 2024
@BaatenHannes

Copy link
Copy Markdown
Contributor Author

I'm a bit busy at the moment. But thanks for the invite.

pull Bot pushed a commit to zilo555/MiniExcel that referenced this pull request Aug 29, 2026
* Add async implementation of frozen rows and columns

* Add unit tests for async implementation of frozen rows and columns

* Fix formatting and delete temp file in unit test

* move freezing top row implementation to DefaultOpenXml.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants