Beispiel #1
0
        public string GetCellComment(CellRef k)
        {
            string v;

            TryGetCellComment(k, out v);
            return(v);
        }
Beispiel #2
0
 internal PdtsText(
     int sheetIndex, CellRef cellRef, string text)
 {
     this.sheetIndex = sheetIndex;
     this.cellRef    = cellRef;
     this.text       = text;
 }
Beispiel #3
0
        private void _VerifyCellRefDuplication(List <Error> errors)
        {
            var fields = fieldsForBuilder;

            for (var n = 1; n < fieldCount; ++n)
            {
                var aF = fields[n - 1];
                var bF = fields[n];
                if (aF.rowOffset != bF.rowOffset ||
                    aF.colOffset != bF.colOffset)
                {
                    continue;
                }
                var oF = aF;
                for (var m = n - 2; m >= 0; --m)
                {
                    oF = fields[m];
                    if (oF.rowOffset != aF.rowOffset ||
                        oF.colOffset != aF.colOffset)
                    {
                        oF = fields[m + 1];
                        break;
                    }
                }
                var cr = new CellRef(
                    rangeStartRef.row + bF.rowOffset,
                    rangeStartRef.col + bF.colOffset
                    );
                var msg = string.Format(
                    "CellRef '{0}' is already used at {1} line.", cr, oF.line
                    );
                errors.Add(new Error(bF.line, msg));
            }
        }
Beispiel #4
0
        private bool _TryParseFieldPattern(
            string[] lines, int n, List <Error> errors)
        {
            var match = _fieldPattern.Match(lines[n]);

            if (!match.Success)
            {
                errors.Add(new Error(n + 1,
                                     "Field pattern is incorrect."
                                     ));
                return(false);
            }
            var cr    = new CellRef(match.Groups["cref"].ToString());
            var field = new Field(
                _fields.Count, n + 1, cr.row, cr.col,
                match.Groups["name"].ToString(),
                match.Groups["type"].ToString()
                );

            if (_fields.Count == 0)
            {
                rangeStartRef = cr;
            }
            else
            {
                rangeStartRef = new CellRef(
                    System.Math.Min(rangeStartRef.row, cr.row),
                    System.Math.Min(rangeStartRef.col, cr.col)
                    );
            }
            _fields.Add(field);
            return(true);
        }
Beispiel #5
0
        private void _ReadCommentsXml()
        {
            if (_cellComments != null)
            {
                return;
            }
            _cellComments = new CellComments();
            if (isClosed || sheetIndex >= sheetCount)
            {
                return;
            }
            var reader = _GetCommentsReader();

            if (reader == null)
            {
                return;
            }
            CellRef?      k = null;
            StringBuilder v = null;

            while (reader.Read())
            {
                if (reader.Name == "comment")
                {
                    var nt = reader.NodeType;
                    if (nt == XmlNodeType.Element)
                    {
                        k = new CellRef(reader.GetAttribute("ref"));
                    }
                    else if (nt == XmlNodeType.EndElement)
                    {
                        _cellComments.Add(k.Value, v.ToString());
                        k = null;
                        v = null;
                    }
                    continue;
                }
                if (k.HasValue && reader.IsStartElement("t"))
                {
                    reader.Read();
                    if (v == null)
                    {
                        v = new StringBuilder();
                    }
                    v.Append(reader.Value);
                    continue;
                }
            }
        }
Beispiel #6
0
        public bool MoveToNextSheet()
        {
            if (isClosed || ++sheetIndex >= sheetCount)
            {
                return(false);
            }
            var sheetPath = _sheetPaths[_sheetRelsIds[sheetIndex]];

            sheetName     = _sheetNames[sheetIndex];
            _sheetReader  = _sheetReaders[sheetPath];
            _cellComments = null;
            cellRef       = new CellRef(0, 0);
            _cellValue    = null;
            return(true);
        }
Beispiel #7
0
        public bool Read()
        {
            if (isClosed || sheetIndex >= sheetCount)
            {
                return(false);
            }
            var needNextSheet = !_TryReadSheetXml();

            if (needNextSheet)
            {
                return((MoveToNextSheet()) ? Read() : false);
            }
            var sr = _sheetReader;

            cellRef    = new CellRef(sr.GetAttribute("r"));
            _cellValue = null;
            return(true);
        }
Beispiel #8
0
        public XlsxRequest AddPdtsText(int sheetIndex, string schemaText)
        {
            if (sheetIndex < 0)
            {
                throw new System.ArgumentOutOfRangeException("sheetIndex");
            }
            if (schemaText == null)
            {
                throw new System.ArgumentNullException("schemaText");
            }
            schemaText = schemaText.Trim();
            var clone   = new XlsxRequest(this);
            var schemas = new List <PdtsText>(clone.pdtsTexts);
            var cellRef = new CellRef("PREDEF1");

            cellRef = new CellRef(cellRef.row + schemas.Count, cellRef.col);
            schemas.Add(new PdtsText(sheetIndex, cellRef, schemaText));
            clone.pdtsTexts = schemas.AsReadOnly();
            return(clone);
        }
