Skip to content

Commit e68018b

Browse files
author
Michael Barrett
committed
initial commit
0 parents  commit e68018b

13 files changed

+284
-0
lines changed

.codeclimate.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
languages:
2+
JavaScript: true
3+
4+
exclude_paths:
5+
- spec.js

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_style = space
7+
insert_final_newline = true
8+
indent_size = 2
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.jscsrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"preset": "node-style-guide"
3+
}

.npmignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/*
2+
!/index.js

.travis.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
language: node_js
2+
3+
node_js:
4+
- "0.10"
5+
- "0.11"
6+
- "0.12"
7+
- "4.0"
8+
- "4.1"
9+
- "iojs"
10+
11+
sudo: false
12+
13+
cache:
14+
directories:
15+
- node_modules
16+
17+
env:
18+
global:
19+
- secure: lGT6/L/pT/ZUMGmkcNafVqr/yneiicgMAXbA24Iu05wOWLw8VNQm/1LLBNfFHP208jd4CJCott1Obxt5qxtqeJe9ax38EWR1sWK2pdPWSzSfHfXCja8Pe1YASkzApDsz9veqQQw98PoyqkkphZs8rINikKrDPV6AP3GfNZ9gkhu4Atorms7ScJ4QISbHFvWPSQtAeF9GjsdVG1VFdVAJ9uz2dCd1L/raoIwJaqFdK54mXevBSh9SR96PPeGUzK9MrSc5A6R/vJSKtQ0DDIjuv14xT3Tmu0/H4uS9bJYg96FpeZjwl3ih5o39+wLKM3Ex01RkZFOvZP9iO6ohgiwRvNMDmSKTtSyQuSyqeygW/Xht6OW9PmH88mK2kJrONa8m5T7xjHWNnaXi47xc7ZKc4T8ePmVvqjpEX+AvrnnGLsj8b6MdkJGxvm+oz3jKwoas8dY9OPgAES2FVZyiWcR8TOYVM3IF+BbrU0EJL7WauXWWRAaBC0P9lTzKvJRDLPJxhtjAXyoh/9mApgPalKgYU6aKTFX0RTBaUKrD6pg0kkixhAP3rYfFgpNWcpqS0gfPP7E4d6eKXMCbzBFxIsXBbWh/n3cAW+nsZDejT5BwPEz0MiJnh0dqnMvkVKOVUYM2GnhffBhbipk4lS7oGYpj091aXBd2n7mr4dGP0O+av9U=
20+
21+
script:
22+
- npm test
23+
- npm run sa
24+
25+
after_script:
26+
- istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly spec.js
27+
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
28+
- cat ./coverage/lcov.info | ./node_modules/codeclimate-test-reporter/bin/codeclimate.js
29+
- rm -rf ./coverage

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
# 1.0.0
4+
5+
First release

CONTRIBUTING.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
Contributions can be made via a Pull Request on [Github](https://github.com/mike182uk/cellref).
6+
7+
## Pull Requests
8+
9+
- **[Node coding style](https://github.com/felixge/node-style-guide)** - [JSCS](http://jscs.info/). Make sure you run `npm run sa` before committing your code.
10+
11+
- **Add specs where appropriate** - [Mocha](http://mochajs.org/), [Should](https://shouldjs.github.io/)
12+
13+
- **Document any change in behavior** - Make sure the README and any other relevant documentation are kept up-to-date.
14+
15+
- **Create topic branches** - i.e `feature/some-awesome-feature`.
16+
17+
- **One pull request per feature**
18+
19+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.
20+
21+
## Running Specs
22+
23+
```bash
24+
npm test
25+
```

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) Michael David Barrett
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Cellref
2+
3+
[![Version](https://img.shields.io/npm/v/cellref.svg?style=flat-square)](https://www.npmjs.com/package/cellref)
4+
[![Build Status](https://img.shields.io/travis/mike182uk/cellref.svg?style=flat-square)](http://travis-ci.org/mike182uk/cellref)
5+
[![Code Climate](https://img.shields.io/codeclimate/github/mike182uk/cellref.svg?style=flat-square)](https://codeclimate.com/github/mike182uk/cellref)
6+
[![Coveralls](https://img.shields.io/coveralls/mike182uk/cellref/master.svg?style=flat-square)](https://coveralls.io/r/mike182uk/cellref)
7+
[![npm](https://img.shields.io/npm/dm/cellref.svg?style=flat-square)](https://www.npmjs.com/package/cellref)
8+
[![License](https://img.shields.io/github/license/mike182uk/cellref.svg?style=flat-square)](https://www.npmjs.com/package/cellref)
9+
10+
Convert between cell reference styles (`A1` notation & `R1C1` notation).
11+
12+
## Installation
13+
14+
```
15+
npm install --save cellref
16+
```
17+
18+
## Usage
19+
20+
### Convert A1 to R1C1
21+
22+
```js
23+
var cellref = require('cellref');
24+
25+
cellref.toR1C1('BU59'); // will return R59C73
26+
```
27+
28+
### Convert R1C1 to A1
29+
30+
```js
31+
var cellref = require('cellref');
32+
33+
cellref.toA1('R59C73'); // will return BU59
34+
```
35+
36+
### Convert automatically based on the input
37+
38+
```js
39+
var cellref = require('cellref');
40+
41+
cellref('R59C73'); // will return BU59
42+
cellref('BU59'); // will return R59C73
43+
```

index.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* R1C1 pattern
3+
*
4+
* @type {RegExp}
5+
*/
6+
7+
var R1C1 = /R([0-9]+)C([0-9]+)/;
8+
9+
/**
10+
* A1 pattern
11+
*
12+
* @type {RegExp}
13+
*/
14+
15+
var A1 = /(\$?[A-Z]*)(\$?\d*)/;
16+
17+
/**
18+
* Convert A1 notation to R1C1 notation
19+
*
20+
* @param {string} a1Value
21+
* @returns {string}
22+
*/
23+
24+
function A1toR1C1(a1Value) {
25+
var a1Parts = a1Value
26+
.replace(A1, '$1,$2')
27+
.split(',');
28+
29+
var columnStr = a1Parts[0];
30+
var row = a1Parts[1];
31+
var column = 0;
32+
33+
for (var i = 0; i < columnStr.length; i++) {
34+
column = 26 * column + columnStr.charCodeAt(i) - 64;
35+
}
36+
37+
return 'R' + row + 'C' + column;
38+
}
39+
40+
/**
41+
* Convert R1C1 notation to A1 notation
42+
*
43+
* @param {string} r1c1Value
44+
* @returns {string}
45+
*/
46+
47+
function R1C1toA1(r1c1Value) {
48+
var r1c1Parts = r1c1Value
49+
.replace(R1C1, '$1,$2')
50+
.split(',');
51+
52+
var row = r1c1Parts[0];
53+
var column = r1c1Parts[1];
54+
var columnStr = '';
55+
56+
for (; column; column = Math.floor(column / 26)) {
57+
columnStr = String.fromCharCode((column % 26) + 64) + columnStr;
58+
}
59+
60+
return columnStr + row;
61+
}
62+
63+
/**
64+
* Auto detects notation used and converts to the opposite notation
65+
*
66+
* @param {string} value
67+
* @returns {string}
68+
*/
69+
70+
function autoConvert(value) {
71+
if (R1C1.test(value)) {
72+
return R1C1toA1(value);
73+
}
74+
75+
return A1toR1C1(value);
76+
}
77+
78+
/**
79+
* Exports
80+
*/
81+
82+
module.exports = autoConvert;
83+
module.exports.toA1 = R1C1toA1;
84+
module.exports.toR1C1 = A1toR1C1;

