mirror of
https://github.com/optilude/xlsx-template.git
synced 2026-07-02 08:27:39 +08:00
Convert between characters and numbers
This commit is contained in:
@@ -338,6 +338,39 @@ module.exports = (function() {
|
||||
});
|
||||
};
|
||||
|
||||
// Turn a reference like "AA" into a number like 27
|
||||
Workbook.prototype.charToNum = function(str) {
|
||||
var num = 0;
|
||||
for(var idx = str.length - 1, iteration = 0; idx >= 0; --idx, ++iteration) {
|
||||
var thisChar = str.charCodeAt(idx) - 64, // A -> 1; B -> 2; ... Z->26
|
||||
multiplier = Math.pow(26, iteration);
|
||||
num += multiplier * thisChar;
|
||||
}
|
||||
return num;
|
||||
};
|
||||
|
||||
// Turn a number like 27 into a reference like "AA"
|
||||
Workbook.prototype.numToChar = function(num) {
|
||||
var str = "";
|
||||
|
||||
|
||||
for(var i = 0; num > 0; ++i) {
|
||||
var remainder = num % 26,
|
||||
charCode = remainder + 64;
|
||||
num = (num - remainder) / 26;
|
||||
|
||||
// Compensate for the fact that we don't represent zero, e.g. A = 1, Z = 26, but AA = 27
|
||||
if(remainder === 0) { // 26 -> Z
|
||||
charCode = 90;
|
||||
--num;
|
||||
}
|
||||
|
||||
str = String.fromCharCode(charCode) + str;
|
||||
}
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
Workbook.prototype.stringify = function (value) {
|
||||
var self = this;
|
||||
|
||||
|
||||
@@ -236,6 +236,62 @@ describe("Helpers", function() {
|
||||
|
||||
});
|
||||
|
||||
describe('charToNum', function() {
|
||||
|
||||
it("can return single letter numbers", function() {
|
||||
var t = new XlsxTemplate();
|
||||
|
||||
expect(t.charToNum("A")).toEqual(1);
|
||||
expect(t.charToNum("B")).toEqual(2);
|
||||
expect(t.charToNum("Z")).toEqual(26);
|
||||
});
|
||||
|
||||
it("can return double letter numbers", function() {
|
||||
var t = new XlsxTemplate();
|
||||
|
||||
expect(t.charToNum("AA")).toEqual(27);
|
||||
expect(t.charToNum("AZ")).toEqual(52);
|
||||
expect(t.charToNum("BZ")).toEqual(78);
|
||||
});
|
||||
|
||||
it("can return triple letter numbers", function() {
|
||||
var t = new XlsxTemplate();
|
||||
|
||||
expect(t.charToNum("AAA")).toEqual(703);
|
||||
expect(t.charToNum("AAZ")).toEqual(728);
|
||||
expect(t.charToNum("ADI")).toEqual(789);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('numToChar', function() {
|
||||
|
||||
it("can convert single letter numbers", function() {
|
||||
var t = new XlsxTemplate();
|
||||
|
||||
expect(t.numToChar(1)).toEqual("A");
|
||||
expect(t.numToChar(2)).toEqual("B");
|
||||
expect(t.numToChar(26)).toEqual("Z");
|
||||
});
|
||||
|
||||
it("can convert double letter numbers", function() {
|
||||
var t = new XlsxTemplate();
|
||||
|
||||
expect(t.numToChar(27)).toEqual("AA");
|
||||
expect(t.numToChar(52)).toEqual("AZ");
|
||||
expect(t.numToChar(78)).toEqual("BZ");
|
||||
});
|
||||
|
||||
it("can convert triple letter numbers", function() {
|
||||
var t = new XlsxTemplate();
|
||||
|
||||
expect(t.numToChar(703)).toEqual("AAA");
|
||||
expect(t.numToChar(728)).toEqual("AAZ");
|
||||
expect(t.numToChar(789)).toEqual("ADI");
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('stringify', function() {
|
||||
|
||||
it("can stringify dates", function() {
|
||||
|
||||
Reference in New Issue
Block a user