Beispiel #9
0
 internal Error(CellRef cellRef, string message)
 {
     this.cellRef = cellRef;
     this.message = message;
 }
Beispiel #10
0
 internal Row(CellRef startRef, object[] cells)
 {
     this.startRef = startRef;
     this._cells   = cells;
 }
Beispiel #11
0
        private List <Row> _ParseCellValues(List <Error> errors)
        {
            var src = _rows;
            var dst = new List <Row>(src.Count);

            foreach (var row in src)
            {
                var cells = new object[row.cellCount];
                for (var n = 0; n < cells.Length; ++n)
                {
                    var error = _ParseCellValue(
                        schema[n].type, (string)row[n], out cells[n]
                        );
                    if (string.IsNullOrEmpty(error))
                    {
                        continue;
                    }
                    string customError = null;
                    object v           = null;
                    try {
                        v = fieldTypeConverter.FromString(
                            schema[n].type, (string)row[n]
                            );
                    } catch (System.Exception e) {
                        customError = _ToFieldTypeConvertorError(e);
                    }
                    if (v == null)
                    {
                        if (!string.IsNullOrEmpty(customError))
                        {
                            error = customError;
                        }
                        else if (row[n] != null)
                        {
                            error = "Cell value cannot convert to null.";
                        }
                    }
                    else
                    {
                        error = null;
                        var t = v.GetType();
                        if (!t.IsValueType && t != typeof(string))
                        {
                            error = ""
                                    + "Cell value type must be "
                                    + "'ValueType' or 'string'.";
                        }
                    }
                    cells[n] = v;
                    if (string.IsNullOrEmpty(error))
                    {
                        continue;
                    }
                    cells[n] = null;
                    var cellRef = new CellRef(
                        row.startRef.row + schema[n].rowOffset,
                        row.startRef.col + schema[n].colOffset
                        );
                    errors.Add(new Error(cellRef, error));
                }
                dst.Add(new Row(row.startRef, cells));
            }
            return(dst);
        }
Beispiel #12
0
 public bool TryGetCellComment(CellRef k, out string v)
 {
     _ReadCommentsXml();
     return(_cellComments.TryGetValue(k, out v));
 }
Beispiel #13
0
        internal static DataTableSchema Parse(
            int workbookIndex, string workbookName,
            int sheetIndex, string sheetName,
            CellRef cellCommentRef, string cellComment)
        {
            if (!cellComment.StartsWith("[["))
            {
                return(null);
            }
            var lines  = _SplitCellComment(cellComment);
            var errors = new List <Error>();
            var inst   = new DataTableSchema();

            inst.workbookIndex  = workbookIndex;
            inst.workbookName   = workbookName;
            inst.sheetIndex     = sheetIndex;
            inst.sheetName      = sheetName;
            inst.cellCommentRef = cellCommentRef;
            inst._fields        = new List <Field>();
            if (!inst._TryParseStartPattern(lines, 0, errors))
            {
                inst.name = string.Format("ERROR_{0}_{1}_{2}",
                                          workbookIndex, sheetIndex, cellCommentRef
                                          );
            }
            var endLineNum = 0;

            for (var n = 1; n < lines.Length; ++n)
            {
                if (lines[n].Trim().Length == 0)
                {
                    continue;
                }
                if (_commentPattern.IsMatch(lines[n]))
                {
                    continue;
                }
                if (inst._TryParseFieldPattern(lines, n, errors))
                {
                    continue;
                }
                if (!lines[n].Trim().StartsWith("]]"))
                {
                    continue;
                }
                if (inst._TryParseEndPattern(lines, n, errors))
                {
                    errors.RemoveAt(errors.Count - 1);
                    endLineNum = n + 1;
                    break;
                }
            }
            if (endLineNum <= 1)
            {
                errors.Add(new Error(lines.Length,
                                     "End pattern not found."
                                     ));
            }
            else if (inst._fields.Count == 0)
            {
                errors.Add(new Error(endLineNum,
                                     "Table must have one field at least."
                                     ));
            }
            inst._InitBlockInfo();
            inst._VerifyNextBlockOffset(endLineNum, errors);
            inst._InitFieldsForBuilder();
            inst._InitFieldsForWriter();
            inst._VerifyCellRefDuplication(errors);
            inst._VerifyFieldType(errors);
            inst._VerifyFieldDuplication(errors);
            inst.errors = errors.AsReadOnly();
            return(inst);
        }