Пример #1
0
        /// <summary>Gets a subset of a specified <see cref="DataTable"/> object in a two-dimensional <see cref="System.Object"/> array representation.
        /// </summary>
        /// <param name="rowIndices">The set of null-based (excluding header) row indices.</param>
        /// <param name="rowCount">The number of elements in <paramref name="rowIndices"/>.</param>
        /// <param name="columnIndices">The set of null-based column indices.</param>
        /// <param name="columnCount">The number of elements in <paramref name="columnIndices"/>.</param>
        /// <param name="dataTable">The data table.</param>
        /// <returns>A two-dimensional <see cref="System.Object"/> array representation of the desired subset of <paramref name="dataTable"/>; it contains
        /// the header if it contains more than one column.</returns>
        private static object[,] GetSubTable(IEnumerable <int> rowIndices, int rowCount, IEnumerable <int> columnIndices, int columnCount, DataTable dataTable)
        {
            if (columnCount == 1)  // without header
            {
                object[,] outputRange = new object[rowCount, 1];
                int columnIndex = columnIndices.First();

                int k = 0;
                foreach (int rowIndex in rowIndices)
                {
                    outputRange[k, 0] = ExcelDataConverter.GetExcelCellRepresentation(dataTable.Rows[rowIndex][columnIndex]);
                    k++;
                }
                return(outputRange);
            }
            else if (columnCount > 1)  // with header
            {
                object[,] outputRange = new object[rowCount + 1, columnCount];

                int k = 0;
                foreach (int columnIndex in columnIndices)
                {
                    outputRange[0, k] = dataTable.Columns[columnIndex].ColumnName;
                    int j = 1;
                    foreach (int rowIndex in rowIndices)
                    {
                        outputRange[j, k] = ExcelDataConverter.GetExcelCellRepresentation(dataTable.Rows[rowIndex][columnIndex]);
                        j++;
                    }
                    k++;
                }
                return(outputRange);
            }
            throw new ArgumentException("Empty data table.", "dataTable");
        }
Пример #2
0
        public static object GetOutputPropertyValue(
            [ExcelArgument(Name = "objectName", Description = "The name of the object", AllowReference = true)]
            object xlObjectName,
            [ExcelArgument(Name = "propertyName", Description = "The name of the property", AllowReference = true)]
            object xlPropertyName,
            [ExcelArgument(Name = "propertyGroupName", Description = "[optional] The name of the property group ('general properties' is standard)", AllowReference = true)]
            object xlPropertyGroupName,
            [ExcelArgument(Name = "category", Description = "The category of the output, i.e. 'general' etc.", AllowReference = true)]
            object xlCategoryName)
        {
            try
            {
                if (ExcelDnaUtil.IsInFunctionWizard() == true)
                {
                    return(String.Empty);
                }

                IExcelDataQuery objectNameQuery = ExcelDataQuery.Create(xlObjectName);
                string          objectName;
                ExcelPoolItem   poolItem;
                if ((objectNameQuery.TryGetValue <string>(out objectName, dataAdvice: ExcelDataAdvice.Create(ExcelPool.GetObjectNames())) != ExcelCellValueState.ProperValue) || (ExcelPool.TryGetItem(objectName, out poolItem) == false))
                {
                    throw new ArgumentException("No object with name '" + objectNameQuery.ToString(0, 0).GetRelevantSubstring() + "' available.");
                }
                /* get the output of the pool object: */
                InfoOutput itemOutput = new InfoOutput();
                poolItem.Value.FillInfoOutput(itemOutput);

                InfoOutputPackage itemOutputCollection = null;
                IExcelDataQuery   categoryNameQuery    = ExcelDataQuery.Create(xlCategoryName);
                if (categoryNameQuery.IsEmpty == true)
                {
                    itemOutputCollection = itemOutput.GetGeneralPackage();
                }
                else
                {
                    string categoryName = categoryNameQuery.GetValue <string>(dataAdvice: ExcelDataAdvice.Create(itemOutput.CategoryNames));
                    itemOutputCollection = itemOutput.GetPackage(categoryName);
                }

                string          propertyGroupName      = InfoOutputPackage.GeneralPropertyGroupName.String;
                IExcelDataQuery propertyGroupNameQuery = ExcelDataQuery.Create(xlPropertyGroupName);
                if (propertyGroupNameQuery.IsEmpty == false)
                {
                    propertyGroupName = propertyGroupNameQuery.GetValue <string>(dataAdvice: ExcelDataAdvice.Create(itemOutputCollection.PropertyGroupNames));
                }

                IIdentifierStringDictionary <InfoOutputProperty> properties;
                if (itemOutputCollection.TryGetProperties(propertyGroupName, out properties) == false)
                {
                    throw new ArgumentException("The property group name' " + propertyGroupName + "' is invalid.");
                }

                IExcelDataQuery propertyNameQuery = ExcelDataQuery.Create(xlPropertyName);
                string          propertyName;
                if (propertyNameQuery.TryGetValue <string>(out propertyName, dataAdvice: ExcelDataAdvice.Create(properties)) != ExcelCellValueState.ProperValue)
                {
                    throw new ArgumentException("The property name '" + propertyNameQuery.ToString(0, 0) + "' is invalid.");
                }

                InfoOutputProperty property;
                if (properties.TryGetValue(propertyName, out property) == false)
                {
                    throw new ArgumentException("No property with name '" + propertyName + "' found.");
                }
                return(ExcelDataConverter.GetExcelCellRepresentation(property.Value));
            }
            catch (Exception e)
            {
                return(ExcelDataConverter.GetExcelRangeErrorMessage(e.Message));
            }
        }