-
-
Notifications
You must be signed in to change notification settings - Fork 429
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
Open
shps951023
wants to merge
7
commits into
master
Choose a base branch
from
feature/deferred-cell-style-updates
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 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 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
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
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,52 @@ | ||
| using MiniExcelLib.OpenXml.Editor; | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
| namespace MiniExcelLib.OpenXml; | ||
|
|
||
| public sealed class OpenXmlEditor | ||
| { | ||
| internal OpenXmlEditor() { } | ||
|
|
||
| /// <summary> | ||
| /// Creates a new editing pipeline for the provided Excel document. | ||
| /// </summary> | ||
| /// <param name="path">The file path to the Excel document to edit.</param> | ||
| /// <returns> | ||
| /// An <see cref="OpenXmlEditingPipeline"/> instance that can be used to apply modifications to the document. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// This method opens the file for exclusive read-write access.The file is locked until | ||
| /// <see cref="OpenXmlEditingPipeline.SaveChangesAsync"/> is called. | ||
| /// </remarks> | ||
| public OpenXmlEditingPipeline StartEditingPipeline(string path) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(path)) | ||
| throw new ArgumentException("The path cannot be null or whitespace.", nameof(path)); | ||
|
|
||
| var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); | ||
| return new OpenXmlEditingPipeline(stream, leaveOpen: false); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a new editing pipeline for the provided Excel document. | ||
| /// </summary> | ||
| /// <param name="stream">The stream containing the Excel file data.</param> | ||
| /// <param name="leaveOpen"> | ||
| /// If true the stream remains open after changes are saved and must be disposed by the caller, | ||
| /// if false it is automatically closed when changes are saved. Default is false. | ||
| /// </param> | ||
| /// <returns> | ||
| /// An <see cref="OpenXmlEditingPipeline"/> instance that can be used to apply modifications to the file. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// Even with parameter <c>leaveOpen: false</c>, the underlying stream will not be disposed until | ||
| /// <see cref="OpenXmlEditingPipeline.SaveChanges"/> or <see cref="OpenXmlEditingPipeline.SaveChangesAsync"/> are called. | ||
| /// </remarks> | ||
| public OpenXmlEditingPipeline StartEditingPipeline(Stream stream, bool leaveOpen = false) | ||
| { | ||
| if (stream is null) | ||
| throw new ArgumentNullException(nameof(stream)); | ||
|
|
||
| return new OpenXmlEditingPipeline(stream, leaveOpen); | ||
| } | ||
| } |
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,68 @@ | ||
| using MiniExcelLib.OpenXml.Styles; | ||
|
|
||
| namespace MiniExcelLib.OpenXml.Editor; | ||
|
|
||
| public sealed partial class OpenXmlEditingPipeline | ||
| { | ||
| private readonly OpenXmlEditorInternals _internals; | ||
|
|
||
| internal OpenXmlEditingPipeline(Stream stream, bool leaveOpen) | ||
| { | ||
| _internals = new OpenXmlEditorInternals(stream, leaveOpen); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Updates the style of a cell using a callback function that modifies the provided <see cref="OpenXmlCellStyle"/> object. | ||
| /// </summary> | ||
| /// <param name="cellReference">The cell reference in standard Excel format (e.g., "A1", "B5").</param> | ||
| /// <param name="updateCellCallback">A callback function that receives an <see cref="OpenXmlCellStyle"/> object and applies the desired style changes.</param> | ||
| /// <param name="sheetName">The name of the worksheet to update. If null or not specified, the first sheet in the workbook is used.</param> | ||
| /// <returns> | ||
| /// Returns this <see cref="OpenXmlEditingPipeline"/> instance to enable method chaining. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// Modifications are queued in the pipeline and not written to the file until <see cref="SaveChanges"/> or <see cref="SaveChangesAsync"/> are called. | ||
| /// </remarks> | ||
| public OpenXmlEditingPipeline UpdateCellStyle(string cellReference, Action<OpenXmlCellStyle> updateCellCallback, string? sheetName = null) | ||
| { | ||
| if (updateCellCallback is null) | ||
| throw new ArgumentNullException(nameof(updateCellCallback)); | ||
|
|
||
| var style = new OpenXmlCellStyle(); | ||
| updateCellCallback(style); | ||
|
|
||
| return UpdateCellStyle(cellReference, style, sheetName); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Updates the style of a cell using a pre-configured <see cref="OpenXmlCellStyle"/> object. | ||
| /// </summary> | ||
| /// <param name="cellReference">The cell reference in standard Excel format (e.g., "A1", "B5").</param> | ||
| /// <param name="cellStyle">The <see cref="OpenXmlCellStyle"/> object containing the style properties to apply to the cell.</param> | ||
| /// <param name="sheetName"> The name of the worksheet to update. If null or not specified, the first sheet in the workbook is used.</param> | ||
| /// <returns> | ||
| /// Returns this <see cref="OpenXmlEditingPipeline"/> instance to enable method chaining. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// Modifications are queued in the pipeline and not written to the file until <see cref="SaveChanges"/> or <see cref="SaveChangesAsync"/> are called. | ||
| /// </remarks> | ||
| public OpenXmlEditingPipeline UpdateCellStyle(string cellReference, OpenXmlCellStyle cellStyle, string? sheetName = null) | ||
| { | ||
| _internals.UpdateCellStyle(cellReference, cellStyle, sheetName); | ||
| return this; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Applies all queued modifications to the Excel file. | ||
| /// </summary> | ||
| /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
| /// <remarks> | ||
| /// This method must be called to persist any modifications made through the pipeline. | ||
| /// If the pipeline was created from a stream with <c>leaveOpen: true</c>, the caller is responsible for its disposal. | ||
| /// </remarks> | ||
| [CreateSyncVersion] | ||
| public async Task SaveChangesAsync(CancellationToken cancellationToken = default) | ||
| { | ||
| await _internals.SaveAsync(cancellationToken).ConfigureAwait(false); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: mini-software/MiniExcel
Length of output: 10479
🏁 Script executed:
Repository: mini-software/MiniExcel
Length of output: 2519
🏁 Script executed:
Repository: mini-software/MiniExcel
Length of output: 2451
🏁 Script executed:
Repository: mini-software/MiniExcel
Length of output: 23610
Use
SaveChanges()for the editor pipeline.StartEditingPipeline(...)returnsOpenXmlEditingPipeline, which exposesSaveChanges()andSaveChangesAsync(). It does not exposeSave(). Replace.Save()with.SaveChanges().🤖 Prompt for AI Agents