Repository navigation
Add deferred cell style updates #1013
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
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7503286
Add deferred cell style updates
shps951023 da1f453
Stream worksheet updates in OpenXml editor
shps951023 2d2f2ba
Renamed the MiniExcel class to MiniExcelV2
michelebastione 4cc021a
Refactored the new OpenXmlEditor utility
michelebastione 42f873a
Separated synchronous from asynchronous tests and moved them to the E…
michelebastione 005bfc4
Added documentation for the API nethods
michelebastione bb81205
Minor adjustments
michelebastione bf60746
Added method overloads to save pipeline modifications to different pa…
michelebastione a03d47d
Moved AlterSheet API from OpenXmlExporter to OpenXmlEditor
michelebastione File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace MiniExcelLib.OpenXml.Styles; | ||
|
|
||
| public sealed class OpenXmlCellStyle | ||
| { | ||
| /// <summary>Gets or sets the cell font color.</summary> | ||
| public Color? FontColor { get; set; } | ||
| } |
115 changes: 115 additions & 0 deletions
115
tests/MiniExcel.OpenXml.Tests/Styles/OpenXmlEditorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| using System.Drawing; | ||
| using ClosedXML.Excel; | ||
| using MiniExcelLib.Tests.Common.Utils; | ||
|
|
||
| namespace MiniExcelLib.OpenXml.Tests.Styles; | ||
|
|
||
| public class OpenXmlEditorTests | ||
| { | ||
| [Fact] | ||
| public void SaveAppliesUpdatesInCellOrderAndPreservesExistingStyle() | ||
| { | ||
| using var path = AutoDeletingPath.Create(); | ||
| using (var workbook = new XLWorkbook()) | ||
| { | ||
| var worksheet = workbook.AddWorksheet("Data"); | ||
| var firstCell = worksheet.Cell("A1"); | ||
| firstCell.Value = 12.34; | ||
| firstCell.Style.NumberFormat.Format = "0.00"; | ||
| firstCell.Style.Fill.BackgroundColor = XLColor.Yellow; | ||
| firstCell.Style.Border.LeftBorder = XLBorderStyleValues.Thin; | ||
| firstCell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; | ||
| worksheet.Cell("X100").Value = "last"; | ||
| workbook.SaveAs(path.ToString()); | ||
| } | ||
|
|
||
| MiniExcelV2.Editors.GetOpenXmlEditor(path.ToString()) | ||
| .UpdateCellStyle("X100", style => style.FontColor = Color.Blue, "Data") | ||
| .UpdateCellStyle("A1", style => style.FontColor = Color.Red, "Data") | ||
| .Save(); | ||
|
|
||
| using var updatedWorkbook = new XLWorkbook(path.ToString()); | ||
| var updatedWorksheet = updatedWorkbook.Worksheet("Data"); | ||
| var firstCellStyle = updatedWorksheet.Cell("A1").Style; | ||
|
|
||
| Assert.Equal(Color.Red.ToArgb(), firstCellStyle.Font.FontColor.Color.ToArgb()); | ||
| Assert.Equal(Color.Blue.ToArgb(), updatedWorksheet.Cell("X100").Style.Font.FontColor.Color.ToArgb()); | ||
| Assert.Equal("0.00", firstCellStyle.NumberFormat.Format); | ||
| Assert.Equal(XLColor.Yellow, firstCellStyle.Fill.BackgroundColor); | ||
| Assert.Equal(XLBorderStyleValues.Thin, firstCellStyle.Border.LeftBorder); | ||
| Assert.Equal(XLAlignmentHorizontalValues.Center, firstCellStyle.Alignment.Horizontal); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void LastUpdateWinsForTheSameCell() | ||
| { | ||
| using var path = AutoDeletingPath.Create(); | ||
| CreateWorkbook(path.ToString()); | ||
|
|
||
| MiniExcelV2.Editors.GetOpenXmlEditor(path.ToString()) | ||
| .UpdateCellStyle("A1", style => style.FontColor = Color.Red) | ||
| .UpdateCellStyle("A1", style => style.FontColor = Color.Blue) | ||
| .Save(); | ||
|
|
||
| using var workbook = new XLWorkbook(path.ToString()); | ||
| Assert.Equal(Color.Blue.ToArgb(), workbook.Worksheet(1).Cell("A1").Style.Font.FontColor.Color.ToArgb()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task SaveAsyncUpdatesASelectedWorksheetInAStream() | ||
| { | ||
| using var stream = new MemoryStream(); | ||
| using (var workbook = new XLWorkbook()) | ||
| { | ||
| workbook.AddWorksheet("First").Cell("A1").Value = "first"; | ||
| workbook.AddWorksheet("Second").Cell("A1").Value = "second"; | ||
| workbook.SaveAs(stream); | ||
| } | ||
|
|
||
| await MiniExcelV2.Editors.GetOpenXmlEditor(stream) | ||
| .UpdateCellStyle("A1", style => style.FontColor = Color.Blue, "Second") | ||
| .SaveAsync(); | ||
|
|
||
| stream.Position = 0; | ||
| using var updatedWorkbook = new XLWorkbook(stream); | ||
| Assert.NotEqual(Color.Blue.ToArgb(), updatedWorkbook.Worksheet("First").Cell("A1").Style.Font.FontColor.Color.ToArgb()); | ||
| Assert.Equal(Color.Blue.ToArgb(), updatedWorkbook.Worksheet("Second").Cell("A1").Style.Font.FontColor.Color.ToArgb()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void UpdateCellStyleRejectsInvalidReferences() | ||
| { | ||
| using var stream = new MemoryStream(); | ||
| var editor = MiniExcelV2.Editors.GetOpenXmlEditor(stream); | ||
|
|
||
| Assert.Throws<ArgumentException>(() => | ||
| editor.UpdateCellStyle("1A", style => style.FontColor = Color.Red)); | ||
| Assert.Throws<ArgumentException>(() => | ||
| editor.UpdateCellStyle("XFE1", style => style.FontColor = Color.Red)); | ||
| Assert.Throws<ArgumentException>(() => | ||
| editor.UpdateCellStyle("A1048577", style => style.FontColor = Color.Red)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void FailedSaveLeavesTheOriginalWorkbookUnchanged() | ||
| { | ||
| using var path = AutoDeletingPath.Create(); | ||
| CreateWorkbook(path.ToString()); | ||
|
|
||
| var editor = MiniExcelV2.Editors.GetOpenXmlEditor(path.ToString()) | ||
| .UpdateCellStyle("A1", style => style.FontColor = Color.Red) | ||
| .UpdateCellStyle("A2", style => style.FontColor = Color.Blue); | ||
|
|
||
| Assert.Throws<InvalidDataException>(() => editor.Save()); | ||
|
|
||
| using var workbook = new XLWorkbook(path.ToString()); | ||
| Assert.NotEqual(Color.Red.ToArgb(), workbook.Worksheet(1).Cell("A1").Style.Font.FontColor.Color.ToArgb()); | ||
| } | ||
|
|
||
| private static void CreateWorkbook(string path) | ||
| { | ||
| using var workbook = new XLWorkbook(); | ||
| workbook.AddWorksheet("Data").Cell("A1").Value = "value"; | ||
| workbook.SaveAs(path); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.