Skip to content

Commit

Permalink
Merge pull request #14 from fahadadeel/main
Browse files Browse the repository at this point in the history
Implement Enhanced CopyRange Feature with Workbook and Worksheet Class Modifications
  • Loading branch information
fahadadeel authored Feb 22, 2024
2 parents c4ea869 + e5ea08c commit c15e783
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 25 deletions.
13 changes: 9 additions & 4 deletions FileFormat.Cells.sln
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 25.0.1705.6
# Visual Studio Version 17
VisualStudioVersion = 17.9.34607.119
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileFormat.Cells", "FileFormat.Cells\FileFormat.Cells.csproj", "{347E8540-1317-4E99-BF8A-A16A04E753BB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileFormat.Cells", "FileFormat.Cells\FileFormat.Cells.csproj", "{347E8540-1317-4E99-BF8A-A16A04E753BB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileFormat.Cells_Tests", "FileFormat.Cells_Tests\FileFormat.Cells_Tests.csproj", "{A9BFD275-A6CD-4CD7-B039-8CB29CDE5C93}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileFormat.Cells_Tests", "FileFormat.Cells_Tests\FileFormat.Cells_Tests.csproj", "{A9BFD275-A6CD-4CD7-B039-8CB29CDE5C93}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
netcore3.1|Any CPU = netcore3.1|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{347E8540-1317-4E99-BF8A-A16A04E753BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{347E8540-1317-4E99-BF8A-A16A04E753BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{347E8540-1317-4E99-BF8A-A16A04E753BB}.netcore3.1|Any CPU.ActiveCfg = netcore3.1|Any CPU
{347E8540-1317-4E99-BF8A-A16A04E753BB}.netcore3.1|Any CPU.Build.0 = netcore3.1|Any CPU
{347E8540-1317-4E99-BF8A-A16A04E753BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{347E8540-1317-4E99-BF8A-A16A04E753BB}.Release|Any CPU.Build.0 = Release|Any CPU
{A9BFD275-A6CD-4CD7-B039-8CB29CDE5C93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A9BFD275-A6CD-4CD7-B039-8CB29CDE5C93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A9BFD275-A6CD-4CD7-B039-8CB29CDE5C93}.netcore3.1|Any CPU.ActiveCfg = netcore3.1|Any CPU
{A9BFD275-A6CD-4CD7-B039-8CB29CDE5C93}.netcore3.1|Any CPU.Build.0 = netcore3.1|Any CPU
{A9BFD275-A6CD-4CD7-B039-8CB29CDE5C93}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A9BFD275-A6CD-4CD7-B039-8CB29CDE5C93}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
Expand Down
24 changes: 20 additions & 4 deletions FileFormat.Cells/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
using DocumentFormat.OpenXml;
using System.Globalization;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Packaging;

namespace FileFormat.Cells
{
public sealed class Cell
{

private readonly DocumentFormat.OpenXml.Spreadsheet.Cell _cell;
private readonly WorkbookPart _workbookPart;

private readonly SheetData _sheetData;

/// <summary>
Expand All @@ -23,10 +27,11 @@ public sealed class Cell
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="cell"/> or <paramref name="sheetData"/> is null.
/// </exception>
public Cell(DocumentFormat.OpenXml.Spreadsheet.Cell cell, SheetData sheetData)
public Cell(DocumentFormat.OpenXml.Spreadsheet.Cell cell, SheetData sheetData, WorkbookPart workbookPart)
{
_cell = cell ?? throw new ArgumentNullException(nameof(cell));
_sheetData = sheetData ?? throw new ArgumentNullException(nameof(sheetData));
_workbookPart = workbookPart ?? throw new ArgumentNullException(nameof(workbookPart));
}

/// <summary>
Expand Down Expand Up @@ -82,9 +87,20 @@ public void PutFormula(string formula)
/// Gets the value of the cell.
/// </summary>
/// <returns>The cell value as a string.</returns>
public string GetValue()
{
return _cell.CellValue?.Text;
public string GetValue()
{
if (_cell == null || _cell.CellValue == null) return "";

if (_cell.DataType != null && _cell.DataType.Value == CellValues.SharedString)
{
int index = int.Parse(_cell.CellValue.Text);
SharedStringTablePart sharedStrings = _workbookPart.SharedStringTablePart;
return sharedStrings.SharedStringTable.ElementAt(index).InnerText;
}
else
{
return _cell.CellValue.Text;
}
}

/// <summary>
Expand Down
21 changes: 17 additions & 4 deletions FileFormat.Cells/FileFormat.Cells.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>10.0</LangVersion>

<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Configurations>Debug;Release;netcore3.1</Configurations>
</PropertyGroup>

<ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup>
<LangVersion>10.0</LangVersion>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.20.0" />
</ItemGroup>
</Project>
</Project>
4 changes: 3 additions & 1 deletion FileFormat.Cells/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using A14 = DocumentFormat.OpenXml.Office2010.Drawing;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Linq;

using System.IO;
using System;

namespace FileFormat.Cells
{

Expand Down
4 changes: 3 additions & 1 deletion FileFormat.Cells/ImageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
using DocumentFormat.OpenXml.Spreadsheet;
using System.Linq;
using DocumentFormat.OpenXml.Drawing.Spreadsheet;

using System;
using System.IO;

namespace FileFormat.Cells
{
public class ImageHandler
Expand Down
37 changes: 37 additions & 0 deletions FileFormat.Cells/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:13841",
"sslPort": 44390
}
},
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5061",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true
},
"https": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:7274;http://localhost:5061",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
9 changes: 5 additions & 4 deletions FileFormat.Cells/Workbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Workbook()

// Adding your Worksheet object to Worksheets list
//var newWorksheet = new Worksheet(worksheetPart, worksheetPart.Worksheet);
var newWorksheet = Worksheet.WorksheetFactory.CreateInstance(worksheetPart, worksheetPart.Worksheet);
var newWorksheet = Worksheet.WorksheetFactory.CreateInstance(worksheetPart, worksheetPart.Worksheet, workbookpart);
this.Worksheets.Add(newWorksheet);

this.stylesPart = this.spreadsheetDocument.WorkbookPart.AddNewPart<WorkbookStylesPart>();
Expand Down Expand Up @@ -124,8 +124,9 @@ private void InitializeWorksheets()
{
var worksheetPart = (WorksheetPart)(this.workbookpart.GetPartById(sheet.Id));
var worksheet = worksheetPart.Worksheet;
var workbookPart = this.workbookpart;
var sheetData = worksheet.Elements<SheetData>().FirstOrDefault() ?? new SheetData();
this.Worksheets.Add(Worksheet.WorksheetFactory.CreateInstance(worksheetPart, worksheetPart.Worksheet));
this.Worksheets.Add(Worksheet.WorksheetFactory.CreateInstance(worksheetPart, worksheetPart.Worksheet, workbookPart));
}
}

Expand Down Expand Up @@ -228,7 +229,7 @@ public Worksheet AddSheet(string sheetName)
newWorksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);

// Create a new Worksheet object and add it to Worksheets list
var newWorksheet = Worksheet.WorksheetFactory.CreateInstance(newWorksheetPart, newWorksheetPart.Worksheet);
var newWorksheet = Worksheet.WorksheetFactory.CreateInstance(newWorksheetPart, newWorksheetPart.Worksheet, this.workbookpart);
this.Worksheets.Add(newWorksheet);

// Append a new sheet and associate it with the workbook
Expand Down Expand Up @@ -286,7 +287,7 @@ private void SyncWorksheets()
var wp = (WorksheetPart)(this.workbookpart.GetPartById(sh.Id));
var ws = wp.Worksheet;
var sd = ws.Elements<SheetData>().FirstOrDefault() ?? new SheetData();
this.Worksheets.Add(Worksheet.WorksheetFactory.CreateInstance(wp, wp.Worksheet));
this.Worksheets.Add(Worksheet.WorksheetFactory.CreateInstance(wp, wp.Worksheet, this.workbookpart));
}
}

Expand Down
45 changes: 39 additions & 6 deletions FileFormat.Cells/Worksheet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using DocumentFormat.OpenXml;
Expand All @@ -16,11 +18,11 @@ public sealed class Worksheet
{
private WorksheetPart _worksheetPart;
private SheetData _sheetData;

private DocumentFormat.OpenXml.Spreadsheet.Cell sourceCell;
public const double DefaultColumnWidth = 8.43; // Default width in character units
public const double DefaultRowHeight = 15.0; // Default height in points


private WorkbookPart _workbookPart;
/// <summary>
/// Gets the CellIndexer for the worksheet. This property provides indexed access to the cells of the worksheet.
/// </summary>
Expand All @@ -41,15 +43,17 @@ public sealed class Worksheet
/// <exception cref="InvalidOperationException">
/// Thrown if SheetData is not found in the provided worksheet.
/// </exception>
private Worksheet(WorksheetPart worksheetPart, DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet)
private Worksheet(WorksheetPart worksheetPart, DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet, WorkbookPart workbookPart)
{
_worksheetPart = worksheetPart ?? throw new ArgumentNullException(nameof(worksheetPart));

_sheetData = worksheet?.Elements<SheetData>().FirstOrDefault()
?? throw new InvalidOperationException("SheetData not found in the worksheet.");
_workbookPart = workbookPart ?? throw new ArgumentNullException(nameof(workbookPart));

// Initialize the Cells property
this.Cells = new CellIndexer(this);

}

/// <summary>
Expand All @@ -61,9 +65,9 @@ private Worksheet(WorksheetPart worksheetPart, DocumentFormat.OpenXml.Spreadshee
/// <returns>A new instance of the Worksheet class.</returns>
public class WorksheetFactory
{
public static Worksheet CreateInstance(WorksheetPart worksheetPart, DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet)
public static Worksheet CreateInstance(WorksheetPart worksheetPart, DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet, WorkbookPart workbookPart)
{
return new Worksheet(worksheetPart, worksheet);
return new Worksheet(worksheetPart, worksheet, workbookPart);
}
}

Expand Down Expand Up @@ -123,7 +127,7 @@ public string Name
public Cell GetCell(string cellReference)
{
// This logic used to be in your indexer
return new Cell(GetOrCreateCell(cellReference), _sheetData);
return new Cell(GetOrCreateCell(cellReference), _sheetData, _workbookPart);
}

/// <summary>
Expand Down Expand Up @@ -1604,8 +1608,36 @@ public void AddComment(string cellReference, Comment comment)
_worksheetPart.Worksheet.Save();
}

public void CopyRange(Range sourceRange, string targetStartCellReference)
{
var (targetStartRow, targetStartColumn) = ParseCellReference(targetStartCellReference);

uint rowOffset = targetStartRow - sourceRange.StartRowIndex;
uint columnOffset = targetStartColumn - sourceRange.StartColumnIndex;

for (uint row = sourceRange.StartRowIndex; row <= sourceRange.EndRowIndex; row++)
{
for (uint column = sourceRange.StartColumnIndex - 1; column < sourceRange.EndColumnIndex; column++)
{
// Calculate target cell's row and column indices
uint targetRow = row + rowOffset;
uint targetColumn = column + columnOffset;

// Construct source and target cell references
string sourceCellRef = $"{IndexToColumnLetter((int)column)}{row}";
string targetCellRef = $"{IndexToColumnLetter((int)targetColumn)}{targetRow}";

this.Cells[targetCellRef].PutValue(this.Cells[sourceCellRef].GetValue());
}
}

// Save the worksheet to apply changes
_worksheetPart.Worksheet.Save();
}




}

public class CellIndexer
Expand Down Expand Up @@ -1635,6 +1667,7 @@ public Cell this[string cellReference]
return _worksheet.GetCell(cellReference);
}
}

}
}

3 changes: 2 additions & 1 deletion FileFormat.Cells_Tests/FileFormat.Cells_Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net70</TargetFramework>
<LangVersion>10.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<Configurations>Debug;Release;netcore3.1</Configurations>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit c15e783

Please sign in to comment.