|
| 1 | +using MiniExcelLib.OpenXml.Editor; |
| 2 | + |
| 3 | +// ReSharper disable once CheckNamespace |
| 4 | +namespace MiniExcelLib.OpenXml; |
| 5 | + |
| 6 | +public sealed partial class OpenXmlEditor |
| 7 | +{ |
| 8 | + internal OpenXmlEditor() { } |
| 9 | + |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// Creates a new editing pipeline for the provided Excel document. |
| 13 | + /// </summary> |
| 14 | + /// <param name="path">The file path to the Excel document to edit.</param> |
| 15 | + /// <returns> |
| 16 | + /// An <see cref="OpenXmlEditingPipeline"/> instance that can be used to apply modifications to the document. |
| 17 | + /// </returns> |
| 18 | + /// <remarks> |
| 19 | + /// This method opens the file for exclusive read-write access. The file is locked until |
| 20 | + /// SaveChanges or SaveChangesAsync is called. |
| 21 | + /// </remarks> |
| 22 | + public OpenXmlEditingPipeline StartEditingPipeline(string path) |
| 23 | + { |
| 24 | + if (string.IsNullOrWhiteSpace(path)) |
| 25 | + throw new ArgumentException("The path cannot be null or whitespace.", nameof(path)); |
| 26 | + |
| 27 | + var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); |
| 28 | + return new OpenXmlEditingPipeline(stream, leaveOpen: false); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Creates a new editing pipeline for the provided Excel document. |
| 33 | + /// </summary> |
| 34 | + /// <param name="stream">The stream containing the Excel file data.</param> |
| 35 | + /// <param name="leaveOpen"> |
| 36 | + /// If true the stream remains open after changes are saved and must be disposed by the caller, |
| 37 | + /// if false it is automatically closed when changes are saved. Default is false. |
| 38 | + /// </param> |
| 39 | + /// <returns> |
| 40 | + /// An <see cref="OpenXmlEditingPipeline"/> instance that can be used to apply modifications to the file. |
| 41 | + /// </returns> |
| 42 | + /// <remarks> |
| 43 | + /// Even with parameter <c>leaveOpen: false</c>, the underlying stream will not be disposed until |
| 44 | + /// SaveChanges or SaveChangesAsync are called. |
| 45 | + /// </remarks> |
| 46 | + public OpenXmlEditingPipeline StartEditingPipeline(Stream stream, bool leaveOpen = false) |
| 47 | + { |
| 48 | + if (stream is null) |
| 49 | + throw new ArgumentNullException(nameof(stream)); |
| 50 | + |
| 51 | + return new OpenXmlEditingPipeline(stream, leaveOpen); |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Modify the properties of a worksheet in the specified document. |
| 56 | + /// </summary> |
| 57 | + /// <param name="path">The path to the OpenXml document.</param> |
| 58 | + /// <param name="sheetName">The name of the worksheet to modify.</param> |
| 59 | + /// <param name="newSheetName">The new name to assign to the worksheet, or <c>null</c> to leave as is.</param> |
| 60 | + /// <param name="newSheetIndex">The position in the workbook to assign to the worksheet, or <c>null</c> to leave as is.</param> |
| 61 | + /// <param name="newSheetState">The visibility state to assign to the worksheet, or <c>null</c> to leave as is.</param> |
| 62 | + /// <param name="cancellationToken">The token to monitor for cancellation requests</param> |
| 63 | + [CreateSyncVersion] |
| 64 | + public async Task AlterSheetInfoAsync(string path, string sheetName, string? newSheetName = null, int? newSheetIndex = null, SheetState? newSheetState = null, CancellationToken cancellationToken = default) |
| 65 | + { |
| 66 | + var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); |
| 67 | + await using var disposableStream = stream.ConfigureAwait(false); |
| 68 | + |
| 69 | + await AlterSheetInfoAsync(stream, sheetName, newSheetName, newSheetIndex, newSheetState, cancellationToken).ConfigureAwait(false); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Modify the properties of a worksheet in the specified document. |
| 74 | + /// </summary> |
| 75 | + /// <param name="stream">The stream to the OpenXml document.</param> |
| 76 | + /// <param name="sheetName">The name of the worksheet to modify.</param> |
| 77 | + /// <param name="newSheetName">The new name to assign to the worksheet, or <c>null</c> to leave as is.</param> |
| 78 | + /// <param name="newSheetIndex">The position in the workbook to assign to the worksheet, or <c>null</c> to leave as is.</param> |
| 79 | + /// <param name="newSheetState">The visibility state to assign to the worksheet, or <c>null</c> to leave as is.</param> |
| 80 | + /// <param name="cancellationToken">The token to monitor for cancellation requests</param> |
| 81 | + [CreateSyncVersion] |
| 82 | + public async Task AlterSheetInfoAsync(Stream stream, string sheetName, string? newSheetName = null, int? newSheetIndex = null, SheetState? newSheetState = null, CancellationToken cancellationToken = default) |
| 83 | + { |
| 84 | + var internals = new OpenXmlEditorInternals(stream, true); |
| 85 | + await internals.AlterWorksheetAsync(sheetName, newSheetName, newSheetIndex, newSheetState, cancellationToken).ConfigureAwait(false); |
| 86 | + } |
| 87 | +} |
0 commit comments