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