基於JavaScript的公式解譯器 – 14 【儲存格對象】

來源:互聯網
上載者:User

Cell儲存格對象

檔案:Cell.js

// JScript source code<br />var $MIN_ROW = 1,<br /> $MAX_ROW = 65535,<br /> $MIN_COL = 1,<br /> $MAX_COL = 256,<br /> $COL_STEP = 26;<br />// -------------- Cell ---------------<br />function Cell(addrStr, absoluteAddr) {<br /> this.set_ClassName("Cell");<br /> this._row = 1;<br /> this._col1 = 0;<br /> this._col2 = 1;<br /> this._content = null;<br /> this._absoluteCol = false;<br /> this._absoluteRow = false;<br /> if (arguments.length > 0) {<br /> switch ($T(addrStr)) {<br /> case "Cell": // copy<br /> {<br /> this.set_Row(addrStr.get_Row());<br /> this.set_Col(addrStr.get_Col());<br /> this.set_Content(addrStr.get_Content());<br /> this.set_AbsoluteCol(addrStr.get_AbsoluteCol());<br /> this.set_AbsoluteRow(addrStr.get_AbsoluteRow());<br /> }<br /> break;<br /> case "string": // Assignment<br /> {<br /> this.FromAddress(addrStr);</p><p> if (arguments.length > 0)<br /> this._absoluteAddress = absoluteAddr == true;<br /> }<br /> break;<br /> } // switch<br /> } // if arguments<br />};<br />Cell.prototype = {<br /> // Property: Content<br /> get_Content: function() { return this._content; },<br /> set_Content: function(val) { this._content = val; },<br /> // Property: Row<br /> get_Row: function() { return this._row; },<br /> set_Row: function(val) {<br /> if (arguments.length == 0)<br /> throw new Exception(this, "set_Row", "Need an argument");<br /> switch ($T(val)) {<br /> case "number":<br /> {<br /> this._row = val.toFixed();<br /> }<br /> break;<br /> case "string":<br /> {<br /> var match = //$(/d+)/.exec(val);<br /> if (match != null) {<br /> this.set_AbsoluteRow(true);<br /> }<br /> else {<br /> match = /(/d+)/.exec(val);<br /> if (match != null)<br /> this.set_AbsoluteRow(false);<br /> } // if ... else match $123<br /> if (match == null)<br /> throw new Exception(this, "set_Row", "Wrong address type:" + val);<br /> this._row = parseInt(match[1]);<br /> }<br /> break;<br /> default: throw new Exception(this, "set_Row", "Unsupported argument type:" + $T(val));<br /> } // switch<br /> },<br /> // Property: Col<br /> get_Col: function() { return this._col1 * $COL_STEP + this._col2; },<br /> set_Col: function(val) {<br /> if (arguments.length == 0)<br /> throw new Exception(this, "set_Col", "Need an argument");<br /> switch ($T(val)) {<br /> case "number":<br /> {<br /> this._col1 = (val / $COL_STEP).toFixed();<br /> this._col2 = (val - ($COL_STEP * this._col1)).toFixed();<br /> }<br /> break;<br /> case "string":<br /> {<br /> var col1 = 0, col2 = 0;<br /> var match = //$([a-z]{1, 2})/i.exec(val);<br /> if (match != null) {<br /> this.set_AbsoluteCol(true);<br /> }<br /> else {<br /> match = /([a-z]{1,2})/i.exec(val);<br /> if (match != null)<br /> this.set_AbsoluteCol(false);<br /> } // if ... else match<br /> if (match == null)<br /> throw new Exception(this, "set_Col", "Invalid address:" + val);<br /> this.set_AbsoluteCol(false);<br /> // absolute<br /> switch (match[1].length) {<br /> case 1:<br /> {<br /> col2 = $Ord(match[1].charAt(0)) - $Ord('A') + $MIN_COL;<br /> }<br /> break;<br /> case 2:<br /> {<br /> col1 = $Ord(match[1].charAt(0)) - $Ord('A') + $MIN_COL;<br /> col2 = $Ord(match[1].charAt(1)) - $Ord('A') + $MIN_COL;<br /> }<br /> break;<br /> default: throw new Exception(this, "FromAddress", "Invalid address:" + val);<br /> } // switch<br /> var col = col1 * $COL_STEP + col2;<br /> if (col < $MIN_COL || col > $MAX_COL)<br /> throw new Exception(this, "set_Col", "Column overrange");<br /> this._absoluteAddress = absAddr;<br /> this._col1 = col1;<br /> this._col2 = col2;<br /> }<br /> break;<br /> default: throw new Exception(this, "set_Col", "Invalid format:" + val);<br /> } // switch<br /> },<br /> // Property: Absolute Col/row<br /> get_AbsoluteCol: function() { return this._absoluteCol; },<br /> set_AbsoluteCol: function(val) { this._absoluteCol = val; },<br /> get_AbsoluteRow: function() { return this._absoluteRow; },<br /> set_AbsoluteRow: function(val) { this._absoluteRow = val; },<br /> // Property: IsValid<br /> IsValid: function() {<br /> return<br /> this._row >= $MIN_ROW &&<br /> this._row <= $MAX_ROW &&<br /> this._col >= $MIN_COL &&<br /> this._col <= $MAX_COL;<br /> },<br /> // Function: ToString<br /> // Return the formatted string address<br /> ToString: function() { return this.ToAddress(); },<br /> // Function: ToAddress()<br /> // Return the formated cell address.<br /> ToAddress: function() {<br /> var col = (this.get_AbsoluteCol() ? "$" : "") +<br /> $Chr($ALPHABET, this._col2 - $MIN_COL); ;<br /> if (this._col1 > 0) {<br /> col = (this.get_AbsoluteCol() ? "$" : "") +<br /> $Chr($ALPHABET, this._col1 - $MIN_COL) +<br /> col;<br /> }<br /> return col +<br /> (this.get_AbsoluteRow() ? "$" : "") + this.get_Row().toString();<br /> }, // function ToAddress<br /> // Function: FromAddress(addr)<br /> // Convert an address string to row/col position.<br /> FromAddress: function(addrStr) {<br /> if (arguments.length == 0)<br /> throw new Exception(this, "FromAddress", "Need arguments");<br /> var row = 0, col1 = 0, col2 = 0;<br /> var match = //$([a-z]{1,2})/$(/d+)/i.exec(addrStr);<br /> if (match != null) {<br /> this.set_AbsoluteCol(true);<br /> this.set_AbsoluteRow(true);<br /> }<br /> else {<br /> match = //$([a-z]{1,2})(/d+)/i.exec(addrStr);<br /> if (match != null) {<br /> this.set_AbsoluteCol(true);<br /> this.set_AbsoluteRow(false);<br /> }<br /> else {<br /> match = /([a-z]{1,2})/$(/d+)/i.exec(addrStr);<br /> if (match != null) {<br /> this.set_AbsoluteCol(false);<br /> this.set_AbsoluteRow(false);<br /> }<br /> else {<br /> match = /([a-z]{1,2})(/d+)/i.exec(addrStr);<br /> if (match != null) {<br /> this.set_AbsoluteCol(false);<br /> this.set_AbsoluteRow(false);<br /> } // if ... else match 4: AB123<br /> } // if ... else match 3: AB$123<br /> } // if ... else match 2: $AB123<br /> } // if ... else match: $AB$123<br /> if (match == null)<br /> throw new Exception(this, "FromAddress", "Invalid address:" + addrStr);<br /> // absolute<br /> switch (match[1].length) {<br /> case 1:<br /> {<br /> col2 = $Ord(match[1].charAt(0)) - $Ord('A') + $MIN_COL;<br /> }<br /> break;<br /> case 2:<br /> {<br /> col1 = $Ord(match[1].charAt(0)) - $Ord('A') + $MIN_COL;<br /> col2 = $Ord(match[1].charAt(1)) - $Ord('A') + $MIN_COL;<br /> }<br /> break;<br /> default: throw new Exception(this, "FromAddress", "Invalid address:" + addrStr);<br /> } // switch<br /> row = parseInt(match[2]);<br /> var col = col1 * $COL_STEP + col2;<br /> if (col < $MIN_COL || col > $MAX_COL || row < $MIN_ROW || row > $MAX_ROW)<br /> throw new Exception(this, "FromAddress", "Address over range:" + addrStr);<br /> this._col1 = col1;<br /> this._col2 = col2;<br /> this._row = row;<br /> }, // function FromAddress<br /> // ------------------------ Test ------------------------<br /> Test: function() {<br /> var str = [<br /> "Ax12",<br /> "A0",<br /> "A1",<br /> "A5",<br /> "A9",<br /> "A999",<br /> "Z0",<br /> "Z1",<br /> "Z5",<br /> "Z9",<br /> "Z999",<br /> "ZZ0",<br /> "ZZ9",<br /> "ZZ5",<br /> "AZ0",<br /> "AZ1",<br /> "AZ9",<br /> "AZ999",<br /> "ZA0",<br /> "ZA1",<br /> "ZA9",<br /> "ZA999",<br /> "$A$1",<br /> "$A$B$255",<br /> "$A1"<br /> ];<br /> var cell = new Cell();<br /> for (var v in str) {<br /> if ($T(str[v]) == "function")<br /> continue;<br /> try {<br /> cell.FromAddress(str[v]);<br /> $Debug.WriteLine(str[v] + "=" + cell.ToAddress());<br /> }<br /> catch (e) {<br /> $Debug.WriteLine("Invalid address:" + str[v] + "/nException:" + e.description);<br /> } // try ... catch<br /> } // for<br /> } // Test<br />}; // prototype<br />

注意,Cell._content對象未使用,留待有用之人來完成吧。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.