9using System.Globalization;
38 #region primitiveParsing
45 public static bool StartsWith(
string input,
string value)
47 if (input ==
null && value ==
null)
51 else if (input ==
null && value !=
null)
55 else if (value ==
null)
59 return input.StartsWith(value, StringComparison.InvariantCulture);
78 public static string ToUpper(
string input)
80 return !
string.IsNullOrEmpty(input) ? input.ToUpper(
InvariantCulture) : input;
88 public static string ToLower(
string input)
90 return !
string.IsNullOrEmpty(input) ? input.ToLower(
InvariantCulture) : input;
243 if (
string.IsNullOrEmpty(rawValue))
250 return value >= 1 ? 1 : 0;
255 return regularBool ? 1 : 0;
269 return bool.TryParse(rawValue, out parsedValue);
278 public static bool TryParseInt(
string rawValue, out
int parsedValue)
280 return int.TryParse(rawValue, NumberStyles.Integer,
InvariantCulture, out parsedValue);
291 return uint.TryParse(rawValue, NumberStyles.Integer,
InvariantCulture, out parsedValue);
302 return long.TryParse(rawValue, NumberStyles.Integer,
InvariantCulture, out parsedValue);
313 return ulong.TryParse(rawValue, NumberStyles.Integer,
InvariantCulture, out parsedValue);
324 return float.TryParse(rawValue, NumberStyles.Any, CultureInfo.InvariantCulture, out parsedValue);
335 return decimal.TryParse(rawValue, NumberStyles.Float,
InvariantCulture, out parsedValue);
345 public static bool TryParseDouble(
string rawValue, out
double parsedValue, NumberStyles numberStyles = NumberStyles.Any)
347 return double.TryParse(rawValue, numberStyles,
InvariantCulture, out parsedValue);
357 if (value ==
null || (!value.Contains(
'\n') && !value.Contains(
'\r')))
361 return value.Replace(
"\n\r",
"\n").Replace(
"\r\n",
"\n").Replace(
"\r",
"\n").Replace(
"\n",
"\r\n");
371 return character >=
'0' && character <=
'9';
375 #region formulaParsing
390 if (input ==
null) {
return "0"; }
391 else if (input is
string)
393 string stringValue = input as string;
394 return string.IsNullOrEmpty(stringValue) ?
"0" : stringValue;
396 else if (input is
bool)
398 if (convertBoolToNumber)
400 return (
bool)input ?
"1" :
"0";
404 return (
bool)input ?
"TRUE" :
"FALSE";
407 else if (input is
byte) {
return ToString((
byte)input); }
408 else if (input is sbyte) {
return ToString((sbyte)input); }
409 else if (input is decimal) {
return ToString((decimal)input); }
410 else if (input is
double) {
return ToString((
double)input); }
411 else if (input is
float) {
return ToString((
float)input); }
412 else if (input is
int) {
return ToString((
int)input); }
413 else if (input is uint) {
return ToString((uint)input); }
414 else if (input is
long) {
return ToString((
long)input); }
415 else if (input is ulong) {
return ToString((ulong)input); }
416 else if (input is
short) {
return ToString((
short)input); }
417 else if (input is ushort) {
return ToString((ushort)input); }
418 else if (input is DateTime)
422 else if (input is TimeSpan)
428 return input.ToString();
443 if (expression ==
null)
450 if (enclosingQuotesRemoved)
454 endIndex = expression.Length;
459 if (expression.Length < 2
460 || expression[0] !=
'"'
461 || expression[expression.Length - 1] !=
'"')
466 endIndex = expression.Length - 1;
469 StringBuilder builder =
new StringBuilder(endIndex - startIndex);
470 for (
int i = startIndex; i < endIndex; i++)
472 char current = expression[i];
475 builder.Append(current);
479 if (i + 1 >= endIndex || expression[i + 1] !=
'"')
487 value = builder.ToString();
492 #region referenceParsing
503 worksheetName =
null;
506 if (
string.IsNullOrEmpty(expression))
511 if (expression[0] !=
'\'')
513 int separatorIndex = expression.IndexOf(
'!');
514 if (separatorIndex <= 0 || separatorIndex == expression.Length - 1)
519 worksheetName = expression.Substring(0, separatorIndex);
520 reference = expression.Substring(separatorIndex + 1);
524 StringBuilder builder =
new StringBuilder();
525 for (
int i = 1; i < expression.Length; i++)
527 char current = expression[i];
530 builder.Append(current);
535 if (i + 1 < expression.Length && expression[i + 1] ==
'\'')
537 builder.Append(
'\'');
543 if (i + 1 >= expression.Length || expression[i + 1] !=
'!')
548 if (i + 2 >= expression.Length)
553 worksheetName = builder.ToString();
554 reference = expression.Substring(i + 2);
562 #region externalReferenceParsing
570 internal static bool ContainsExternalReference(
string formulaExpression)
572 if (
string.IsNullOrEmpty(formulaExpression) || formulaExpression.IndexOf(
'[') < 0)
577 bool inStringLiteral =
false;
578 for (
int i = 0; i < formulaExpression.Length; i++)
580 char current = formulaExpression[i];
583 if (inStringLiteral && i + 1 < formulaExpression.Length && formulaExpression[i + 1] ==
'"')
588 inStringLiteral = !inStringLiteral;
591 if (inStringLiteral || current !=
'[')
596 int closingBracket = formulaExpression.IndexOf(
']', i + 1);
597 if (closingBracket <= i + 1)
602 bool hasWorksheetName =
false;
603 for (
int j = closingBracket + 1; j < formulaExpression.Length; j++)
605 char referenceCharacter = formulaExpression[j];
606 if (referenceCharacter ==
'!')
608 if (hasWorksheetName)
614 if (referenceCharacter ==
'[' || referenceCharacter ==
']' || referenceCharacter ==
'"'
615 || referenceCharacter ==
'+' || referenceCharacter ==
'-' || referenceCharacter ==
'*'
616 || referenceCharacter ==
'/' || referenceCharacter ==
'^' || referenceCharacter ==
'&'
617 || referenceCharacter ==
'=' || referenceCharacter ==
'<' || referenceCharacter ==
'>'
618 || referenceCharacter ==
',' || referenceCharacter ==
';' || referenceCharacter ==
'('
619 || referenceCharacter ==
')' || referenceCharacter ==
'{' || referenceCharacter ==
'}')
623 if (!
char.IsWhiteSpace(referenceCharacter) && referenceCharacter !=
'\'')
625 hasWorksheetName =
true;
638 internal static bool IsValidExternalLinkId(
string identifier)
640 if (
string.IsNullOrEmpty(identifier) ||
641 identifier.Length < 3 ||
642 identifier[0] !=
'[' ||
643 identifier[identifier.Length - 1] !=
']')
648 for (
int i = 1; i < identifier.Length - 1; i++)
667 internal static bool TryReadExternalLinkId(
string expression,
int startIndex, out
int identifierLength)
669 identifierLength = 0;
670 if (
string.IsNullOrEmpty(expression) ||
671 startIndex < 0 || startIndex >= expression.Length || expression[startIndex] !=
'[')
676 int currentIndex = startIndex + 1;
677 if (currentIndex >= expression.Length ||
687 while (currentIndex < expression.Length &&
IsAsciiDigit(expression[currentIndex]));
688 if (currentIndex >= expression.Length || expression[currentIndex] !=
']')
693 int closingBracketIndex = currentIndex;
694 if (!HasValidPrefixBoundary(expression, startIndex))
698 if (!HasValidSuffixBoundary(expression, closingBracketIndex))
702 identifierLength = closingBracketIndex - startIndex + 1;
709 private static bool HasValidPrefixBoundary(
string expression,
int openingBracketIndex)
711 if (openingBracketIndex == 0)
716 char previous = expression[openingBracketIndex - 1];
718 if (previous ==
'\'')
723 return !IsNameCharacter(previous);
729 private static bool HasValidSuffixBoundary(
string expression,
int closingBracketIndex)
731 int nextIndex = closingBracketIndex + 1;
732 if (nextIndex >= expression.Length)
737 char next = expression[nextIndex];
749 if (
char.IsWhiteSpace(next))
784 private static bool IsNameCharacter(
char character)
786 return char.IsLetterOrDigit(character) ||
General data utils class with static methods.
static string GetOATimeString(TimeSpan time)
Method to convert a time into the internal Excel time format (OAdate without days).
static string GetOADateTimeString(DateTime date)
Method to convert a date or date and time into the internal Excel time format (OAdate).
Class providing static methods to parse string values to specific types or to print object as languag...
static string ToString(long input)
Transforms a long to an invariant sting.
const string NumericFormat
Numeric format for ToString conversions. This format ensures that a numeric value is printed in a lan...
static string ToString(byte input)
Transforms a byte to an invariant sting.
static readonly CultureInfo InvariantCulture
Constant for number conversion. The invariant culture (represents mostly the US numbering scheme) ens...
static bool TryParseDecimal(string rawValue, out decimal parsedValue)
Tries to parse a decimal (with float parsing style) independent from the culture info of the host.
static string ToString(double input)
Transforms a double to an invariant sting.
static bool TryParseFloat(string rawValue, out float parsedValue)
Tries to parse a float (with any parsing style) independent from the culture info of the host.
static string ToString(ulong input)
Transforms a ulong to an invariant sting.
static bool StartsWith(string input, string value)
Determines whether a string starts with a specific value.
static string ToUpper(string input)
Transforms a string to upper case with null check and invariant culture.
static string ToString(short input)
Transforms a short to an invariant sting.
static string ToString(sbyte input)
Transforms a sbyte to an invariant sting.
static string ToString(decimal input)
Transforms a decimal to an invariant sting.
static double ParseDouble(string rawValue)
Parses a double independent from the culture info of the host.
static string ToCachedValueString(object input, bool convertBoolToNumber=true)
Transforms a given object to a string displayed as cached Values. The common known compatible numeric...
static string ToLower(string input)
Transforms a string to lower case with null check and invariant culture.
static bool TryParseInt(string rawValue, out int parsedValue)
Tries to parse an int independent of the culture info of the host.
static bool TryParseLong(string rawValue, out long parsedValue)
Tries to parse a long independent from the culture info of the host.
static bool TryParseUlong(string rawValue, out ulong parsedValue)
Tries to parse an unsigned long (ulong) independent from the culture info of the host.
static string ToString(uint input)
Transforms a uint to an invariant sting.
static string ToString(ushort input)
Transforms a ushort to an invariant sting.
static float ParseFloat(string rawValue)
Parses a float independent from the culture info of the host.
static bool NotStartsWith(string input, string value)
Determines whether a string does not start with a specific value.
static int ParseInt(string rawValue)
Parses an int independent from the culture info of the host.
static bool TryParseUint(string rawValue, out uint parsedValue)
Tries to parse an unsigned int (uint) independent from the culture info of the host.
static bool TryParseBool(string rawValue, out bool parsedValue)
Tries to parse a bool from its string name (true/false), independent from the case.
static bool TryParseDouble(string rawValue, out double parsedValue, NumberStyles numberStyles=NumberStyles.Any)
Tries to parse a double independent from the culture info of the host.
static bool IsAsciiDigit(char character)
Determines whether the passed character is a ASCII digit character (0-9).
static bool TryParseFormulaStringConstant(string expression, out string value, bool enclosingQuotesRemoved=false)
Tries to parse a raw string as an Excel formula string constant. Escaped double quotes ("") are conve...
static string ToString(float input)
Transforms a float to an invariant sting.
static string NormalizeNewLines(string value)
Normalizes all newlines of a string to CR+LF.
static bool TryParseWorksheetQualifiedReference(string expression, out string worksheetName, out string reference)
Tries to parse a qualifies worksheet name and address or range expression from a raw string.
static int ParseBinaryBool(String rawValue)
Parses a bool as a binary number either based on an int (0/1) or a string expression (true/ false),...
static string ToString(int input)
Transforms an integer to an invariant sting.