DocuSort is a collection of Go packages that are meant to make sorting .xlsx
, .csv
, and other spreadsheet files much easier. It currently only supports .xlsx
files (the dsx package), but I have plans in the future to support various other file extensions.
Uses the Go package excelize to access .xlsx
files. Dsx has built in methods to make tasks that involve large data sets much simpler.
go get github.com/Cosiamo/DocuSort/dsx
import (
"github.com/Cosiamo/DocuSort/dsx"
)
func main() {
// OpenXlsx("[PATH]/<file name>")
file := dsx.OpenXlsx("spreadsheets/testSheet.xlsx")
// either call CloseXlsx with 'defer'
// or at the end of the function
defer dsx.CloseXlsx(file)
}
This is for when you need to get specific groups of data. Let's say you're working with a spreadsheet that has list of computers. In column "A" you have the brand names, in column "B" the product number, in column "C" the MAC address, and column "D" if they're active or not. If you want to get all data from a specific brand, the Group
method will return all the cells with the relevant info.
func main() {
file := dsx.OpenXlsx("spreadsheets/testSheet.xlsx")
defer dsx.CloseXlsx(file)
sheet := "Sheet1"
var columns []string = []string{"A", "B", "C", "D"}
// want to return all info about Apple computers from the spreadsheet
// all the rows that have the value "Apple" as brand
var rows []int = []int{2, 3, 4, 5, 6}
// returns values of cells from an array of strings and ints
for res := range dsx.Group(f, sheet, columns, rows) {
fmt.Println(res)
}
}
If you're working with dozens, hundreds, or even thousands of rows that you need grouped, the RangeOfRows
method will let you set a start-row and end-row so you don't have to manually type in all row numbers. (This gives the same result as the previous example)
func main() {
file := dsx.OpenXlsx("spreadsheets/testSheet.xlsx")
defer dsx.CloseXlsx(f)
sheet := "Sheet1"
var columns []string = []string{"A", "B", "C", "D"}
startRow := 2
endRow := 6
// returns int slices within params passed into the method
for rows := range dsx.RangeOfRows(startRow, endRow) {
for res := range dsx.Group(file, sheet, columns, rows) {
fmt.Println(res)
}
}
}
If you're working a lot of columns, the RangeOfCols
method will let you set a start-column and end-column. It can take values anywhere from "A" to "ZZ".
func main() {
file := dsx.OpenXlsx("spreadsheets/testSheet.xlsx")
defer dsx.CloseXlsx(f)
sheet := "Sheet1"
startCol := "A"
endCol := "D"
startRow := 2
endRow := 6
for rows := range dsx.RangeOfRows(startRow, endRow) {
// returns string slices within params passed into the method
for columns := range RangeOfCols(startCol, endCol) {
for res := range dsx.Group(file, sheet, columns, rows) {
fmt.Println(res)
}
}
}
}
Maps the cells coordinates to the parameters that are provided.
func main() {
file := dsx.OpenXlsx("spreadsheets/testSheet.xlsx")
defer dsx.CloseXlsx(f)
sheet := "Sheet1"
var columns []string = []string{"A", "B", "C", "D"}
var rows []int = []int{2, 3, 4, 5, 6}
for res := range dsx.MapGroupCoords(columns, rows) {
fmt.Println(res)
}
}
func main() {
file := dsx.OpenXlsx("spreadsheets/testSheet.xlsx")
defer dsx.CloseXlsx(file)
sheet := "Sheet1"
// returns all data from the spreadsheet by row
for data := range dsx.AllDataByRows(file, sheet) {
fmt.Print(data)
fmt.Println()
}
}
func main() {
file := dsx.OpenXlsx("spreadsheets/testSheet.xlsx")
defer dsx.CloseXlsx(file)
sheet := "Sheet1"
// returns all data from the spreadsheet by column
for data := range dsx.AllDataByCols(file, sheet) {
fmt.Print(data)
fmt.Println()
}
}