Skip to content

Commit

Permalink
Merge pull request #12 from pcvolkmer/features/10_excelcolumns
Browse files Browse the repository at this point in the history
feat: support columns with more than one char
  • Loading branch information
pcvolkmer authored Dec 19, 2024
2 parents 1c8d7e4 + 18b03a7 commit 9160472
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
19 changes: 15 additions & 4 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func addPatientData(file *excelize.File, index int, patientData []PatientData) e
file.SetActiveSheet(index)

for idx, columnHeader := range PatientDataHeaders() {
cell := string(rune(idx+'A')) + "1"
cell := getColumn(idx) + "1"
_ = file.SetCellValue("Patients Data", cell, columnHeader)
}

for row, data := range patientData {
for idx, value := range data.AsStringArray() {
cell := string(rune(idx+'A')) + fmt.Sprint(row+2)
cell := getColumn(idx) + fmt.Sprint(row+2)
_ = file.SetCellValue("Patients Data", cell, value)
}
}
Expand All @@ -108,16 +108,27 @@ func addSampleData(file *excelize.File, index int, sampleData []SampleData) erro
file.SetActiveSheet(index)

for idx, columnHeader := range SampleDataHeaders() {
cell := string(rune(idx+'A')) + "1"
cell := getColumn(idx) + "1"
_ = file.SetCellValue("Samples Data", cell, columnHeader)
}

for row, data := range sampleData {
for idx, value := range data.AsStringArray() {
cell := string(rune(idx+'A')) + fmt.Sprint(row+2)
cell := getColumn(idx) + fmt.Sprint(row+2)
_ = file.SetCellValue("Samples Data", cell, value)
}
}

return nil
}

func getColumn(idx int) string {
z := int('Z' - 'A' + 1)
m := idx % z
if idx <= m {
return string(rune(idx + 'A'))
}

r := ((idx - m) / z) - 1
return string(rune(r+'A')) + string(rune(m+'A'))
}
22 changes: 22 additions & 0 deletions files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "testing"

func TestShouldReturnExpectedColumn(t *testing.T) {

testsArgs := map[int]string{
0: "A",
25: "Z",
26: "AA",
51: "AZ",
}

for key, value := range testsArgs {
actual := getColumn(key)
if actual != value {
t.Logf("wrong Column: Expected %s, got %s", value, actual)
t.Fail()
}
}

}

0 comments on commit 9160472

Please sign in to comment.