package.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "cellref",
3+
"version": "1.0.0",
4+
"description": "Convert between cell reference styles (A1 notation & R1C1 notation)",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha spec.js",
8+
"sa": "jscs index.js && jshint index.js"
9+
},
10+
"author": "Michael David Barrett <mike182uk@gmail.com>",
11+
"license": "MIT",
12+
"repository": {
13+
"type": "git",
14+
"url": "git://github.com/mike182uk/cellref.git"
15+
},
16+
"homepage": "https://github.com/mike182uk/cellref",
17+
"keywords": [
18+
"A1",
19+
"R1C1",
20+
"excel",
21+
"xlsx",
22+
"xls",
23+
"cell",
24+
"spreadsheet"
25+
],
26+
"devDependencies": {
27+
"codeclimate-test-reporter": "^0.1.1",
28+
"coveralls": "^2.11.4",
29+
"istanbul": "^0.4.0",
30+
"jscs": "^2.5.1",
31+
"jshint": "^2.8.0",
32+
"mocha": "^2.3.3",
33+
"mocha-lcov-reporter": "^1.0.0",
34+
"should": "^7.1.1"
35+
}
36+
}

spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var should = require('should');
2+
3+
var cellref = require('./index');
4+
5+
describe('cellref', function () {
6+
it('should convert A1 notation to R1C1 notation', function () {
7+
cellref.toR1C1('A1').should.be.exactly('R1C1');
8+
cellref.toR1C1('BU59').should.be.exactly('R59C73');
9+
});
10+
11+
it('should convert R1C1 notation to A1 notation', function () {
12+
cellref.toA1('R1C1').should.be.exactly('A1');
13+
cellref.toA1('R59C73').should.be.exactly('BU59');
14+
});
15+
16+
it('should auto detect the notation used and convert to the opposite notation', function () {
17+
cellref('R1C1').should.be.exactly('A1');
18+
cellref('BU59').should.be.exactly('R59C73');
19+
});
20+
});

0 commit comments

Comments
 (0)