Exemplo n.º 1
0
        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));
            }
        }
Exemplo n.º 2
0
        public static object GetInputPropertyValue(
            [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 = "propertyValueColumnIndex", Description = "[optional] The null-based column index of the column which contains the value of the property, '1' is standard", AllowReference = true)]
            object xlPropertyValueColumnIndex,
            [ExcelArgument(Name = "propertyTableName", Description = "[optional] The name of the table which represents the properties ('general properties' is standard)", AllowReference = true)]
            object xlPropertyTableName = null)
        {
            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.");
                }

                string          propertyTableName  = "General properties";
                IExcelDataQuery propertyTableQuery = ExcelDataQuery.Create(xlPropertyTableName);
                if (propertyTableQuery.IsEmpty == false)
                {
                    propertyTableName = propertyTableQuery.GetValue <string>(dataAdvice: ExcelDataAdvice.Create(poolItem.GetDataQueryNames()));
                }
                GuidedExcelDataQuery propertyDataQuery;
                if (poolItem.TryGetDataQuery(propertyTableName, out propertyDataQuery) == false)
                {
                    throw new ArgumentException("The property table name' " + propertyTableName + "' is invalid.");
                }

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

                /* now get the property value, but use a Excel conform output: */
                int propertyRowIndex;
                if (propertyDataQuery.TryGetRowIndexOfPropertyName(propertyName, out propertyRowIndex) == false)
                {
                    throw new ArgumentException("No property with name '" + propertyName + "' found.");
                }
                int propertyValueColumnIndex;
                ExcelCellValueState propertyValueColumnIndexState = ExcelDataQuery.Create(xlPropertyValueColumnIndex).TryGetValue <int>(out propertyValueColumnIndex);

                if (propertyValueColumnIndexState == ExcelCellValueState.EmptyOrMissingExcelCell)
                {
                    propertyValueColumnIndex = 1;
                }
                else if (propertyValueColumnIndexState == ExcelCellValueState.NoValidValue)
                {
                    throw new ArgumentException("Invalid 'Property value column index', a positive integer expected.");
                }

                return(propertyDataQuery.GetExcelData(propertyRowIndex, propertyValueColumnIndex));
            }
            catch (Exception e)
            {
                return(ExcelDataConverter.GetExcelRangeErrorMessage(e.Message));
            }
        }