diff --git a/files.go b/files.go index 26fcad0..4e5f2a6 100644 --- a/files.go +++ b/files.go @@ -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) } } @@ -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')) +} diff --git a/files_test.go b/files_test.go new file mode 100644 index 0000000..f33d758 --- /dev/null +++ b/files_test.go @@ -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() + } + } + +}