Skip to content

Commit 5ff3b0a

Browse files
author
fileformat-cells
authored
Merge pull request #4 from fahadadeel/main
Add "ProtectWorksheet" Feature
2 parents 49b5068 + d92958b commit 5ff3b0a

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

FileFormat.Cells/Worksheet.cs

+34-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void saveDataToSheet(int sheetIndex)
143143
}
144144

145145
// Save the worksheet data
146-
worksheet.Save();
146+
//worksheet.Save();
147147

148148

149149

@@ -172,6 +172,39 @@ public void AddFormulaToCell(string cellRef, UInt32 rowIndex, string formula)
172172
sheetData.sheetData.Append(row);
173173
}
174174

175+
public void ProtectSheet(string password, int sheetIndex)
176+
{
177+
SheetProtection sheetProtection = new SheetProtection()
178+
{
179+
Sheet = true,
180+
Objects = true,
181+
Scenarios = true,
182+
Password = password,
183+
};
184+
185+
var targetSheet = this.spreadsheetDocument.WorkbookPart.WorksheetParts.ElementAt(sheetIndex).Worksheet;
186+
187+
if (targetSheet.Elements<SheetProtection>().Any())
188+
{
189+
targetSheet.Elements<SheetProtection>().First().Remove();
190+
}
191+
192+
targetSheet.InsertAt(sheetProtection, 0);
193+
}
194+
195+
/// <summary>
196+
/// Unprotects the worksheet.
197+
/// </summary>
198+
public void UnProtectSheet()
199+
{
200+
// Remove existing SheetProtection if exists
201+
if (worksheet.Elements<SheetProtection>().Any())
202+
{
203+
worksheet.Elements<SheetProtection>().First().Remove();
204+
}
205+
}
206+
207+
175208
private static List<UInt32> AddStyle(ref Stylesheet stylesheet)
176209
{
177210
UInt32 Fontid = 0, Fillid = 0, Cellformatid = 0;

0 commit comments

Comments
 (0)