NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
BasicFormulas.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2025
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System;
9using System.Globalization;
10using NanoXLSX.Utils;
11using FormatException = NanoXLSX.Exceptions.FormatException;
12
13namespace NanoXLSX
14{
18 public static class BasicFormulas
19 {
25 public static Cell Average(Range range)
26 { return Average(null, range); }
27
34 public static Cell Average(Worksheet target, Range range)
35 { return GetBasicFormula(target, range, "AVERAGE", null); }
36
43 public static Cell Ceil(Address address, int decimals)
44 { return Ceil(null, address, decimals); }
45
53 public static Cell Ceil(Worksheet target, Address address, int decimals)
54 { return GetBasicFormula(target, new Range(address, address), "ROUNDUP", decimals.ToString(CultureInfo.InvariantCulture)); }
55
62 public static Cell Floor(Address address, int decimals)
63 { return Floor(null, address, decimals); }
64
72 public static Cell Floor(Worksheet target, Address address, int decimals)
73 { return GetBasicFormula(target, new Range(address, address), "ROUNDDOWN", decimals.ToString(CultureInfo.InvariantCulture)); }
74
80 public static Cell Max(Range range)
81 { return Max(null, range); }
82
89 public static Cell Max(Worksheet target, Range range)
90 { return GetBasicFormula(target, range, "MAX", null); }
91
97 public static Cell Median(Range range)
98 { return Median(null, range); }
99
106 public static Cell Median(Worksheet target, Range range)
107 { return GetBasicFormula(target, range, "MEDIAN", null); }
108
114 public static Cell Min(Range range)
115 { return Min(null, range); }
116
123 public static Cell Min(Worksheet target, Range range)
124 { return GetBasicFormula(target, range, "MIN", null); }
125
132 public static Cell Round(Address address, int decimals)
133 { return Round(null, address, decimals); }
134
142 public static Cell Round(Worksheet target, Address address, int decimals)
143 { return GetBasicFormula(target, new Range(address, address), "ROUND", decimals.ToString(CultureInfo.InvariantCulture)); }
144
150 public static Cell Sum(Range range)
151 { return Sum(null, range); }
152
159 public static Cell Sum(Worksheet target, Range range)
160 { return GetBasicFormula(target, range, "SUM", null); }
161
162
172 public static Cell VLookup(object number, Range range, int columnIndex, bool exactMatch)
173 { return VLookup(number, null, range, columnIndex, exactMatch); }
174
185 public static Cell VLookup(object number, Worksheet rangeTarget, Range range, int columnIndex, bool exactMatch)
186 { return GetVLookup(null, new Address(), number, rangeTarget, range, columnIndex, exactMatch, true); }
187
197 public static Cell VLookup(Address address, Range range, int columnIndex, bool exactMatch)
198 { return VLookup(null, address, null, range, columnIndex, exactMatch); }
199
211 public static Cell VLookup(Worksheet queryTarget, Address address, Worksheet rangeTarget, Range range, int columnIndex, bool exactMatch)
212 {
213 return GetVLookup(queryTarget, address, 0, rangeTarget, range, columnIndex, exactMatch, false);
214 }
215
229 private static Cell GetVLookup(Worksheet queryTarget, Address address, object number, Worksheet rangeTarget, Range range, int columnIndex, bool exactMatch, bool numericLookup)
230 {
231 int rangeWidth = Math.Abs(range.EndAddress.Column - range.StartAddress.Column) + 1;
232 if (columnIndex < 1 || columnIndex > rangeWidth)
233 {
234 throw new FormatException("The column index on range " + range.ToString() + " can only be between 1 and " + rangeWidth);
235 }
236 CultureInfo culture = CultureInfo.InvariantCulture;
237 string arg1;
238 string arg2;
239 string arg3;
240 string arg4;
241 if (numericLookup)
242 {
243 if (number == null)
244 {
245 throw new FormatException("The lookup variable can only be a cell address or a numeric value. The passed value was null.");
246 }
247 Type t = number.GetType();
248 if (t == typeof(byte))
249 { arg1 = ((byte)number).ToString("G", culture); }
250 else if (t == typeof(sbyte))
251 { arg1 = ((sbyte)number).ToString("G", culture); }
252 else if (t == typeof(decimal))
253 { arg1 = ((decimal)number).ToString("G", culture); }
254 else if (t == typeof(double))
255 { arg1 = ((double)number).ToString("G", culture); }
256 else if (t == typeof(float))
257 { arg1 = ((float)number).ToString("G", culture); }
258 else if (t == typeof(int))
259 { arg1 = ((int)number).ToString("G", culture); }
260 else if (t == typeof(uint))
261 { arg1 = ((uint)number).ToString("G", culture); }
262 else if (t == typeof(long))
263 { arg1 = ((long)number).ToString("G", culture); }
264 else if (t == typeof(ulong))
265 { arg1 = ((ulong)number).ToString("G", culture); }
266 else if (t == typeof(short))
267 { arg1 = ((short)number).ToString("G", culture); }
268 else if (t == typeof(ushort))
269 { arg1 = ((ushort)number).ToString("G", culture); }
270 else
271 {
272 throw new FormatException("The lookup variable can only be a cell address or a numeric value. The value '" + number + "' is invalid.");
273 }
274 }
275 else
276 {
277 if (queryTarget != null)
278 { arg1 = queryTarget.SheetName + "!" + address; }
279 else { arg1 = address.ToString(); }
280 }
281 if (rangeTarget != null)
282 { arg2 = rangeTarget.SheetName + "!" + range; }
283 else { arg2 = range.ToString(); }
284 arg3 = ParserUtils.ToString(columnIndex);
285 if (exactMatch)
286 { arg4 = "TRUE"; }
287 else { arg4 = "FALSE"; }
288 return new Cell("VLOOKUP(" + arg1 + "," + arg2 + "," + arg3 + "," + arg4 + ")", Cell.CellType.Formula);
289 }
290
291
300 private static Cell GetBasicFormula(Worksheet target, Range range, string functionName, string postArg)
301 {
302 string arg1;
303 string arg2;
304 string prefix;
305 if (postArg == null)
306 { arg2 = ""; }
307 else { arg2 = "," + postArg; }
308 if (target != null)
309 { prefix = target.SheetName + "!"; }
310 else { prefix = ""; }
311 if (range.StartAddress.Equals(range.EndAddress))
312 { arg1 = prefix + range.StartAddress; }
313 else { arg1 = prefix + range; }
314 return new Cell(functionName + "(" + arg1 + arg2 + ")", Cell.CellType.Formula);
315 }
316 }
317
318}
Class for handling of basic Excel formulas.
static Cell Average(Range range)
Returns a cell with an average formula.
static Cell Sum(Range range)
Returns a cell with a sum formula.
static Cell VLookup(Worksheet queryTarget, Address address, Worksheet rangeTarget, Range range, int columnIndex, bool exactMatch)
Function to generate a Vlookup as Excel function.
static Cell Ceil(Address address, int decimals)
Returns a cell with a ceil formula.
static Cell Median(Worksheet target, Range range)
Returns a cell with a median formula.
static Cell Round(Worksheet target, Address address, int decimals)
Returns a cell with a round formula.
static Cell Sum(Worksheet target, Range range)
Returns a cell with a sum formula.
static Cell Median(Range range)
Returns a cell with a median formula.
static Cell Round(Address address, int decimals)
Returns a cell with a round formula.
static Cell Floor(Worksheet target, Address address, int decimals)
Returns a cell with a floor formula.
static Cell Average(Worksheet target, Range range)
Returns a cell with an average formula.
static Cell VLookup(Address address, Range range, int columnIndex, bool exactMatch)
Function to generate a Vlookup as Excel function.
static Cell Min(Worksheet target, Range range)
Returns a cell with a min formula.
static Cell Max(Range range)
Returns a cell with a max formula.
static Cell Min(Range range)
Returns a cell with a min formula.
static Cell Max(Worksheet target, Range range)
Returns a cell with a max formula.
static Cell VLookup(object number, Range range, int columnIndex, bool exactMatch)
Function to generate a Vlookup as Excel function.
static Cell VLookup(object number, Worksheet rangeTarget, Range range, int columnIndex, bool exactMatch)
Function to generate a Vlookup as Excel function.
static Cell Ceil(Worksheet target, Address address, int decimals)
Returns a cell with a ceil formula.
static Cell Floor(Address address, int decimals)
Returns a cell with a floor formula.
Class representing a cell of a worksheet.
Definition Cell.cs:24
Class for exceptions regarding format error incidents.
Class representing a worksheet of a workbook.
Definition Worksheet.cs:26
Struct representing the cell address as column and row (zero based).
Definition Address.cs:16
override string ToString()
Overwritten ToString method.
Definition Address.cs:102
bool Equals(Address other)
Compares two addresses whether they are equal.
Definition Address.cs:112
int Column
Column number (zero based).
Definition Address.cs:24
Struct representing a cell range with a start and end address.
Definition Range.cs:16
override string ToString()
Overwritten ToString method.
Definition Range.cs:136
Address StartAddress
Start address of the range.
Definition Range.cs:27
Address EndAddress
End address of the range.
Definition Range.cs:23