您的位置:首页 > Web前端 > JavaScript

基于JavaScript的公式解释器 - 15 【单元格范围对象】

2010-10-19 21:36 381 查看
CellRange单元格范围对象

文件:CellRange.js

function CellRange() {
    this.set_ClassName("CellRange");
    this._startCell = new Cell();
    this._endCell = new Cell();
    
    switch(arguments.length)
    {
        case 0: break;
        case 1:
            {
                switch ($T(arguments[0])) {
                    case "CellRange":
                        {
                            this._startCell.set_Row(Math.min(arguments[0].get_StartRow(), arguments[0].get_EndRow()));
                            this._startCell.set_Col(Math.min(arguments[0].get_StartCol(), arguments[0].get_EndCol()));
                            this._endCell.set_Row(Math.max(arguments[0].get_StartRow(), arguments[0].get_EndRow()));
                            this._endCell.set_Col(Math.max(arguments[0].get_StartCol(), arguments[0].get_EndCol()));
                        }
                        break;
                    case "string": this.FromAddress(arguments[0]); break;
                    default: throw new Exception(this, "CellRange", "Unsupported argument");
                } // switch
            }
            break;
        case 2:
            {
                var cells = [];
                for (var i = 0; i < arguments.length; i++) {
                    switch ($T(arguments[i])) {
                        case "string": cells[i] = new Cell(arguments[i]); break;
                        case "Cell": cells[i] = arguments[i]; break;
                        default: throw new Exception(this, "CellRange", "Unsupported argument:" + $T(arguments[i]));
                    }
                }
                this._startCell.set_Row(Math.min(cells[0].get_Row(), cells[1].get_Row()));
                this._startCell.set_Col(Math.min(cells[0].get_Col(), cells[1].get_Col()));
                this._endCell.set_Row(Math.max(cells[0].get_Row(), cells[1].get_Row()));
                this._endCell.set_Col(Math.max(cells[0].get_Col(), cells[1].get_Col()));
                
            }
            break;
        default: throw new Exception(this, "CellRange", "Wrong number of arguments");
    }
};
CellRange.prototype = {
    get_StartRow: function() {
        return this._startCell.get_Row();
    },
    get_StartCol: function() {
        return this._startCell.get_Col();
    },
    get_EndRow: function() {
        return this._endCell.get_Row();
    },
    get_EndCol: function() {
        return this._endCell.get_Col();
    },
    ContainsCell: function(cell) {
        $ASSERT($T(cell) == "Cell");
        return cell.get_Row() >= this.get_StartRow() && cell.get_Row() <= this.get_EndRow() &&
               cell.get_Col() >= this.get_StartCol() && cell.get_Col() <= this.get_EndCol();
    },
    IsValid: function() {
        return this._startCell.IsValid() && this._endCell().IsValid();
    },
    ToString: function() { return this.ToAddress(); },
    ToAddress: function() {
        return this._startCell.ToAddress() + ":" + this._endCell.ToAddress();
    },
    FromAddress: function(addrStr) {
        var match = /(.*):(.*)/i.exec(addrStr);
        if (match == null || match[1].length == 0 || match[2].length == 0)
            throw new Exception(this, "FromAddress", "Wrong address format");
        var cells = [new Cell(match[1]), new Cell(match[2])];
        this._startCell.set_Row(Math.min(cells[0].get_Row(), cells[1].get_Row()));
        this._startCell.set_Col(Math.min(cells[0].get_Col(), cells[1].get_Col()));
        this._endCell.set_Row(Math.max(cells[0].get_Row(), cells[1].get_Row()));
        this._endCell.set_Col(Math.max(cells[0].get_Col(), cells[1].get_Col()));
    },
    // ------------------- TEST ------------------------
    Test: function() {
        $Debug.WriteLine("============= " + this.get_ClassName() + " ===================");
        $Debug.WriteLine("----------------- FromAddress --------------------");
        // Test FromAddress
        var values = [
        //            "A1:A1",
        //            "A1:A10",
        //            "A10:A1",
            "A1:B1",
            "B1:A1",
            "A1:B10",
            "B10:A1",
            "AB1:AB10",
            "AB10:AB1",
            "AB1:CD10",
            "CD10:AB1"
        ];
        for (var i = 0; i < values.length; i++) {
            try {
                var rng = new CellRange();
                $Debug.Write(values[i] + "=");
                rng.FromAddress(values[i]);
                $Debug.WriteLine(rng.ToAddress());
            }
            catch (e) {
                $Debug.WriteLine("Error:" + e.description);
            }
        } // for
        $Debug.WriteLine("----------------- CTOR: one argument --------------------");
        // Test CTOR: one argument
        values = [
            "A1:A1",
            "A1:A10",
            "A10:A1",
            "A1:B1",
            "B1:A1",
            "A1:B10",
            "B10:A1",
            "AB1:AB10",
            "AB10:AB1",
            "AB1:CD10",
            "CD10:AB1",
            new CellRange("A1:A1"),
            new CellRange("A1:A10"),
            new CellRange("A10:A1"),
            new CellRange("A1:B1"),
            new CellRange("B1:A1"),
            new CellRange("A1:B10"),
            new CellRange("B10:A1"),
            new CellRange("AB1:AB10"),
            new CellRange("AB10:AB1"),
            new CellRange("AB1:CD10"),
            new CellRange("CD10:AB1")
        ];

        for (var i = 0; i < values.length; i++) {
            try {
                $Debug.Write(values[i].ToString() + "=");
                var rng = new CellRange(values[i]);
                $Debug.WriteLine(rng.ToAddress());
            }
            catch (e) {
                $Debug.WriteLine("Error:" + e.description);
            }
        } // for
        $Debug.WriteLine("----------------- CTOR: Two arguments --------------------");
        // Test CTOR: two argument
        values = [
            "A1", "A1",
            "A1", "A10",
            "A10", "A1",
            "A1", "B1",
            "B1", "A1",
            "A1", "B10",
            "B10", "A1",
            "AB1", "AB10",
            "AB10", "AB1",
            "AB1", "CD10",
            "CD10", "AB1",
            new Cell("A1"), new Cell("A1"),
            new Cell("A1"), new Cell("A10"),
            new Cell("A10"), new Cell("A1"),
            new Cell("A1"), new Cell("B1"),
            new Cell("B1"), new Cell("A1"),
            new Cell("A1"), new Cell("B10"),
            new Cell("B10"), new Cell("A1"),
            new Cell("AB1"), new Cell("AB10"),
            new Cell("AB10"), new Cell("AB1"),
            new Cell("AB1"), new Cell("CD10"),
            new Cell("CD10"), new Cell("AB1")
        ];

        for (var i = 0; i < values.length; i += 2) {
            try {
                $Debug.Write(values[i].ToString() + ":" + values[i + 1].ToString() + "=");
                var rng = new CellRange(values[i], values[i + 1]);
                $Debug.WriteLine(rng.ToAddress());
            }
            catch (e) {
                $Debug.WriteLine("Error:" + e.description);
            }
        } // for

        // Test ContainsCell()
        values = [
            "A1", "A1",
            "A1", "A10",
            "A10", "A1",
            "A1", "B1",
            "B1", "A1",
            "A1", "B10",
            "B10", "A1",
            "AB1", "AB10",
            "AB10", "AB1",
            "AB1", "CD10",
            "CD10", "AB1",
            new Cell("A1"), new Cell("A1"),
            new Cell("A1"), new Cell("A10"),
            new Cell("A10"), new Cell("A1"),
            new Cell("A1"), new Cell("B1"),
            new Cell("B1"), new Cell("A1"),
            new Cell("A1"), new Cell("B10"),
            new Cell("B10"), new Cell("A1"),
            new Cell("AB1"), new Cell("AB10"),
            new Cell("AB10"), new Cell("AB1"),
            new Cell("AB1"), new Cell("CD10"),
            new Cell("CD10"), new Cell("AB1")
        ];

        for (var i = 0; i < values.length; i += 2) {
            try {
                $Debug.Write(values[i].ToString() + ":" + values[i + 1].ToString() + "=");
                var rng = new CellRange(values[i], values[i + 1]);
                var cell = new Cell();
                cell.set_Row((rng.get_StartRow() + rng.get_EndRow()) / 1);
                cell.set_Col((rng.get_StartCol() + rng.get_EndCol()) / 1);
                if (rng.ContainsCell(cell))
                    $Debug.WriteLine(cell.ToAddress() + " in range");
                else
                    $Debug.WriteLine(cell.ToAddress() + " NOT in range");
            }
            catch (e) {
                $Debug.WriteLine("Error:" + e.description);
            }
        } // for
    } // Test
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: