/// <summary>Gets the value of a specific property. /// </summary> /// <typeparam name="TEnum">The type of the value which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="propertyName">The name of the property to search.</param> /// <param name="value">The value of the property (output).</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation of the enumeration <typeparamref name="TEnum"/>.</param> /// <param name="propertyValueColumnIndex">The null-based index of the column which contains the value, the second column is standard.</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a specific enumeration.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static ExcelPropertyValueState TryGetPropertyValue <TEnum>(this IExcelDataQuery dataQuery, string propertyName, out TEnum value, EnumStringRepresentationUsage enumStringRepresentationUsage, int propertyValueColumnIndex = 1) where TEnum : struct, IComparable, IConvertible, IFormattable { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } int rowIndex; if (dataQuery.TryGetRowIndexOfPropertyName(propertyName, out rowIndex) == false) { value = default(TEnum); return(ExcelPropertyValueState.NoPropertyFound); } ExcelCellValueState state = dataQuery.TryGetValue <TEnum>(enumStringRepresentationUsage, out value, rowIndex, propertyValueColumnIndex); if (state == ExcelCellValueState.ProperValue) { return(ExcelPropertyValueState.ProperProperty); } else if (state == ExcelCellValueState.EmptyOrMissingExcelCell) { return(ExcelPropertyValueState.ValueIsEmptyExcelCell); } else { return(ExcelPropertyValueState.NoValidValue); } }
/// <summary>Gets the value of a specific pool with respect to a specific [optional] property name. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="dataQuery">The data query, i.e. property name/value pairs.</param> /// <param name="propertyName">The name of the property to search.</param> /// <param name="tryGetPoolElement">A method to pick a specific object.</param> /// <param name="value">On input this is a standard value of the property; on exit this argument will be changed if and only if a property with /// name <paramref name="propertyName"/> exists and contains valid data.</param> /// <param name="dataAdvice">Data advice, i.e. a list of possible outcome to improve the useability, perhaps <c>null</c>.</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if the user input is invalid, i.e. the name of the pool item is not given in its <see cref="System.String"/> representation.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static bool TryGetOptionalPropertyPoolValue <T>(this IExcelDataQuery dataQuery, string propertyName, ExcelDataQuery.tTryGetPoolElement <T> tryGetPoolElement, ref T value, IExcelDataAdvice dataAdvice = null) { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } string objectName; ExcelPropertyValueState state = dataQuery.TryGetPropertyValue <String>(propertyName, out objectName, dataAdvice); if (state == ExcelPropertyValueState.ProperProperty) { T tempValue; if (tryGetPoolElement(objectName, out tempValue) == true) { value = tempValue; return(true); } throw new ArgumentException("No valid input for property '" + propertyName + "' found; '" + objectName + "' is invalid input" + GetFormatedDataQueryName(dataQuery) + "."); } else if ((state == ExcelPropertyValueState.NoPropertyFound) || (state == ExcelPropertyValueState.ValueIsEmptyExcelCell)) { return(false); } throw new ArgumentException("No valid input for property '" + propertyName + "' found" + GetFormatedDataQueryName(dataQuery) + "."); }
/// <summary>Gets the value of a specific property. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="propertyName">The name of the property to search.</param> /// <param name="value">The value of the property (output).</param> /// <param name="dataAdvice">A data advice for a the property value, i.e. possible outcome to improve the useability.</param> /// <param name="propertyValueColumnIndex">The null-based index of the column which contains the value.</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="T"/> represents the type of a specific enumeration.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static ExcelPropertyValueState TryGetPropertyValue <T>(this IExcelDataQuery dataQuery, string propertyName, out T value, IExcelDataAdvice dataAdvice = null, int propertyValueColumnIndex = 1) { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } int rowIndex; if (dataQuery.TryGetRowIndexOfPropertyName(propertyName, out rowIndex) == false) { value = default(T); return(ExcelPropertyValueState.NoPropertyFound); } ExcelCellValueState state = dataQuery.TryGetValue <T>(out value, rowIndex, propertyValueColumnIndex, dataAdvice); if (state == ExcelCellValueState.ProperValue) { return(ExcelPropertyValueState.ProperProperty); } else if (state == ExcelCellValueState.EmptyOrMissingExcelCell) { return(ExcelPropertyValueState.ValueIsEmptyExcelCell); } else { return(ExcelPropertyValueState.NoValidValue); } }
/// <summary>Gets the [optional] value of a specific pool with respect to a specific position of a <see cref="IExcelDataQuery"/> object. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="dataQuery">The data query, i.e. property name/value pairs.</param> /// <param name="tryGetPoolElement">A method to pick a specific object.</param> /// <param name="value">On input this is a standard value; on exist this argument will be changed if and only if <paramref name="dataQuery"/> contains valid data at the desired position.</param> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <param name="dataAdvice">Data advice, i.e. a list of possible outcome to improve the useability, perhaps <c>null</c>.</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data; or the Excel cell is empty.</returns> /// <exception cref="ArgumentException">Thrown, if the user input is invalid, i.e. the name of the pool item is not given its <see cref="System.String"/> representation.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static bool TryGetOptionalPoolValue <T>(this IExcelDataQuery dataQuery, ExcelDataQuery.tTryGetPoolElement <T> tryGetPoolElement, ref T value, int rowIndex = 0, int columnIndex = 0, IExcelDataAdvice dataAdvice = null) { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } string objectName; ExcelCellValueState state = dataQuery.TryGetValue <String>(out objectName, rowIndex, columnIndex, dataAdvice); if (state == ExcelCellValueState.ProperValue) { T tempValue; if (tryGetPoolElement(objectName, out tempValue) == true) { value = tempValue; return(true); } } else if (state == ExcelCellValueState.EmptyOrMissingExcelCell) { return(false); } throw new ArgumentException(dataQuery.ToString(rowIndex, columnIndex) + " is no valid input" + GetFormatedDataQueryName(dataQuery) + "."); }
/// <summary>Gets the value of a specific pool with respect to a specific position of a <see cref="IExcelDataQuery"/> object. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="dataQuery">The data query, i.e. property name/value pairs.</param> /// <param name="tryGetPoolElement">A method to pick a specific object.</param> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <param name="dataAdvice">Data advice, i.e. a list of possible outcome to improve the useability, perhaps <c>null</c>.</param> /// <returns>The value.</returns> /// <exception cref="ArgumentException">Thrown, if the user input is invalid.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static T GetRequiredPoolValue <T>(this IExcelDataQuery dataQuery, ExcelDataQuery.tTryGetPoolElement <T> tryGetPoolElement, int rowIndex = 0, int columnIndex = 0, IExcelDataAdvice dataAdvice = null) { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } string objectName; ExcelCellValueState state = dataQuery.TryGetValue <String>(out objectName, rowIndex, columnIndex, dataAdvice); T value; if (state == ExcelCellValueState.ProperValue) { if (tryGetPoolElement(objectName, out value) == true) { return(value); } else { throw new ArgumentException(dataQuery.ToString(rowIndex, columnIndex) + " is no valid name" + GetFormatedDataQueryName(dataQuery) + "."); } } else if (state == ExcelCellValueState.EmptyOrMissingExcelCell) { throw new ArgumentException("Valid pool element name required" + GetFormatedDataQueryName(dataQuery) + "."); } throw new ArgumentException(dataQuery.ToString(rowIndex, columnIndex) + " is no valid input" + GetFormatedDataQueryName(dataQuery) + "."); }
/// <summary>Gets the value of a specific required property. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="dataQuery">The data query, i.e. property name/value pairs.</param> /// <param name="propertyName">The name of the property to search.</param> /// <param name="tryGetPoolElement">A method to pick a specific object.</param> /// <param name="dataAdvice">Data advice, i.e. a list of possible outcome to improve the useability, perhaps <c>null</c>.</param> /// <returns>The value of the property.</returns> /// <exception cref="ArgumentException">Thrown, if no property or valid value is given by the user.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static T GetRequiredPropertyPoolValue <T>(this IExcelDataQuery dataQuery, string propertyName, ExcelDataQuery.tTryGetPoolElement <T> tryGetPoolElement, IExcelDataAdvice dataAdvice = null) { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } string objectName; ExcelPropertyValueState state = dataQuery.TryGetPropertyValue <String>(propertyName, out objectName, dataAdvice); if (state == ExcelPropertyValueState.NoPropertyFound) { throw new ArgumentException("No property with name '" + propertyName + " ' found" + GetFormatedDataQueryName(dataQuery) + "."); } else if (state == ExcelPropertyValueState.NoValidValue) { throw new ArgumentException("No valid input for property '" + propertyName + "' found" + GetFormatedDataQueryName(dataQuery) + "."); } else if (state == ExcelPropertyValueState.ValueIsEmptyExcelCell) { throw new ArgumentException("Input required for property '" + propertyName + "'" + GetFormatedDataQueryName(dataQuery) + "."); } T value; if (tryGetPoolElement(objectName, out value) == true) { return(value); } throw new ArgumentException("No valid input for property '" + propertyName + "' found" + GetFormatedDataQueryName(dataQuery) + "."); }
public static object GetExcelPoolItemLogging( [ExcelArgument(Name = "objectName", Description = "The name of the object", AllowReference = true)] object xlObjectName) { try { IExcelDataQuery objectNameQuery = ExcelDataQuery.Create(xlObjectName); string objectName; if (objectNameQuery.TryGetValue <string>(out objectName, dataAdvice: ExcelDataAdvice.Create(ExcelPool.GetObjectNames())) != ExcelCellValueState.ProperValue) { return(ExcelDataConverter.GetExcelRangeErrorMessage("Invalid object name '" + objectNameQuery.ToString(0, 0) + "'.")); } objectNameQuery.QueryCompleted(); ILoggedObject loggingObject; if (ExcelPool.TryGetObject <ILoggedObject>(objectName, out loggingObject) == false) { return(ExcelDataConverter.GetExcelRangeErrorMessage("No object found of name '" + objectName + "' which supports logging functionality.")); } var excelLogFile = loggingObject.Logging as ExcelObjectLogger; if (excelLogFile == null) { return(ExcelDataConverter.GetExcelRangeErrorMessage("No log file available.")); } // return ExcelDataConverter.GetExcelRangeOutput<string>(excelLogFile.GetAsStringArray()); throw new NotImplementedException(); } catch (Exception e) { return(ExcelDataConverter.GetExcelRangeErrorMessage(e.Message)); } }
/// <summary>Gets the name of the file. /// </summary> /// <param name="xlPath">The path.</param> /// <param name="xlFileName">The name of the file (without path).</param> /// <param name="fileExtensionSearchPattern">A collection of possible file extensions.</param> /// <returns>The file name including path.</returns> private static string GetFileName(object xlPath, object xlFileName, IEnumerable <IdentifierString> fileExtensionSearchPattern) { IExcelDataQuery pathDataQuery = ExcelDataQuery.Create(xlPath); IExcelDataQuery fileNameDataQuery = ExcelDataQuery.Create(xlFileName); string path; if (pathDataQuery.TryGetValue <string>(out path) != ExcelCellValueState.ProperValue) { throw new ArgumentException("No valid path!"); } List <string> possibleFileNameList = new List <string>(); foreach (var fileExtension in fileExtensionSearchPattern) { string[] possibleFileNames = Directory.GetFiles(path, "*." + fileExtension.String, SearchOption.TopDirectoryOnly); if ((possibleFileNames != null) && (possibleFileNames.Length > 0)) { possibleFileNameList.AddRange(possibleFileNames); } } string fileName; if (fileNameDataQuery.TryGetValue <string>(out fileName, dataAdvice: ExcelDataAdvice.Create(possibleFileNameList, possibleFileName => Path.GetFileName(possibleFileName))) != ExcelCellValueState.ProperValue) { throw new ArgumentException("No valid file name!"); } return(Path.Combine(path, fileName)); }
public static object TryEvaluateUDF( [ExcelArgument(Name = "dodoni UDF result", Description = "The Excel cell to check whether the result of a Dodoni.net UDF is meaningful")] object xlDodoniUDFResult) { IExcelDataQuery excelDataQuery = ExcelDataQuery.Create(xlDodoniUDFResult); string value; if (excelDataQuery.TryGetValue <string>(out value) == ExcelCellValueState.ProperValue) { if (value.StartsWith("Error!", true, CultureInfo.InvariantCulture) == true) // here, we just check whether some Error message starts with "Error!" { if (excelDataQuery.IsSingleCell == true) { return(String.Empty); } object[,] emptyOutput = new object[excelDataQuery.RowCount, excelDataQuery.ColumnCount]; for (int j = 0; j < excelDataQuery.RowCount; j++) { for (int k = 0; k < excelDataQuery.ColumnCount; k++) { emptyOutput[j, k] = String.Empty; } } return(emptyOutput); } } return(xlDodoniUDFResult); }
/// <summary>Gets the name of a specific <see cref="IExcelDataQuery"/> object in a specific <see cref="System.String"/> representation. /// </summary> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <returns>The name of <paramref name="dataQuery"/> in a specific <see cref="System.String"/> representation, i.e. add '[' and ']'; <see cref="System.String.Empty"/> if no name available.</returns> public static string GetFormatedDataQueryName(this IExcelDataQuery dataQuery) { if ((dataQuery == null) | (dataQuery.Name == null) || (dataQuery.Name.String.Length == 0)) { return(String.Empty); } return(" [" + dataQuery.Name.String + "]"); }
public static object ListObjectsFromFile( [ExcelArgument(Name = "path", Description = "The path", AllowReference = true)] object xlPath, [ExcelArgument(Name = "fileName", Description = "The file name", AllowReference = true)] object xlFileName, [ExcelArgument(Name = "fileFormat", Description = "[Optional] The file format; the file extension will be used by default", AllowReference = true)] object xlFileFormat) { try { IExcelDataQuery fileFormatDataQuery = ExcelDataQuery.Create(xlFileFormat, "File format"); string fileName; IObjectStreamer objectStreamer; if (fileFormatDataQuery.IsEmpty == true) { fileName = GetFileName(xlPath, xlFileName, ObjectStreamer.GetFileExtensions()); if (ObjectStreamer.TryGetObjectStreamerByFileExtension(ExtendedPath.GetExtension(fileName), out objectStreamer) == false) { throw new ArgumentException("Invalid file extension '" + ExtendedPath.GetExtension(fileName) + "', used default file extensions or specify the file format."); } } else { if (fileFormatDataQuery.TryGetPoolValue <IObjectStreamer>(ObjectStreamer.TryGetObjectStreamer, out objectStreamer, dataAdvice: ExcelDataAdvice.Create(ObjectStreamer.GetNames())) == false) { throw new ArgumentException("Invalid file format " + fileFormatDataQuery.ToString(0, 0) + "."); } fileName = GetFileName(xlPath, xlFileName, objectStreamer.FileExtension); fileFormatDataQuery.QueryCompleted(); } StreamReader streamReader = new StreamReader(fileName); IObjectStreamReader objectStreamReader = objectStreamer.GetStreamReader(streamReader); var objectNames = ExcelPool.GetObjectNames(objectStreamReader); int n = objectNames.Count(); object[,] output = new object[n + 1, 2]; output[0, 0] = "Name"; output[0, 1] = "Type"; int j = 1; foreach (var obj in objectNames) { output[j, 0] = obj.Item2; output[j, 1] = obj.Item1.Name.String; j++; } objectStreamReader.Close(); return(ExcelDataConverter.GetExcelRangeOutput(output)); } catch (Exception e) { return(ExcelDataConverter.GetExcelRangeErrorMessage(e)); } }
/// <summary>Gets the value of a specific Excel cell. /// </summary> /// <typeparam name="TEnum">The type of the output which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="value">The value (output).</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a enumeration.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static ExcelCellValueState TryGetValue <TEnum>(this IExcelDataQuery dataQuery, out TEnum value, EnumStringRepresentationUsage enumStringRepresentationUsage, int rowIndex = 0, int columnIndex = 0) where TEnum : struct, IComparable, IConvertible, IFormattable { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } return(dataQuery.TryGetValue <TEnum>(enumStringRepresentationUsage, out value, rowIndex, columnIndex)); }
/// <summary>Gets the null-based column index of a specific header name. /// </summary> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="headerName">The name of the header to search.</param> /// <returns>The null-based index of the column which contains in the first row <paramref name="headerName"/>.</returns> /// <exception cref="ArgumentException">Thrown, if <paramref name="dataQuery"/> does not contain a header with name <paramref name="headerName"/>.</exception> public static int GetHeaderNameColumnIndex(this IExcelDataQuery dataQuery, string headerName) { int columnNameHeaderIndex; if (TryGetHeaderNameColumnIndex(dataQuery, headerName, out columnNameHeaderIndex) == true) { return(columnNameHeaderIndex); } throw new ArgumentException("No column header found with name '" + headerName + "'" + GetFormatedDataQueryName(dataQuery) + "."); }
/// <summary>Gets the null-based column index of a specific header name. /// </summary> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="isHeaderName">A delegate that determines the name of the header to search; the argument is in the original representation, i.e. including whitespaces etc.</param> /// <param name="rawHeaderName">A 'raw header name' which is used in the <see cref="System.String"/> representation of the <see cref="ArgumentException"/> thrown in the case that no specified header name is found.</param> /// <returns>The null-based index of the column which contains in the first row the desired header name.</returns> /// <exception cref="ArgumentException">Thrown, if <paramref name="dataQuery"/> does not contain a header with respect to <paramref name="isHeaderName"/>.</exception> public static int GetHeaderNameColumnIndex(this IExcelDataQuery dataQuery, Func <string, bool> isHeaderName, string rawHeaderName = "") { int columnNameHeaderIndex; if (TryGetHeaderNameColumnIndex(dataQuery, isHeaderName, out columnNameHeaderIndex) == true) { return(columnNameHeaderIndex); } throw new ArgumentException(String.Format("No column header found with name '<{0}>' {1}.", rawHeaderName, GetFormatedDataQueryName(dataQuery))); }
/// <summary>Gets the level of details, i.e. the value of the optional property 'Level of Details'. /// </summary> /// <param name="generalPropertyExcelDataQuery">A <see cref="IExcelDataQuery"/> object that contains general properties.</param> /// <param name="propertyValueColumnIndex">The null-based index of the column which contains the value, the second column is standard.</param> /// <returns>A value indicating the level of detail for the output, i.e. for the Pool inspector etc.</returns> public static InfoOutputDetailLevel GetLevelOfDetails(this IExcelDataQuery generalPropertyExcelDataQuery, int propertyValueColumnIndex = 1) { if (generalPropertyExcelDataQuery == null) { throw new ArgumentNullException("generalPropertyExcelDataQuery"); } InfoOutputDetailLevel levelOfDetails = InfoOutputDetailLevel.Full; generalPropertyExcelDataQuery.TryGetOptionalPropertyValue <InfoOutputDetailLevel>("Level of Details", ref levelOfDetails, EnumStringRepresentationUsage.StringAttribute, propertyValueColumnIndex); return(levelOfDetails); }
public static object LoadObjectsFromFile( [ExcelArgument(Name = "path", Description = "The path", AllowReference = true)] object xlPath, [ExcelArgument(Name = "fileName", Description = "The file name", AllowReference = true)] object xlFileName, [ExcelArgument(Name = "objectNames", Description = "[Optional] A list of object names to load; all objects will be loaded by default", AllowReference = true)] object xlObjectNames, [ExcelArgument(Name = "fileFormat", Description = "[Optional] The file format; the file extension will be used by default", AllowReference = true)] object xlFileFormat) { try { IExcelDataQuery fileFormatDataQuery = ExcelDataQuery.Create(xlFileFormat, "File format"); string fileName; IObjectStreamer objectStreamer; if (fileFormatDataQuery.IsEmpty == true) { fileName = GetFileName(xlPath, xlFileName, ObjectStreamer.GetFileExtensions()); if (ObjectStreamer.TryGetObjectStreamerByFileExtension(ExtendedPath.GetExtension(fileName), out objectStreamer) == false) { throw new ArgumentException("Invalid file extension '" + Path.GetExtension(fileName) + "', used default file extensions or specify the file format."); } } else { if (fileFormatDataQuery.TryGetPoolValue <IObjectStreamer>(ObjectStreamer.TryGetObjectStreamer, out objectStreamer, dataAdvice: ExcelDataAdvice.Create(ObjectStreamer.GetNames())) == false) { throw new ArgumentException("Invalid file format " + fileFormatDataQuery.ToString(0, 0) + "."); } fileName = GetFileName(xlPath, xlFileName, objectStreamer.FileExtension); fileFormatDataQuery.QueryCompleted(); } IExcelDataQuery objectNamesDataQuery = ExcelDataQuery.Create(xlObjectNames, "Object names"); IEnumerable <string> objectNames = objectNamesDataQuery.GetColumnVector <string>(); objectNamesDataQuery.QueryCompleted(); StreamReader streamReader = new StreamReader(fileName); IObjectStreamReader objectStreamReader = objectStreamer.GetStreamReader(streamReader); string infoMessage; IEnumerable <ExcelPoolItem> excelPoolItems; ExcelPool.TryLoadObjectsByName(objectStreamReader, objectNames, out infoMessage, out excelPoolItems); objectStreamReader.Close(); return(infoMessage.ToTimeStampString()); } catch (Exception e) { return(ExcelDataConverter.GetExcelRangeErrorMessage(e)); } }
public static object IsError( [ExcelArgument(Name = "dodoni UDF result", Description = "The Excel cell to check whether the result of a Dodoni.net UDF is meaningful")] object xlDodoniUDFResult) { IExcelDataQuery excelDataQuery = ExcelDataQuery.Create(xlDodoniUDFResult); string value; if (excelDataQuery.TryGetValue <string>(out value) == ExcelCellValueState.ProperValue) { return(value.StartsWith("Error!", true, CultureInfo.InvariantCulture)); // here, we just check whether some Error message starts with "Error!" } return(false); }
/// <summary>Gets the value of a specific cell. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <param name="dataAdvice">A data advice for a the property value, i.e. possible outcome to improve the useability.</param> /// <returns>The value of the cell at the desired position.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="T"/> represents a enumeration or no valid input is given by the user.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static T GetValue <T>(this IExcelDataQuery dataQuery, int rowIndex = 0, int columnIndex = 0, IExcelDataAdvice dataAdvice = null) { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } T value; if (dataQuery.TryGetValue <T>(out value, rowIndex, columnIndex, dataAdvice) == ExcelCellValueState.ProperValue) { return(value); } throw new ArgumentException("No valid input '" + dataQuery.ToString(rowIndex, columnIndex) + "' at row: " + rowIndex + ", column: " + columnIndex + " found" + GetFormatedDataQueryName(dataQuery) + "."); }
/// <summary>Gets the value of a specific cell. /// </summary> /// <typeparam name="TEnum">The type of the output which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation.</param> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a enumeration or no valid input is given by the user.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static TEnum GetValue <TEnum>(this IExcelDataQuery dataQuery, EnumStringRepresentationUsage enumStringRepresentationUsage, int rowIndex = 0, int columnIndex = 0) where TEnum : struct, IComparable, IConvertible, IFormattable { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } TEnum value; if (dataQuery.TryGetValue <TEnum>(enumStringRepresentationUsage, out value, rowIndex, columnIndex) == ExcelCellValueState.ProperValue) { return(value); } throw new ArgumentException("No valid input '" + dataQuery.ToString(rowIndex, columnIndex) + "' at row: " + rowIndex + ", column: " + columnIndex + " found" + GetFormatedDataQueryName(dataQuery) + "."); }
/// <summary>Gets the value of a specific [optional] property. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="propertyName">The name of the property to search.</param> /// <param name="value">On input this is a standard value of the property; on exit this argument will be changed if and only if a property with /// name <paramref name="propertyName"/> exists and contains valid data.</param> /// <param name="dataAdvice">A data advice for a the property value, i.e. possible outcome to improve the useability.</param> /// <param name="propertyValueColumnIndex">The null-based index of the column which contains the value.</param> /// <returns>A value indicating whether <paramref name="value"/> has been changed with user input.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="T"/> represents the type of a specific enumeration or no <b>valid</b> user input is given, i.e. /// a property of name <paramref name="propertyName"/> exists but the value is non-empty and can not convert to <typeparamref name="T"/>.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> /// <remarks>Use this method if some standard values are available and the user has the option to change the standard setting.</remarks> public static bool TryGetOptionalPropertyValue <T>(this IExcelDataQuery dataQuery, string propertyName, ref T value, IExcelDataAdvice dataAdvice = null, int propertyValueColumnIndex = 1) { T tempValue; ExcelPropertyValueState state = TryGetPropertyValue <T>(dataQuery, propertyName, out tempValue, dataAdvice, propertyValueColumnIndex); if (state == ExcelPropertyValueState.NoValidValue) { throw new ArgumentException("No valid input for property '" + propertyName + "' found" + GetFormatedDataQueryName(dataQuery) + "."); } else if (state == ExcelPropertyValueState.ProperProperty) { value = tempValue; return(true); } return(false); }
/// <summary>Gets the null-based column index of a specific header name. /// </summary> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="headerName">The name of the header to search.</param> /// <param name="columnIndex">The null-based index of the column which contains in the first row <paramref name="headerName"/> (output).</param> /// <returns>A value indicating whether <paramref name="columnIndex"/> contains valid data.</returns> public static bool TryGetHeaderNameColumnIndex(this IExcelDataQuery dataQuery, string headerName, out int columnIndex) { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } if (headerName == null) { throw new ArgumentNullException("headerName"); } if (headerName.Length == 0) { throw new ArgumentException(String.Format(ExceptionMessages.ArgumentIsInvalid, "<empty>"), "headerName"); } string idHeaderName = headerName.ToIDString(); for (int j = 0; j < dataQuery.ColumnCount; j++) { string value; switch (dataQuery.TryGetValue <string>(out value, rowIndex: 0, columnIndex: j)) { case ExcelCellValueState.EmptyOrMissingExcelCell: break; case ExcelCellValueState.NoValidValue: throw new ArgumentException("Invalid header, ' " + dataQuery.ToString(0, j) + "' does not represent a string" + GetFormatedDataQueryName(dataQuery) + "."); case ExcelCellValueState.ProperValue: if (value != null) { if (idHeaderName == value.ToIDString()) { columnIndex = j; return(true); } } break; default: throw new NotImplementedException(); } } columnIndex = -1; return(false); }
/// <summary>Gets the value of a specific [optional] property. /// </summary> /// <typeparam name="TEnum">The type of the value which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="propertyName">The name of the property to search.</param> /// <param name="value">On input this is a standard value of the property; on exit this argument will be changed if and only if a property with /// name <paramref name="propertyName"/> exists and contains valid data.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation of the enumeration <typeparamref name="TEnum"/>.</param> /// <param name="propertyValueColumnIndex">The null-based index of the column which contains the value, the second column is standard.</param> /// <returns>A value indicating whether <paramref name="value"/> has been changed with user input.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="TEnum"/> does not represents a specific enumeration or no <b>valid</b> user input is given, i.e. /// a property of name <paramref name="propertyName"/> exists but the value is non-empty and can not convert to <typeparamref name="TEnum"/>.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> /// <remarks>Use this method if some standard values are available and the user has the option to change the standard setting.</remarks> public static bool TryGetOptionalPropertyValue <TEnum>(this IExcelDataQuery dataQuery, string propertyName, ref TEnum value, EnumStringRepresentationUsage enumStringRepresentationUsage, int propertyValueColumnIndex = 1) where TEnum : struct, IComparable, IConvertible, IFormattable { TEnum tempValue; ExcelPropertyValueState state = TryGetPropertyValue <TEnum>(dataQuery, propertyName, out tempValue, enumStringRepresentationUsage, propertyValueColumnIndex); if (state == ExcelPropertyValueState.NoValidValue) { throw new ArgumentException("No valid input for property '" + propertyName + "' found" + GetFormatedDataQueryName(dataQuery) + "."); } else if (state == ExcelPropertyValueState.ProperProperty) { value = tempValue; return(true); } return(false); }
/// <summary>Gets the set of null-based (excluding header) row indices of a specified <see cref="DataTable"/> object. /// </summary> /// <param name="rowDataQuery">The row data query, which contains the null-based (excluding header) row indices.</param> /// <param name="dataTable">The data table.</param> /// <param name="rowCount">The number of row indices, i.e. of the return value (output).</param> /// <returns>The set of null-based row indices; <c>null</c> if the set of row indices is empty.</returns> private static IEnumerable <int> GetRowIndices(IExcelDataQuery rowDataQuery, DataTable dataTable, out int rowCount) { if (rowDataQuery.IsEmpty == true) { rowCount = dataTable.Rows.Count; return(GetNullbasedIndices(rowCount)); } if (rowDataQuery.ColumnCount > 1) { throw new ArgumentException("The Excel Range that contains the row indices must be a single row or column."); } SortedSet <int> rowIndices = new SortedSet <int>(); for (int j = 0; j < rowDataQuery.RowCount; j++) { int rowIndex; switch (rowDataQuery.TryGetValue <int>(out rowIndex, j)) { case ExcelCellValueState.ProperValue: if ((rowIndex < 0) || (rowIndex > dataTable.Rows.Count)) { throw new ArgumentException("Invalid row index '" + rowDataQuery.ToString(j, 0) + "'."); } rowIndices.Add(rowIndex); break; case ExcelCellValueState.NoValidValue: throw new ArgumentException("Invalid row index '" + rowDataQuery.ToString(j, 0) + "'."); case ExcelCellValueState.EmptyOrMissingExcelCell: break; // nothing to do here default: throw new NotImplementedException(); } } if (rowIndices.Count == 0) { rowCount = dataTable.Rows.Count; return(GetNullbasedIndices(rowCount)); } rowCount = rowIndices.Count; return(rowIndices); }
/// <summary>Gets the value of a specific required property. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="propertyName">The name of the property to search.</param> /// <param name="dataAdvice">A data advice for a the property value, i.e. possible outcome to improve the useability.</param> /// <param name="propertyValueColumnIndex">The null-based index of the column which contains the value.</param> /// <returns>The value of the property.</returns> /// <exception cref="ArgumentException">Thrown, if no property or valid value is given by the user or if /// <typeparamref name="T"/> represents the type of a specific enumeration.</exception> public static T GetRequiredPropertyValue <T>(this IExcelDataQuery dataQuery, string propertyName, IExcelDataAdvice dataAdvice = null, int propertyValueColumnIndex = 1) { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } T value; int rowIndex; if (dataQuery.TryGetRowIndexOfPropertyName(propertyName, out rowIndex) == false) { throw new ArgumentException("No property with name '" + propertyName + "' found" + GetFormatedDataQueryName(dataQuery) + "."); } if (dataQuery.TryGetValue <T>(out value, rowIndex, propertyValueColumnIndex, dataAdvice) == ExcelCellValueState.ProperValue) { return(value); } throw new ArgumentException("No valid input for property '" + propertyName + "' found" + GetFormatedDataQueryName(dataQuery) + "."); }
/// <summary>Gets the value of a specific required property. /// </summary> /// <typeparam name="TEnum">The type of the value which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="propertyName">The name of the property to search.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation of the enumeration <typeparamref name="TEnum"/>.</param> /// <param name="propertyValueColumnIndex">The null-based index of the column which contains the value, the second column is standard.</param> /// <returns>The value of the property.</returns> /// <exception cref="ArgumentException">Thrown, if no property or valid value is given by the user or if /// <typeparamref name="TEnum"/> does not represents a specific enumeration.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static TEnum GetRequiredPropertyValue <TEnum>(this IExcelDataQuery dataQuery, string propertyName, EnumStringRepresentationUsage enumStringRepresentationUsage, int propertyValueColumnIndex = 1) where TEnum : struct, IComparable, IConvertible, IFormattable { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } TEnum value; int rowIndex; if (dataQuery.TryGetRowIndexOfPropertyName(propertyName, out rowIndex) == false) { throw new ArgumentException("No property with name '" + propertyName + "' found" + GetFormatedDataQueryName(dataQuery) + "."); } if (dataQuery.TryGetValue <TEnum>(enumStringRepresentationUsage, out value, rowIndex, propertyValueColumnIndex) == ExcelCellValueState.ProperValue) { return(value); } throw new ArgumentException("No valid input for property '" + propertyName + "' found" + GetFormatedDataQueryName(dataQuery) + "."); }
/// <summary>Gets the name of the file. /// </summary> /// <param name="xlPath">The path.</param> /// <param name="xlFileName">The name of the file (without path).</param> /// <param name="fileExtensionSearchPattern">The file extension to search.</param> /// <returns>The file name including path.</returns> private static string GetFileName(object xlPath, object xlFileName, IdentifierString fileExtensionSearchPattern) { IExcelDataQuery pathDataQuery = ExcelDataQuery.Create(xlPath); IExcelDataQuery fileNameDataQuery = ExcelDataQuery.Create(xlFileName); string path; if (pathDataQuery.TryGetValue <string>(out path) != ExcelCellValueState.ProperValue) { throw new ArgumentException("No valid path!"); } string[] possibleFileNames = Directory.GetFiles(path, "*." + fileExtensionSearchPattern.String, SearchOption.TopDirectoryOnly); string fileName; if (fileNameDataQuery.TryGetValue <string>(out fileName, dataAdvice: ExcelDataAdvice.Create(possibleFileNames, possibleFileName => Path.GetFileName(possibleFileName))) != ExcelCellValueState.ProperValue) { throw new ArgumentException("No valid file name!"); } return(Path.Combine(path, fileName)); }
/// <summary>Gets the [optional] value of a specific Excel cell. /// </summary> /// <typeparam name="T">The type of the output.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="value">On input this is a standard value; on exist this argument will be changed if and only if <paramref name="dataQuery"/> contains valid data at the desired position.</param> /// <param name="rowIndex">The null-based index of the row.</param> /// <param name="columnIndex">The null-based index of the column.</param> /// <param name="dataAdvice">Data advice, i.e. a list of possible outcome to improve the useability, perhaps <c>null</c>.</param> /// <returns>A value indicating whether <paramref name="value"/> has been changed and set to some user input.</returns> /// <exception cref="ArgumentException">Thrown, if <typeparamref name="T"/> represents an enumeration or if the user input at the desired position can not converted to <typeparamref name="T"/>.</exception> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> public static bool TryGetOptionalValue <T>(this IExcelDataQuery dataQuery, ref T value, int rowIndex = 0, int columnIndex = 0, IExcelDataAdvice dataAdvice = null) { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } T tempValue; ExcelCellValueState state = dataQuery.TryGetValue <T>(out tempValue, rowIndex, columnIndex, dataAdvice); if (state == ExcelCellValueState.NoValidValue) { throw new ArgumentException("No valid input '" + dataQuery.ToString(rowIndex, columnIndex) + "'" + GetFormatedDataQueryName(dataQuery) + "."); } else if (state == ExcelCellValueState.ProperValue) { value = tempValue; return(true); } return(false); }
/// <summary>Sets an optional property value, i.e. if a specific property value is available the value will be used /// to change the state of a specific object, otherwise the standard value will be use. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="dataQuery">The property stream.</param> /// <param name="propertyName">The name of the property.</param> /// <param name="trySetPropertyValue">A delegate which is used to set the value of the property for a specific object, if the property value is available.</param> /// <param name="dataAdvice">A data advice for a the property value, i.e. possible outcome to improve the useability.</param> /// <param name="propertyValueColumnIndex">The null-based index of the column which contains the value.</param> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> /// <remarks>Use this method for properties which have some standard value if the user does not /// enter a specific value, for example the standard tolerance used for a optimizer algorithm is 1e-7 if the /// user does not enter a different value.</remarks> public static void SetOptionalPropertyValue <T>(this IExcelDataQuery dataQuery, string propertyName, Func <T, bool> trySetPropertyValue, IExcelDataAdvice dataAdvice = null, int propertyValueColumnIndex = 1) { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } T propertyValue; ExcelPropertyValueState state = dataQuery.TryGetPropertyValue <T>(propertyName, out propertyValue, dataAdvice, propertyValueColumnIndex); if (state == ExcelPropertyValueState.ProperProperty) { if (trySetPropertyValue(propertyValue) == false) { throw new ArgumentException("Invalid data for property '" + propertyName + "'" + GetFormatedDataQueryName(dataQuery) + "."); } } else if (state == ExcelPropertyValueState.NoValidValue) { throw new ArgumentException("Invalid data for property '" + propertyName + "'" + GetFormatedDataQueryName(dataQuery) + "."); } }
/// <summary>Gets the null-based column index of a specific header name. /// </summary> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="isHeaderName">A delegate that determines the name of the header to search; the argument is in the original representation, i.e. including whitespaces etc.</param> /// <param name="columnIndex">The null-based index of the column which contains in the first row the desired header name (output).</param> /// <returns>A value indicating whether <paramref name="columnIndex"/> contains valid data.</returns> public static bool TryGetHeaderNameColumnIndex(this IExcelDataQuery dataQuery, Func <string, bool> isHeaderName, out int columnIndex) { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } if (isHeaderName == null) { throw new ArgumentNullException("isHeaderName"); } for (int j = 0; j < dataQuery.ColumnCount; j++) { string value; switch (dataQuery.TryGetValue <string>(out value, rowIndex: 0, columnIndex: j)) { case ExcelCellValueState.EmptyOrMissingExcelCell: break; case ExcelCellValueState.NoValidValue: throw new ArgumentException("Invalid header, ' " + dataQuery.ToString(0, j) + "' does not represent a string" + GetFormatedDataQueryName(dataQuery) + "."); case ExcelCellValueState.ProperValue: if (value != null) { if (isHeaderName(value) == true) { columnIndex = j; return(true); } } break; default: throw new NotImplementedException(); } } columnIndex = -1; return(false); }
/// <summary>Sets an optional property value, i.e. if a specific property value is available the value will be used /// to change the state of a specific object, otherwise the standard value will be use. /// </summary> /// <typeparam name="TEnum">The type of the value which is assumed to be a enumeration.</typeparam> /// <param name="dataQuery">The <see cref="IExcelDataQuery"/> object.</param> /// <param name="propertyName">The name of the property.</param> /// <param name="trySetPropertyValue">A delegate which is used to set the value of the property with respect to a specific object, if the property value is available.</param> /// <param name="enumStringRepresentationUsage">The method how to compute the <see cref="System.String"/> representation of the enumeration <typeparamref name="TEnum"/>.</param> /// <exception cref="ArgumentNullException">Thrown, if <paramref name="dataQuery"/> is <c>null</c>.</exception> /// <remarks>Use this method for properties which have some standard value if the user does not /// enter a specific value, for example the standard exit condition of an optimizer algorithm /// is the maximal number of iterations.</remarks> public static void SetOptionalPropertyValue <TEnum>(this IExcelDataQuery dataQuery, string propertyName, Func <TEnum, bool> trySetPropertyValue, EnumStringRepresentationUsage enumStringRepresentationUsage) where TEnum : struct, IComparable, IConvertible, IFormattable { if (dataQuery == null) { throw new ArgumentNullException("dataQuery"); } TEnum propertyValue; ExcelPropertyValueState state = dataQuery.TryGetPropertyValue <TEnum>(propertyName, out propertyValue, enumStringRepresentationUsage); if (state == ExcelPropertyValueState.ProperProperty) { if (trySetPropertyValue(propertyValue) == false) { throw new ArgumentException("Invalid data for property '" + propertyName + "'" + GetFormatedDataQueryName(dataQuery) + "."); } } else if (state == ExcelPropertyValueState.NoValidValue) { throw new ArgumentException("Invalid data for property '" + propertyName + "'" + GetFormatedDataQueryName(dataQuery) + "."); } }