Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chonla committed Nov 5, 2020
0 parents commit 8b0b9f7
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 0 deletions.
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
The MIT License (MIT)

Copyright (c) Chonlasith Jucksriporn and Contributors

All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Cell Walker

Cell Walker is a go package for virtually traversing Excel cell by cell's name. The package does not actually traverse into a real Excel file.

## Example

```
package main
import (
"fmt"
"github.com/chonla/cellwalker"
)
func main() {
fmt.Println(cellwalker.At("B3").Right().Below().String()) // C4
}
```

## License

[MIT](LICENSE)
140 changes: 140 additions & 0 deletions cellwalker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package cellwalker

import (
"fmt"
"math"
"regexp"
"strconv"
"strings"
)

// RowsLimit https://support.microsoft.com/en-us/office/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3
const (
RowsLimit = 1048576
ColumnsLimit = 16384
)

// CellWalker struct
type CellWalker struct {
column int
row int
}

// At initializes CellWalker by specify initial cell to start
func At(cellID string) *CellWalker {
cleanCellID := strings.ToUpper(cellID)
re := regexp.MustCompile(`^([A-Z]+)([0-9]*)$`)
match := re.FindStringSubmatch(cleanCellID)

col := match[1]
row, err := strconv.ParseInt(fmt.Sprintf("0%s", match[2]), 10, 32)
if err != nil || row == 0 {
row = 1
}

return &CellWalker{
column: ColumnNameToIndex(col),
row: int(row),
}
}

// ColumnIndexToName converts column index to default excel name
func ColumnIndexToName(id int) string {
name := ""
dividend := id
modulo := 0

for dividend > 0 {
modulo = (dividend - 1) % 26
name = fmt.Sprintf("%c%s", rune(modulo+'A'), name)
dividend = (dividend - modulo) / 26
}
return name
}

// ColumnNameToIndex converts default excel column name to index, 1-based index
// name must be uppercase start from A, B, C, ..., Z, AA, AB, ... ZZ, AAA, ..., ZZZ, ...
func ColumnNameToIndex(name string) int {
index := 0
for colCharIndex, colCharLen := 0, len(name); colCharIndex < colCharLen; colCharIndex++ {
charNum := int(name[colCharIndex]-'A') + 1
digitNum := (colCharLen - (colCharIndex + 1))
columnWeight := int(math.Pow(26.0, float64(digitNum)))
columnValue := charNum * columnWeight
index += columnValue
}
return index
}

// String representation of Cell
func (c *CellWalker) String() string {
return fmt.Sprintf("%s%d", ColumnIndexToName(c.column), c.row)
}

// Above to move up one row
func (c *CellWalker) Above() *CellWalker {
rowAbove := c.row - 1
if rowAbove < 1 {
rowAbove = 1
}
return &CellWalker{
column: c.column,
row: rowAbove,
}
}

// Below to move down one row
func (c *CellWalker) Below() *CellWalker {
rowBeneath := c.row + 1
if rowBeneath > RowsLimit {
rowBeneath = RowsLimit
}
return &CellWalker{
column: c.column,
row: rowBeneath,
}
}

// Right to move right one column
func (c *CellWalker) Right() *CellWalker {
rowRight := c.column + 1
if rowRight > ColumnsLimit {
rowRight = ColumnsLimit
}
return &CellWalker{
column: rowRight,
row: c.row,
}
}

// Left to move left one column
func (c *CellWalker) Left() *CellWalker {
rowLeft := c.column - 1
if rowLeft < 1 {
rowLeft = 1
}
return &CellWalker{
column: rowLeft,
row: c.row,
}
}

// CanMoveLeft determines if it is at the left most cell
func (c *CellWalker) CanMoveLeft() bool {
return c.column > 1
}

// CanMoveRight determines if it is at the right most cell
func (c *CellWalker) CanMoveRight() bool {
return c.column < ColumnsLimit
}

// CanMoveUp determines if it is at the up most cell
func (c *CellWalker) CanMoveUp() bool {
return c.row > 1
}

// CanMoveDown determines if it is at the bottom most cell
func (c *CellWalker) CanMoveDown() bool {
return c.row < RowsLimit
}
175 changes: 175 additions & 0 deletions cellwalker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package cellwalker

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestConvertColumnIndexToSingleColumnName(t *testing.T) {
result := ColumnIndexToName(1)

assert.Equal(t, "A", result)
}

func TestConvertLastSingleColumnIndexToSingleColumnName(t *testing.T) {
result := ColumnIndexToName(26)

assert.Equal(t, "Z", result)
}

