8using System.Collections.Generic;
32 private string filename;
33 private List<Worksheet> worksheets;
36 private IPassword workbookProtectionPassword;
37 private bool lockWindowsIfProtected;
38 private bool lockStructureIfProtected;
39 private int selectedWorksheet;
41 private readonly List<Color> mruColors =
new List<Color>();
42 private readonly List<DefinedName> definedNames =
new List<DefinedName>();
43 internal bool importInProgress;
52 internal AuxiliaryData AuxiliaryData {
get;
private set; }
59 get {
return shortener; }
68 get {
return currentWorksheet; }
79 get {
return filename; }
80 set { filename = value; }
88 get {
return lockStructureIfProtected; }
96 get {
return lockWindowsIfProtected; }
104 get {
return workbookMetadata; }
105 set { workbookMetadata = value; }
113 get {
return selectedWorksheet; }
133 get {
return worksheets; }
151 internal FeatureSet Features {
get; } =
new FeatureSet() { };
195 this.filename = filename;
205 public Workbook(
string filename,
string sheetName,
bool sanitizeSheetName)
208 this.filename = filename;
209 if (sanitizeSheetName)
239 mruColors.Add(color);
309 if (worksheet ==
null)
313 if (cellAddress ==
null)
315 throw new WorksheetException(
"The cell address pointing to a defined name cannot be null");
317 return AddDefinedName(name,
DefinedName.
NameType.Cell, cellAddress, worksheet, localWorksheet, comment);
351 Range range =
new Range(startAddress, endAddress);
370 Range range =
new Range(startColumn, startRow, endColumn, endRow);
386 if (worksheet ==
null)
390 return AddDefinedName(name,
DefinedName.
NameType.Range, rangeAddress, worksheet, localWorksheet, comment);
408 throw new WorksheetException(
"A constant value pointing to a defined name cannot be nul");
410 return AddDefinedName(name,
DefinedName.
NameType.Constant, value,
null, localWorksheet, comment);
428 if (
string.IsNullOrWhiteSpace(formula))
430 throw new WorksheetException(
"A formula value pointing to a defined name cannot be null or empty");
433 return AddDefinedName(name,
DefinedName.
NameType.Formula, formula,
null, localWorksheet, comment);
449 DefinedName definedName =
new DefinedName(
this, type, name, value, targetWorksheet, localWorksheet, comment);
450 AddDefinedName(definedName);
458 internal void AddDefinedName(
DefinedName definedName)
460 definedNames.Add(definedName);
485 int index = FindDefinedNameIndex(name, localSheet);
493 InvalidateDefinedNameReferences(definedName);
495 definedName.Features.Remove(Features);
496 definedNames.RemoveAt(index);
506 for (
int i = definedNames.Count - 1; i >= 0; i--)
509 InvalidateDefinedNameReferences(definedName);
510 definedName.Features.Remove(Features);
511 definedNames.RemoveAt(i);
523 int index = FindDefinedNameIndex(name, localSheet);
524 return index < 0 ? null : definedNames[index];
542 internal int FindDefinedNameIndex(
string name,
Worksheet localSheet)
544 for (
int i = 0; i < definedNames.Count; i++)
547 if (
string.Equals(candidate.
Name, name, System.StringComparison.OrdinalIgnoreCase)
548 && ReferenceEquals(candidate.
LocalSheet, localSheet))
570 throw new WorksheetException(
"The worksheet with the name '" + name +
"' already exists.");
573 int number = GetNextWorksheetId();
575 currentWorksheet = newWs;
576 worksheets.Add(newWs);
577 newWs.Features.Add(Features);
578 shortener.SetCurrentWorksheetInternal(currentWorksheet);
590 if (sanitizeSheetName)
621 if (sanitizeSheetName)
624 worksheet.SheetName = name;
628 if (
string.IsNullOrEmpty(worksheet.
SheetName))
632 for (
int i = 0; i < worksheets.Count; i++)
634 if (worksheets[i].SheetName == worksheet.
SheetName)
640 worksheet.SheetID = GetNextWorksheetId();
641 currentWorksheet = worksheet;
642 worksheets.Add(worksheet);
643 worksheet.WorkbookReference =
this;
644 worksheet.Features.Add(Features);
655 Worksheet worksheetToRemove = worksheets.FirstOrDefault(w => w.SheetName == name);
656 if (worksheetToRemove ==
null)
658 throw new WorksheetException(
"The worksheet with the name '" + name +
"' does not exist.");
660 int index = worksheets.IndexOf(worksheetToRemove);
661 bool resetCurrentWorksheet = worksheetToRemove == currentWorksheet;
674 if (index < 0 || index >= worksheets.Count)
678 bool resetCurrentWorksheet = worksheets[index] == currentWorksheet;
687 internal void ResolveMergedCells()
689 foreach (
Worksheet worksheet
in worksheets)
691 worksheet.ResolveMergedCells();
704 shortener.SetCurrentWorksheetInternal(currentWorksheet);
705 return currentWorksheet;
717 shortener.SetCurrentWorksheetInternal(currentWorksheet);
718 return currentWorksheet;
728 int index = worksheets.IndexOf(worksheet);
731 throw new WorksheetException(
"The passed worksheet object is not in the worksheet collection.");
733 currentWorksheet = worksheets[index];
734 shortener.SetCurrentWorksheetInternal(worksheet);
744 int index = worksheets.FindIndex(w => w.SheetName == name);
747 throw new WorksheetException(
"No worksheet with the name '" + name +
"' was found in this workbook.");
749 selectedWorksheet = index;
761 if (worksheetIndex < 0 || worksheetIndex > worksheets.Count - 1)
763 throw new RangeException(
"The worksheet index " + worksheetIndex +
" is out of range");
765 selectedWorksheet = worksheetIndex;
766 ValidateWorksheets();
777 selectedWorksheet = worksheets.IndexOf(worksheet);
778 if (selectedWorksheet < 0)
780 throw new WorksheetException(
"The passed worksheet object is not in the worksheet collection.");
782 ValidateWorksheets();
793 int index = worksheets.FindIndex(w => w.SheetName == name);
796 throw new WorksheetException(
"No worksheet with the name '" + name +
"' was found in this workbook.");
798 return worksheets[index];
809 if (index < 0 || index > worksheets.Count - 1)
811 throw new RangeException(
"The worksheet index " + index +
" is out of range");
813 return worksheets[index];
825 lockWindowsIfProtected = protectWindows;
826 lockStructureIfProtected = protectStructure;
827 workbookProtectionPassword.SetPassword(password);
828 if (!protectWindows && !protectStructure)
849 return CopyWorksheetTo(sourceWorksheet, newWorksheetName,
this, sanitizeSheetName);
863 return CopyWorksheetTo(sourceWorksheet, newWorksheetName,
this, sanitizeSheetName);
876 return CopyWorksheetTo(sourceWorksheet, newWorksheetName,
this, sanitizeSheetName);
888 public Worksheet CopyWorksheetTo(
string sourceWorksheetName,
string newWorksheetName, Workbook targetWorkbook,
bool sanitizeSheetName =
true)
891 return CopyWorksheetTo(sourceWorksheet, newWorksheetName, targetWorkbook, sanitizeSheetName);
903 public Worksheet CopyWorksheetTo(
int sourceWorksheetIndex,
string newWorksheetName, Workbook targetWorkbook,
bool sanitizeSheetName =
true)
906 return CopyWorksheetTo(sourceWorksheet, newWorksheetName, targetWorkbook, sanitizeSheetName);
921 if (targetWorkbook ==
null)
925 if (sourceWorksheet ==
null)
946 internal void ValidateWorksheets()
948 if (importInProgress)
953 int worksheetCount = worksheets.Count;
954 if (worksheetCount == 0)
958 for (
int i = 0; i < worksheetCount; i++)
962 if (i == selectedWorksheet)
964 throw new WorksheetException(
"The worksheet with the index " + selectedWorksheet +
" cannot be set as selected, since it is set hidden");
974 private void InvalidateDefinedNameReferences(DefinedName definedName)
976 foreach (Worksheet worksheet
in worksheets)
978 if (!worksheet.Features.ContainsDefinedNameReferences)
982 foreach (KeyValuePair<string, Cell> cell
in worksheet.
Cells)
984 if (cell.Value.DataType == Cell.CellType.Formula)
986 FormulaData formula = cell.Value.Formula;
989 formula.DefinedNameReference =
null;
1004 worksheets[index].Features.Remove(Features);
1005 worksheets.RemoveAt(index);
1006 if (worksheets.Count > 0)
1008 for (
int i = 0; i < worksheets.Count; i++)
1010 worksheets[i].SheetID = i + 1;
1012 if (resetCurrentWorksheet)
1014 currentWorksheet = worksheets[worksheets.Count - 1];
1016 if (selectedWorksheet == index || selectedWorksheet > worksheets.Count - 1)
1018 selectedWorksheet = worksheets.Count - 1;
1023 currentWorksheet =
null;
1024 selectedWorksheet = 0;
1026 ValidateWorksheets();
1033 private int GetNextWorksheetId()
1035 if (worksheets.Count == 0)
1039 return worksheets.Max(w => w.SheetID) + 1;
1047 worksheets =
new List<Worksheet>();
1048 workbookMetadata =
new Metadata();
1049 shortener =
new Shortener(
this);
1050 workbookProtectionPassword =
new LegacyPassword(LegacyPassword.PasswordType.WorkbookProtection);
1051 AuxiliaryData =
new AuxiliaryData();
Compound class representing a color in various representations (RGB, indexed, theme,...
static Color CreateRgb(SrgbColor color)
Creates a Color from an RGB/ARGB color.
Class representing a defined name within a workbook. A defined name is a descriptive text that repres...
Worksheet LocalSheet
Gets the worksheet that scopes (constraint) this defined name. If null, the defined name has workbook...
string Name
Gets the name of the defined name as it appears in the workbook (e.g. MyRange).
NameType
Enum to specify the type of the defined name.
Class for exceptions regarding range incidents (e.g. out-of-range).
Class for exceptions regarding worksheet incidents.
Class to register plug-in classes that extends the functionality of NanoXLSX (Core or any other packa...
static bool Initialize()
Initializes the plug-in loader process. If already initialized, the method returns without action.
Class to provide access to the current worksheet with a shortened syntax.
Class representing an Office theme.
Class providing general validator methods.
static void ValidateGenericColor(string hexCode, bool allowEmpty=false)
Validates the passed string, whether it is a valid RGB or ARGB value that can be used for Fills,...
void AddWorksheet(Worksheet worksheet, bool sanitizeSheetName)
Adding a new Worksheet. The new worksheet will be defined as current worksheet.
bool Hidden
Gets or sets whether the whole workbook is hidden.
Workbook(string filename, string sheetName, bool sanitizeSheetName)
Constructor with filename ant the name of the first worksheet.
void SetSelectedWorksheet(Worksheet worksheet)
Sets the selected worksheet in the output workbook.
void ClearDefinedNames()
Removes all defined names of the workbook and removes their references from formula cells.
void AddMruColor(string color)
Adds a color value (HEX; 6-digit RGB or 8-digit ARGB) to the MRU list.
Workbook()
Default constructor. No initial worksheet is created. Use AddWorksheet(string) (or overloads) to add ...
void ClearMruColors()
Clears the MRU color list.
void SetCurrentWorksheet(Worksheet worksheet)
Sets the current worksheet.
void SetSelectedWorksheet(int worksheetIndex)
Sets the selected worksheet in the output workbook.
int SelectedWorksheet
Gets the selected worksheet. The selected worksheet is not the current worksheet while design time bu...
void AddWorksheet(string name, bool sanitizeSheetName)
Adding a new Worksheet with a sanitizing option. The new worksheet will be defined as current workshe...
void RemoveWorksheet(int index)
Removes the defined worksheet based on its index. If the worksheet is the current or selected workshe...
Worksheet CopyWorksheetIntoThis(int sourceWorksheetIndex, string newWorksheetName, bool sanitizeSheetName=true)
Copies a worksheet of the current workbook by its index.
Shortener WS
Gets the shortener object for the current worksheet.
DefinedName AddDefinedNameCell(string name, Worksheet worksheet, int column, int row, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a single cell, to the workbook.
DefinedName AddDefinedNameRange(string name, Worksheet worksheet, string rangeAddress, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a cell range, to the workbook.
void SetWorkbookProtection(bool state, bool protectWindows, bool protectStructure, string password)
Sets or removes the workbook protection. If protectWindows and protectStructure are both false,...
Theme WorkbookTheme
Gets or sets the theme of the workbook. The default is defined by Theme.GetDefaultTheme....
DefinedName GetDefinedName(string name, Worksheet localSheet=null)
Gets the defined name with the supplied name and scope.
DefinedName AddDefinedNameRange(string name, Worksheet worksheet, Address startAddress, Address endAddress, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a cell range, to the workbook.
string Filename
Gets or sets the filename of the workbook.
Workbook(string sheetName)
Constructor with additional parameter to create a default worksheet with the specified name....
Workbook(bool createWorkSheet)
Constructor with additional parameter to create a default worksheet. This constructor can be used to ...
void AddWorksheet(Worksheet worksheet)
Adding a new Worksheet. The new worksheet will be defined as current worksheet.
IReadOnlyList< Color > GetMruColors()
Gets the MRU color list.
Worksheet GetWorksheet(int index)
Gets a worksheet from this workbook by index.
bool LockStructureIfProtected
Gets whether the structure are locked if workbook is protected. See also SetWorkbookProtection.
Workbook(string filename, string sheetName)
Constructor with filename ant the name of the first worksheet.
void SetSelectedWorksheet(string name)
Sets the selected worksheet in the output workbook.
Worksheet SetCurrentWorksheet(int worksheetIndex)
Sets the current worksheet.
DefinedName AddDefinedNameCell(string name, Worksheet worksheet, Address cellAddress, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a single cell, to the workbook.
Worksheet CopyWorksheetTo(string sourceWorksheetName, string newWorksheetName, Workbook targetWorkbook, bool sanitizeSheetName=true)
Copies a worksheet of the current workbook by its name into another workbook.
void AddMruColor(Color color)
Adds a generic color value. This can be an RGB/ARGB color, Auto, Theme, Indexed or System color.
IReadOnlyList< DefinedName > GetDefinedNames()
Gets a read-only view of all defined names in this workbook in insertion order.
DefinedName AddDefinedNameCell(string name, Worksheet worksheet, string cellAddress, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a single cell, to the workbook.
Worksheet CurrentWorksheet
Gets the current worksheet.
Worksheet CopyWorksheetTo(int sourceWorksheetIndex, string newWorksheetName, Workbook targetWorkbook, bool sanitizeSheetName=true)
Copies a worksheet of the current workbook by its index into another workbook.
List< Worksheet > Worksheets
Gets the list of worksheets in the workbook.
void AddWorksheet(string name)
Adding a new Worksheet. The new worksheet will be defined as current worksheet.
bool RemoveDefinedName(string name, Worksheet localSheet=null)
Removes the defined name with the supplied name and scope.
Metadata WorkbookMetadata
Meta data object of the workbook.
bool LockWindowsIfProtected
Gets whether the windows are locked if workbook is protected. See also SetWorkbookProtection.
bool UseWorkbookProtection
Gets or sets whether the workbook is protected.
virtual IPassword WorkbookProtectionPassword
Password instance of the protected workbook. If a password was set, the pain text representation and ...
Worksheet CopyWorksheetIntoThis(Worksheet sourceWorksheet, string newWorksheetName, bool sanitizeSheetName=true)
Copies a worksheet of any workbook into the current workbook.
Worksheet CopyWorksheetIntoThis(string sourceWorksheetName, string newWorksheetName, bool sanitizeSheetName=true)
Copies a worksheet of the current workbook by its name.
Worksheet SetCurrentWorksheet(string name)
Sets the current worksheet.
DefinedName AddDefinedNameFormula(string name, string formula, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a formula expression, to the workbook. Do not add a leading equal si...
DefinedName AddDefinedNameRange(string name, Worksheet worksheet, Range rangeAddress, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a cell range, to the workbook.
DefinedName AddDefinedNameRange(string name, Worksheet worksheet, int startColumn, int startRow, int endColumn, int endRow, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a cell range, to the workbook.
void RemoveWorksheet(string name)
Removes the defined worksheet based on its name. If the worksheet is the current or selected workshee...
DefinedName AddDefinedNameConstant(string name, object value, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a constant value, to the workbook.
Worksheet GetWorksheet(string name)
Gets a worksheet from this workbook by name.
static Worksheet CopyWorksheetTo(Worksheet sourceWorksheet, string newWorksheetName, Workbook targetWorkbook, bool sanitizeSheetName=true)
Copies a worksheet of any workbook into the another workbook.
Class representing a worksheet of a workbook.
Worksheet Copy()
Creates a (dereferenced) deep copy of this worksheet.
string SheetName
Gets or sets the name of the worksheet.
void SetSheetName(string name)
Validates and sets the worksheet name.
static string SanitizeWorksheetName(string input, Workbook workbook)
Sanitizes a worksheet name.
IReadOnlyDictionary< string, Cell > Cells
Gets the cells of the worksheet as read-only dictionary with the cell address string as key and the c...
Interface to represent a protection password, either for workbooks or worksheets. The implementations...
Struct representing the cell address as column and row (zero based).
Struct representing a cell range with a start and end address.