Ejemplo n.º 1
0
        public void Ctor_Default()
        {
            var result = new ReadCellValueResult();

            Assert.Equal(0, result.ColumnIndex);
            Assert.Null(result.StringValue);
        }
Ejemplo n.º 2
0
        public void Ctor_ColumnIndex_StringValue(int columnIndex, string stringValue)
        {
            var result = new ReadCellValueResult(columnIndex, stringValue);

            Assert.Equal(columnIndex, result.ColumnIndex);
            Assert.Equal(stringValue, result.StringValue);
        }
        public string TransformStringValue(ExcelSheet sheet, int rowIndex, ReadCellValueResult readResult)
        {
            if (readResult.StringValue == null)
            {
                return(readResult.StringValue);
            }

            return(readResult.StringValue.Trim());
        }
Ejemplo n.º 4
0
        public PropertyMapperResultType MapCellValue(ReadCellValueResult readResult, ref object value)
        {
            if (!Guid.TryParse(readResult.StringValue, out Guid result))
            {
                return(PropertyMapperResultType.Invalid);
            }

            value = result;
            return(PropertyMapperResultType.Success);
        }
Ejemplo n.º 5
0
        public PropertyMapperResultType GetProperty(ReadCellValueResult readResult, ref object value)
        {
            if (!Uri.TryCreate(readResult.StringValue, UriKind.Absolute, out var result))
            {
                return(PropertyMapperResultType.Invalid);
            }

            value = result;
            return(PropertyMapperResultType.Success);
        }
Ejemplo n.º 6
0
        public PropertyMapperResultType GetProperty(ReadCellValueResult readResult, ref object value)
        {
            // If we didn't find anything, keep going. This is not necessarily a fatal error.
            if (!MappingDictionary.TryGetValue(readResult.StringValue, out T result))
            {
                return(PropertyMapperResultType.Continue);
            }

            value = result;
            return(PropertyMapperResultType.Success);
        }
Ejemplo n.º 7
0
 public PropertyMapperResultType GetProperty(ReadCellValueResult readResult, ref object value)
 {
     try
     {
         value = Enum.Parse(EnumType, readResult.StringValue);
         return(PropertyMapperResultType.Success);
     }
     catch
     {
         return(PropertyMapperResultType.Invalid);
     }
 }
Ejemplo n.º 8
0
 public PropertyMapperResultType MapCellValue(ReadCellValueResult readResult, ref object value)
 {
     try
     {
         value = Convert.ChangeType(readResult.StringValue, Type);
         return(PropertyMapperResultType.Success);
     }
     catch
     {
         return(PropertyMapperResultType.Invalid);
     }
 }
Ejemplo n.º 9
0
        public bool TryGetValue(ExcelSheet sheet, int rowIndex, IExcelDataReader reader, out ReadCellValueResult result)
        {
            if (ColumnIndex >= reader.FieldCount)
            {
                result = default;
                return(false);
            }

            var value = reader[ColumnIndex]?.ToString();

            result = new ReadCellValueResult(ColumnIndex, value);
            return(true);
        }
Ejemplo n.º 10
0
        public bool TryGetValue(ExcelSheet sheet, int rowIndex, IExcelDataReader reader, out ReadCellValueResult result)
        {
            if (sheet.Heading == null)
            {
                throw new ExcelMappingException($"The sheet \"{sheet.Name}\" does not have a heading. Use a column index mapping instead.");
            }

            if (!sheet.Heading.TryGetColumnIndex(ColumnName, out int index))
            {
                result = default;
                return(false);
            }

            string value = reader[index]?.ToString();

            result = new ReadCellValueResult(index, value);
            return(true);
        }
Ejemplo n.º 11
0
        public PropertyMapperResultType GetProperty(ReadCellValueResult readResult, ref object value)
        {
            // Excel transforms bool values such as "true" or "false" to "1" or "0".
            if (readResult.StringValue == "1")
            {
                value = true;
                return(PropertyMapperResultType.Success);
            }

            if (readResult.StringValue == "0")
            {
                value = false;
                return(PropertyMapperResultType.Success);
            }

            if (!bool.TryParse(readResult.StringValue, out var result))
            {
                return(PropertyMapperResultType.Invalid);
            }

            value = result;
            return(PropertyMapperResultType.Success);
        }
        public bool TryGetValues(ExcelSheet sheet, int rowIndex, IExcelDataReader reader, out IEnumerable <ReadCellValueResult> result)
        {
            if (sheet.Heading == null)
            {
                throw new ExcelMappingException($"The sheet \"{sheet.Name}\" does not have a heading. Use a column index mapping instead.");
            }

            var values = new ReadCellValueResult[ColumnNames.Length];

            for (int i = 0; i < ColumnNames.Length; i++)
            {
                if (!sheet.Heading.TryGetColumnIndex(ColumnNames[i], out int index))
                {
                    result = default;
                    return(false);
                }

                var value = reader[index]?.ToString();
                values[i] = new ReadCellValueResult(index, value);
            }

            result = values;
            return(true);
        }
Ejemplo n.º 13
0
        internal static object GetPropertyValue(IValuePipeline pipeline, ExcelSheet sheet, int rowIndex, IExcelDataReader reader, ReadCellValueResult readResult, MemberInfo member)
        {
            foreach (ICellValueTransformer transformer in pipeline.CellValueTransformers)
            {
                readResult = new ReadCellValueResult(readResult.ColumnIndex, transformer.TransformStringValue(sheet, rowIndex, readResult));
            }

            if (string.IsNullOrEmpty(readResult.StringValue) && pipeline.EmptyFallback != null)
            {
                return(pipeline.EmptyFallback.PerformFallback(sheet, rowIndex, readResult, member));
            }

            PropertyMapperResultType resultType = PropertyMapperResultType.Success;
            object value = null;

            foreach (ICellValueMapper mappingItem in pipeline.CellValueMappers)
            {
                PropertyMapperResultType newResultType = mappingItem.MapCellValue(readResult, ref value);
                if (newResultType == PropertyMapperResultType.Success)
                {
                    return(value);
                }

                if (newResultType != PropertyMapperResultType.Continue)
                {
                    resultType = newResultType;
                }
            }

            if (resultType != PropertyMapperResultType.Success && resultType != PropertyMapperResultType.SuccessIfNoOtherSuccess && pipeline.InvalidFallback != null)
            {
                return(pipeline.InvalidFallback.PerformFallback(sheet, rowIndex, readResult, member));
            }

            return(value);
        }
Ejemplo n.º 14
0
 public object PerformFallback(ExcelSheet sheet, int rowIndex, ReadCellValueResult readResult, MemberInfo member)
 {
     throw new ExcelMappingException($"Invalid assigning \"{readResult.StringValue}\" to member \"{member.Name}\" of type \"{member.MemberType()}\"", sheet, rowIndex, readResult.ColumnIndex);
 }
 public bool TryGetValue(ExcelSheet sheet, int rowIndex, IExcelDataReader reader, out ReadCellValueResult result)
 {
     result = new ReadCellValueResult();
     return(true);
 }
Ejemplo n.º 16
0
 public PropertyMapperResultType MapCellValue(ReadCellValueResult readResult, ref object value)
 {
     return(Converter(readResult, ref value));
 }
Ejemplo n.º 17
0
 public PropertyMapperResultType MapCellValue(ReadCellValueResult result, ref object value)
 {
     value = result.StringValue;
     return(PropertyMapperResultType.SuccessIfNoOtherSuccess);
 }