示例#1
0
        private static byte[] GetStringBytes(WDBSheet sheet, out Dictionary <string, int> strOffsetDic)
        {
            strOffsetDic = new Dictionary <string, int>();
            strOffsetDic.Add("", 0);
            MemoryStream stream = new MemoryStream();

            ByteWriter.WriteString(stream, "");
            for (int i = 0; i < sheet.LineCount; ++i)
            {
                WDBLine line = sheet.GetLineAtIndex(i);
                for (int j = 0; j < sheet.FieldCount; ++j)
                {
                    WDBField field = sheet.GetFieldAtIndex(j);
                    if (field.FieldType == WDBFieldType.String || field.FieldType == WDBFieldType.Address ||
                        field.FieldType == WDBFieldType.Lua)
                    {
                        WDBCell cell      = line.GetCellByCol(field.Col);
                        string  cellValue = cell.GetValue(field);

                        string content = cellValue ?? "";
                        if (field.FieldType == WDBFieldType.Lua)
                        {
                            content = GetLuaFuncContent(content);
                        }
                        if (strOffsetDic.ContainsKey(content))
                        {
                            continue;
                        }
                        strOffsetDic.Add(content, (int)stream.Length);
                        ByteWriter.WriteString(stream, content);
                    }
                }
            }
            return(stream.ToArray());
        }
        private static void ReadLineFromSheet(WDBSheet sheetData, ISheet sheet)
        {
            logHandler?.Invoke(LogType.Info, string.Format(LogMessage.INFO_START_READ_LINE));

            int firstRowNum = readerExcelStyle.RowStartIndex + readerExcelStyle.FieldRowCount;
            int lastRowNum  = sheet.LastRowNum;

            int firstColNum = sheet.GetRow(readerExcelStyle.RowStartIndex).FirstCellNum;
            int lastColNum  = sheet.GetRow(readerExcelStyle.RowStartIndex).LastCellNum;

            bool isStart = false;

            for (int r = firstRowNum; r < lastRowNum; ++r)
            {
                IRow row = sheet.GetRow(r);
                if (row == null)
                {
                    logHandler?.Invoke(LogType.Info, string.Format(LogMessage.INFO_LINE_EMPTY, r));
                    continue;
                }
                string cellValue = GetCellStringValue(row.GetCell(firstColNum));
                if (string.IsNullOrEmpty(cellValue))
                {
                    if (!isStart)
                    {
                        continue;
                    }
                }
                else
                {
                    if (!isStart && cellValue == readerExcelStyle.LineStartFlag)
                    {
                        isStart = true;
                    }
                    else if (isStart && cellValue == readerExcelStyle.LineEndFlag)
                    {
                        isStart = false;
                        break;
                    }
                }

                WDBLine line = sheetData.AddLine(r);
                for (int c = firstColNum + 1; c < lastColNum; c++)
                {
                    WDBField field = sheetData.GetFieldAtCol(c);
                    if (field == null)
                    {
                        continue;
                    }
                    ICell valueCell = row.GetCell(c);
                    line.AddCell(c, GetCellStringValue(valueCell));
                }
                logHandler?.Invoke(LogType.Info, string.Format(LogMessage.INFO_CREATE_LINE, line));
            }

            logHandler?.Invoke(LogType.Info, LogMessage.INFO_END_READ_LINE);
        }
        protected override bool DoVerify()
        {
            if (sheet.Name != CurrentSheetName)
            {
                CurrentSheetName = null;
            }
            if (CurrentSheetName == null)
            {
                CurrentSheetName = sheet.Name;
                contentRepeatedDic.Clear();
            }
            Dictionary <string, int> repeatedDic = null;

            if (!contentRepeatedDic.TryGetValue(field.Name, out repeatedDic))
            {
                repeatedDic = new Dictionary <string, int>();
                contentRepeatedDic[field.Name] = repeatedDic;

                for (int i = 0; i < sheet.LineCount; ++i)
                {
                    WDBLine tLine  = sheet.GetLineAtIndex(i);
                    WDBCell tCell  = tLine.GetCellByCol(field.Col);
                    string  tValue = tCell.GetValue(field) ?? string.Empty;
                    if (repeatedDic.ContainsKey(tValue))
                    {
                        repeatedDic[tValue]++;
                    }
                    else
                    {
                        repeatedDic[tValue] = 1;
                    }
                }
            }

            string cellValue = cell.GetValue(field) ?? string.Empty;

            if (repeatedDic != null && repeatedDic.TryGetValue(cellValue, out var count) && count > 1)
            {
                errors.Add(GetErrorMsg(WDBVerifyConst.VALIDATION_CELL_UNIQUE_REPEAT_ERR, cellValue));
                return(false);
            }
            return(true);
        }
