diff options
Diffstat (limited to 'split.go')
-rw-r--r-- | split.go | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/split.go b/split.go new file mode 100644 index 0000000..bfcf474 --- /dev/null +++ b/split.go @@ -0,0 +1,114 @@ +package badtudexo + +import ( + "fmt" + "strings" + "os" + xl "git.gabbott.dev/george/excelize/v2" + st "saggytrousers" +) + +type SplitAlgorithm int +const ( + IndivAlgorithm SplitAlgorithm = iota + 1 + GroupAlgorithm +) + +const SaveDirectory string = "bt-split" + +// rows [][]any must NOT include the header. +func indivSplit(rows [][]any, header []string, index int, dir, fn string) { + st.Log(false, "bt.indivSplit: index (%v), dir (%v), fn (%v).", index, dir, fn) + var uniq []any + saveDir := fmt.Sprintf("%s/%s", dir, SaveDirectory) + st.Log(false, "bt.indivSplit: saveDir = %v\n", saveDir) + os.Mkdir(saveDir, 0777) + + // First pass: get all the unique values. + for _, row := range rows { + v := row[index] + if !st.InSliceAny(uniq, v) { + uniq = append(uniq, v) + } + } + + // Rest of the passes: for each unique value, grab them and chuck all + // entries matching it to a file, then save it. + count, total := 0, 0 + for _, v := range uniq { + //if (n == 0) { /* change for _, ... to: for n, ... if using + // // Skip the first one, as it is the header. + // continue + //} + pos := 1 + suffix := deriveSplitSuffix(v) + filename := st.CreateFilepath(dir, SaveDirectory, fn, suffix) + sheetname := st.MakeValidSheetName(st.CreateFilename(fn, suffix)) + + + // Create the new file, and append the header to it. + file := xl.NewFile() + file.SetSheetName("Sheet1", sheetname) + st.AppendToFile(file, sheetname, header, pos) + pos++ + + // Now add each matching row to the file as well. + for _, row := range rows { + sv := row[index] + if sv == v { + st.AppendToFile(file, sheetname, row, pos) + pos++ + } + } + + // Finally, save the file, close it, and we can do it all again + // for the next unique value. + filename = fmt.Sprintf("%s.xlsx", filename) + if err := file.SaveAs(filename); err != nil { + st.ErrLog(true, "Unable to save [%s]\n", filename) + } else { + st.Log(true, "Saved [%s]\n", filename) + count++ + } + total++ + } + st.Log(true, "Saved %v of %v files successfully.\n", count, total) +} + +func deriveSplitSuffix(str any) string { + s:= fmt.Sprintf("%v", str) + if strings.HasSuffix(s, "UTC") { + return s[0:10] + } + + return s +} + +// func groupSplit(rows [][]string, header []string, index int, dir, fn string) map[string]*xl.File { +// } +// +// func groupSave(m map[string]*xl.File) { +// } + + + +// Takes rows, and splits on the given index. Then saves each of these splits +// as a separate file at the specified location and with the specified name. +// rows must NOT include the header. +func Split(rows [][]any, header []string, index int, + dir, filename string, + algo SplitAlgorithm) { + + switch algo { + case IndivAlgorithm: + st.Log(false, "badtudexo.Split: algorithm = IndivAlgorithm\n") + indivSplit(rows, header, index, dir, filename) + // case GroupAlgorithm: + // // TODO: implement these again. + // m := groupSplit(rows, header, index, dir, filename) + // groupSave(m) + default: + st.ErrLog(true, "badtudexo.Split received an invalid algorithm.\n") + os.Exit(-1) + } +} |