private static string GetMessage(Type targetType, IDataSheetCell cell)
 {
     return
         ($"Conversion from '{nameof(String)}' to '{targetType.Name}' is not supported for value " +
          $"'{cell.GetString()}' within cell {cell.ColumnLetter}{cell.RowNumber}. " +
          "Try configuring additional converters to support the requested operation!");
 }
Example #2
0
        public object ConvertTo(IDataSheetCell cell, Type convertTo)
        {
            if (cell == null)
            {
                throw new ArgumentNullException(nameof(cell));
            }
            if (convertTo == null)
            {
                throw new ArgumentNullException(nameof(convertTo));
            }

            var converters = _converterProvider.GetConverters();

            if (converters.Empty())
            {
                throw new NoConvertersConfiguredException();
            }

            if (converters.ContainsKey(convertTo) == false)
            {
                throw new CellValueConversionNotSupportedException(convertTo, cell);
            }

            var converter = converters[convertTo];

            try
            {
                return(converter(cell));
            }
            catch (Exception exception)
            {
                throw ValueException.ForInvalidValue(convertTo, cell, exception);
            }
        }
        private bool TryMatchCellValue(IDataSheetCell c)
        {
            var cellValue      = c.GetValue <string>();
            var searchCriteria = _convention.ApplyConvention(cellValue);
            var result         =
                string.Compare(searchCriteria, _valueToFind, _options) == 0;

            return(result);
        }
Example #4
0
        public TTargetValue ConvertTo <TTargetValue>(IDataSheetCell cell)
        {
            var value = ConvertTo(cell, typeof(TTargetValue));

            if (value.AnyValue())
            {
                return((TTargetValue)value);
            }

            return(default(TTargetValue));
        }
Example #5
0
        public void ShouldNotTrimValueWhenConvertingToString(IDataSheetCell cell, HardcodedConverterProvider sut)
        {
            // Arrange
            cell.GetStringOrDefault().Returns(" Value  ");
            var converters = sut.GetConverters();
            var converter  = converters[typeof(string)];
            // Act
            var result = converter(cell);

            // Assert
            Assert.Equal(" Value  ", result);
        }
Example #6
0
        public IDataSheetPicture MoveTo(IDataSheetCell cell)
        {
            if (cell == null)
            {
                throw new ArgumentNullException(nameof(cell));
            }

            var internalCell = ((DataSheetCell)cell).Internal;

            _picture.MoveTo(internalCell);
            return(this);
        }
Example #7
0
        public static ValueException ForInvalidValue(Type targetType, IDataSheetCell cell)
        {
            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }
            if (cell == null)
            {
                throw new ArgumentNullException(nameof(cell));
            }

            var message = GetMessage(targetType, cell);

            return(new ValueException(message));
        }
 public CellValueConversionNotSupportedException(Type targetType, IDataSheetCell cell)
     : base(GetMessage(targetType, cell))
 {
 }
Example #9
0
 private static string GetMessage(Type targetType, IDataSheetCell cell)
 {
     return
         ($"Conversion from '{typeof(string).Name}' to '{targetType.Name}' for value '{cell.GetString()}' has failed!" +
          $" Value in the cell '{cell.ColumnLetter + cell.RowNumber}' is not valid for the destination type! Please check converter logic or amend the value within the cell.");
 }
Example #10
0
 public static ValueException ForInvalidValue <TDestinationValueType>(IDataSheetCell cell, Exception exception)
 {
     return(ForInvalidValue(typeof(TDestinationValueType), cell, exception));
 }
Example #11
0
        public static ValueException ForMissingValue <TDestinationValueType>(string worksheetName, IDataSheetCell cell)
        {
            var message = $"Value of type '{typeof(TDestinationValueType).Name}' expected in worksheet {worksheetName} cell {cell.ColumnLetter}{cell.RowNumber}";

            return(new ValueException(message));
        }