func TestConvertColumnIndexTo2CharactersColumnName(t *testing.T) {
result := ColumnIndexToName(27)

assert.Equal(t, "AA", result)
}

func TestConvertLastColumnIndexTo2CharactersColumnName(t *testing.T) {
result := ColumnIndexToName(52)

assert.Equal(t, "AZ", result)
}

func TestConvertComplexLastColumnIndexTo2CharactersColumnName(t *testing.T) {
result := ColumnIndexToName(702)

assert.Equal(t, "ZZ", result)
}

func TestColumnIndexTo3CharactersColumnName(t *testing.T) {
result := ColumnIndexToName(703)

assert.Equal(t, "AAA", result)
}

func TestColumnNameToSingleDigitIndex(t *testing.T) {
result := ColumnNameToIndex("A")

assert.Equal(t, 1, result)
}

func TestLast1CharColumnNameToIndex(t *testing.T) {
result := ColumnNameToIndex("Z")

assert.Equal(t, 26, result)
}

func TestFirst2CharsColumnNameToIndex(t *testing.T) {
result := ColumnNameToIndex("AA")

assert.Equal(t, 27, result)
}

func TestSpecifyCellWithCellID(t *testing.T) {
result := At("A1").String()

assert.Equal(t, "A1", result)
}

func TestSpecifyCellWithRowOnly(t *testing.T) {
result := At("B").String()

assert.Equal(t, "B1", result)
}

func TestSpecifyCellWithLargeRowOnly(t *testing.T) {
result := At("BBBC").String()

assert.Equal(t, "BBBC1", result)
}

func TestMoveCurrentCellUpward(t *testing.T) {
result := At("A2").Above().String()

assert.Equal(t, "A1", result)
}

func TestMoveTopMostCellUpwardShouldGoNowhere(t *testing.T) {
result := At("A1").Above().String()

assert.Equal(t, "A1", result)
}

func TestMoveCurrentCellDownward(t *testing.T) {
result := At("A2").Below().String()

assert.Equal(t, "A3", result)
}

func TestMoveBottomMostCellDownwardShouldGoNowhere(t *testing.T) {
result := At("A1048576").Below().String()

assert.Equal(t, "A1048576", result)
}

func TestMoveCurrentCellRightward(t *testing.T) {
result := At("A1").Right().String()

assert.Equal(t, "B1", result)
}

func TestMoveRightMostCellRightwardShouldGoNowhere(t *testing.T) {
result := At("XFD1").Right().String()

assert.Equal(t, "XFD1", result)
}

func TestMoveCurrentCellLeftward(t *testing.T) {
result := At("B1").Left().String()

assert.Equal(t, "A1", result)
}

func TestMoveLeftMostCellLeftwardShouldGoNowhere(t *testing.T) {
result := At("A1").Left().String()

assert.Equal(t, "A1", result)
}

func TestCanMoveLeftShouldReturnTrueIfNotAtTheLeftMostCell(t *testing.T) {
result := At("B1").CanMoveLeft()

assert.True(t, result)
}

func TestCanMoveLeftShouldReturnFalseIfAtTheLeftMostCell(t *testing.T) {
result := At("A1").CanMoveLeft()

assert.False(t, result)
}

func TestCanMoveRightShouldReturnTrueIfNotAtTheRightMostCell(t *testing.T) {
result := At("XFC1").CanMoveRight()

assert.True(t, result)
}

func TestCanMoveRightShouldReturnFalseIfAtTheRightMostCell(t *testing.T) {
result := At("XFD1").CanMoveRight()

assert.False(t, result)
}

func TestCanMoveUpShouldReturnTrueIfNotAtTheTopMostCell(t *testing.T) {
result := At("A2").CanMoveUp()

assert.True(t, result)
}

func TestCanMoveUpShouldReturnFalseIfAtTheTopMostCell(t *testing.T) {
result := At("A1").CanMoveUp()

assert.False(t, result)
}

func TestCanMoveDownShouldReturnTrueIfNotAtTheDownMostCell(t *testing.T) {
result := At("A1048575").CanMoveDown()

assert.True(t, result)
}

func TestCanMoveDownShouldReturnFalseIfAtTheDownMostCell(t *testing.T) {
result := At("A1048576").CanMoveDown()

assert.False(t, result)
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/chonla/cellwalker

go 1.15

require github.com/stretchr/testify v1.6.1
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 8b0b9f7

Please sign in to comment.