Beispiel #1
0
        public async Task <SheetValidationResult> Validate(string url, int sheetIndex            = 0,
                                                           JsonNamingStrategy jsonNamingStrategy = JsonNamingStrategy.None,
                                                           string[] headerMap = null, bool ignoreFirstRow = true)
        {
            loaded         = true;
            ignoreFirst    = ignoreFirstRow;
            namingStrategy = jsonNamingStrategy;
            map            = headerMap;
            var sheet = await reader.LoadSheet(url, sheetIndex, map);

            if (sheet == null)
            {
                return(SheetValidationResult.SheetNotPresent());
            }

            var request = new SheetOnTypeParseRequest
            {
                MappingOptions = new SheetMappingOptions
                {
                    Ignore    = ignoreFirst,
                    Map       = headerMap,
                    IndexAsId = true
                },
                NamingStrategy = namingStrategy,
                RootType       = typeof(T),
                Sheet          = sheet
            };

            sheetValidationResult = await sheetValidator.Validate(request);

            return(sheetValidationResult);
        }
Beispiel #2
0
        public SheetRecord(ExcelFileRecord parent, ISheet sheet, SheetValidationResult validationResult)
        {
            ExcelFileRecord = parent ?? throw new ArgumentNullException(nameof(parent));
            Index           = sheet.Index;

            Name              = sheet.Name;
            Empty             = sheet.Empty;
            EmptyData         = sheet.EmptyData;
            RowCount          = sheet.RowCount;
            InvalidName       = validationResult.InvalidName;
            InvalidHeaders    = validationResult.InvalidHeaders;
            Map               = validationResult.Map;
            IgnoreFirstRow    = validationResult.IgnoreFirstRow;
            HeaderCellRecords = sheet.Header?.Select(x => new CellRecord(x)).ToArray();
        }
        public Task <SheetValidationResult> Validate(SheetOnTypeParseRequest request)
        {
            var sheet = request.Sheet;

            if (sheet.Empty || sheet.EmptyData)
            {
                return(Task.FromResult(SheetValidationResult.EmptySheet()));
            }

            var  invalidName    = false;
            var  invalidHeaders = new List <int>();
            Type type           = null;

            var sheetOptions = request.MappingOptions;

            var(ignoreFirstRow, headerMap) = GetMap(sheet, sheetOptions);

            if (sheet.Index == 0)
            {
                type        = request.RootType;
                invalidName = false;
            }
            else
            {
                type = GetEnumerableType(request.RootType,
                                         request.LongName ?? sheet.Name, request.NamingStrategy);
                invalidName = type == null;
            }

            if (!invalidName)
            {
                var idHeaders = new List <int>();
                if (request.MappingOptions.IdIndex.HasValue)
                {
                    idHeaders.Add(request.MappingOptions.IdIndex.Value);
                }

                if (request.MappingOptions.ParentIdIndex.HasValue)
                {
                    idHeaders.Add(request.MappingOptions.ParentIdIndex.Value);
                }

                for (var i = 0; i < headerMap.Length; i++)
                {
                    if (idHeaders.Contains(i))
                    {
                        continue;
                    }
                    if (string.IsNullOrEmpty(headerMap[i]))
                    {
                        invalidHeaders.Add(i);
                        continue;
                    }
                    var propertyName = headerMap[i].Transform(request.NamingStrategy);
                    var propertyPath = PropertyPath.TryParse(type, propertyName);
                    if (propertyPath == null)
                    {
                        invalidHeaders.Add(i);
                    }
                    else
                    {
                        var enumerableType = GetEnumerableType(type, propertyName, request.NamingStrategy);
                        if (enumerableType != null && !AllowedEnumerableInSheet(enumerableType))
                        {
                            invalidHeaders.Add(i);
                        }
                    }
                }
            }

            var result = new SheetValidationResult(headerMap, ignoreFirstRow, invalidName, invalidHeaders.ToArray());

            return(Task.FromResult(result));
        }