8000 Async implementation of freezing top row by BaatenHannes · Pull Request #684 · mini-software/MiniExcel · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Async implementation of freezing top row #684

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

Merged
merged 4 commits into from
Nov 2, 2024

Conversation

BaatenHannes
Copy link
Contributor

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

@meld-cp
Copy link
Contributor
meld-cp commented Oct 30, 2024

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
Contributor Author

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

@shps951023
Copy link
Member

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

@shps951023 shps951023 merged commit 92b295d into mini-software:master Nov 2, 2024
2 checks passed
@BaatenHannes
Copy link
Contributor Author

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

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
0