Javascript-based formula interpreter-14 [Cell Object]

Source: Internet
Author: User

Cell Object

File: 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. _ absolut ERow = 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": // Assignme Nt <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 .exe c (val ); <br/> if (match! = Null) {<br/> this. set_AbsoluteCol (true); <br/>}< br/> else {<br/> match =/([a-z] {1, 2})/I .exe c (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 (mat Ch [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. _ absoluteAddres S = 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 thi S. 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 .exe c (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 .exe c (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 .exe c (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 .exe c (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/>

Note that the Cell. _ content object is not used. Leave it to be completed by a useful person.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.