基于JavaScript的公式解释器 - 15 【单元格范围对象】
2010-10-19 21:36
381 查看
CellRange单元格范围对象
文件:CellRange.js
文件: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 };
相关文章推荐
- 基于JavaScript的公式解释器 - 14 【单元格对象】
- 基于JavaScript的公式解释器 - 7 【算术操作符的实现】
- 基于JavaScript的公式解释器 - 8 【比较操作符的实现】
- 基于JavaScript的公式解释器 - 9 【逻辑操作符的实现】
- 基于JavaScript的公式解释器 - 10 【位操作符的实现】
- 基于JavaScript的公式解释器 - 16 【MailStone,测试】
- 基于JavaScript的公式解释器 - 1 【设计】
- 基于JavaScript的公式解释器 - 2 【完善自己的类型系统】
- 基于JavaScript的公式解释器 - 11 【函数的实现】
- 基于JavaScript的公式解释器 - 3 【操作数类型的实现】
- 基于JavaScript的公式解释器 - 4 【操作符基类型的实现】
- 基于JavaScript的公式解释器 - 5 【一元操作符的实现】
- 基于JavaScript的公式解释器 - 12 【公式解析器】
- 基于JavaScript的公式解释器 - 13 【公式估值器的实现】
- 基于JavaScript的公式解释器 - 6 【二元操作符基类的实现】
- 【原创整理,基于JavaScript的创建对象方式的集锦】
- JavaScript对象的创建之外部属性定义方式(基于已有对象扩充其属性和方法)
- JavaScript--基于对象的脚本语言学习笔记(一)
- 详解JavaScript基于面向对象之创建对象(2)
- javascript基于对象编程基础