示例#4
0
        private static byte[] GetLineBytes(WDBSheet sheet, Dictionary <string, int> strOffsetDic)
        {
            MemoryStream stream = new MemoryStream();

            for (int i = 0; i < sheet.LineCount; ++i)
            {
                WDBLine line = sheet.GetLineAtIndex(i);
                for (int j = 0; j < sheet.FieldCount; ++j)
                {
                    WDBField field     = sheet.GetFieldAtIndex(j);
                    WDBCell  cell      = line.GetCellByCol(field.Col);
                    string   cellValue = cell.GetValue(field);

                    if (field.FieldType == WDBFieldType.String || field.FieldType == WDBFieldType.Address ||
                        field.FieldType == WDBFieldType.Lua)
                    {
                        string content = cellValue ?? "";
                        if (field.FieldType == WDBFieldType.Lua)
                        {
                            content = GetLuaFuncContent(content);
                        }
                        if (!strOffsetDic.TryGetValue(content, out var offset))
                        {
                            throw new Exception();
                        }
                        ByteWriter.WriteInt(stream, offset);
                    }
                    else if (field.FieldType == WDBFieldType.Bool)
                    {
                        if (string.IsNullOrEmpty(cellValue) || !bool.TryParse(cellValue, out var result))
                        {
                            ByteWriter.WriteBool(stream, false);
                        }
                        else
                        {
                            ByteWriter.WriteBool(stream, result);
                        }
                    }
                    else if (field.FieldType == WDBFieldType.Int || field.FieldType == WDBFieldType.Text)
                    {
                        if (string.IsNullOrEmpty(cellValue) || !int.TryParse(cellValue, out var result))
                        {
                            ByteWriter.WriteInt(stream, 0);
                        }
                        else
                        {
                            ByteWriter.WriteInt(stream, result);
                        }
                    }
                    else if (field.FieldType == WDBFieldType.Id || field.FieldType == WDBFieldType.Ref)
                    {
                        if (string.IsNullOrEmpty(cellValue) || !int.TryParse(cellValue, out var result))
                        {
                            ByteWriter.WriteInt(stream, -1);
                        }
                        else
                        {
                            ByteWriter.WriteInt(stream, result);
                        }
                    }
                    else if (field.FieldType == WDBFieldType.Long)
                    {
                        if (string.IsNullOrEmpty(cellValue) || !long.TryParse(cellValue, out var result))
                        {
                            ByteWriter.WriteLong(stream, 0);
                        }
                        else
                        {
                            ByteWriter.WriteLong(stream, result);
                        }
                    }
                    else if (field.FieldType == WDBFieldType.Float)
                    {
                        if (string.IsNullOrEmpty(cellValue) || !float.TryParse(cellValue, out var result))
                        {
                            ByteWriter.WriteFloat(stream, 0);
                        }
                        else
                        {
                            ByteWriter.WriteFloat(stream, result);
                        }
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
            }
            return(stream.ToArray());
        }
示例#5
0
        private static bool VerifySheet(WDBSheet sheet)
        {
            List <string> errors = (List <string>)context.Get(WDBContextIENames.CONTEXT_ERRORS_NAME);

            if (string.IsNullOrEmpty(sheet.Name))
            {
                errors.Add(WDBVerifyConst.VERIFY_SHEET_NAME_EMPTY_ERR);
                return(false);
            }
            if (!Regex.IsMatch(sheet.Name, WDBVerifyConst.VERIFY_SHEET_NAME_REGEX))
            {
                errors.Add(string.Format(WDBVerifyConst.VERIFY_SHEET_NAME_REGEX_ERR, sheet.Name));
                return(false);
            }

            if (sheet.FieldCount == 0)
            {
                errors.Add(string.Format(WDBVerifyConst.VERIFY_SHEET_NO_FIELD_ERR, sheet.Name));
                return(false);
            }
            if (sheet.LineCount == 0)
            {
                errors.Add(string.Format(WDBVerifyConst.VERIFY_SHEET_NO_ROW_ERR, sheet.Name));
                return(false);
            }
            bool result = true;

            for (int i = 0; i < sheet.FieldCount; ++i)
            {
                WDBField       field       = sheet.GetFieldAtIndex(i);
                WDBFieldVerify fieldVerify = new WDBFieldVerify()
                {
                    FieldType        = field.FieldType,
                    FieldPlatform    = field.FieldPlatform,
                    ValueValidations = field.ValueValidations,
                };
                fieldVerifyDic.Add(field, fieldVerify);
                if (!VerifyField(field))
                {
                    result = false;
                }
            }
            if (!result)
            {
                return(false);
            }

            context.Add(WDBContextIENames.CONTEXT_SHEET_NAME, sheet);
            for (int i = 0; i < sheet.LineCount; ++i)
            {
                WDBLine line = sheet.GetLineAtIndex(i);
                if (line.CellCount != sheet.FieldCount)
                {
                    result = false;
                    errors.Add(string.Format(WDBVerifyConst.VERIFY_SHEET_FIELD_ROW_ERR, sheet.FieldCount, line.Row));
                }
            }
            if (result)
            {
                for (int i = 0; i < sheet.FieldCount; ++i)
                {
                    WDBField       field       = sheet.GetFieldAtIndex(i);
                    WDBFieldVerify fieldVerify = fieldVerifyDic[field];
                    context.Add(WDBContextIENames.CONTEXT_FIELD_NAME, field);
                    context.Add(WDBContextIENames.CONTEXT_FIELD_VERIFY_NAME, fieldVerify);

                    for (int j = 0; j < sheet.LineCount; ++j)
                    {
                        WDBLine line = sheet.GetLineAtIndex(j);
                        WDBCell cell = line.GetCellByIndex(i);
                        context.Add(WDBContextIENames.CONTEXT_LINE_NAME, line);
                        if (cell.Col != field.Col)
                        {
                            result = false;
                            errors.Add(string.Format(WDBVerifyConst.VERIFY_CELL_COL_NOTSAME_ERR, cell.Row, cell.Col));
                        }
                        else if (fieldVerify.ValueValidations != null && fieldVerify.ValueValidations.Length > 0)
                        {
                            context.Add(WDBContextIENames.CONTEXT_CELL_NAME, cell);

                            foreach (var cellValidation in fieldVerify.ValueValidations)
                            {
                                context.InjectTo(cellValidation);
                                if (!cellValidation.Verify())
                                {
                                    result = false;
                                }
                            }
                            context.Remove(WDBContextIENames.CONTEXT_CELL_NAME);
                        }
                        context.Remove(WDBContextIENames.CONTEXT_LINE_NAME);
                    }
                    context.Remove(WDBContextIENames.CONTEXT_FIELD_NAME);
                    context.Remove(WDBContextIENames.CONTEXT_FIELD_VERIFY_NAME);
                }
            }
            context.Remove(WDBContextIENames.CONTEXT_SHEET_NAME);

            return(result);
        }