Documentation
¶
Overview ¶
Package xlsxtra provides extra utilities for the xlsx package (https://github.com/tealeg/xlsx) to manipulate excel files:
- Sort(), SortByHeaders: multi-column (reverse) sort of selected rows. (Note that columns are one based, not zero based to make reverse sort possible.)
- AddBool(), AddInt(), AddFloat(), ...: shortcut to add a cell to a row with the right type.
- NewStyle(): create a style and set the ApplyFill, ApplyFont, ApplyBorder and ApplyAlignment automatically.
- NewStyles(): create a slice of styles based on a color palette
- Sheets: access sheets by name instead of by index
- Col: access cell values of a row by column header title
- SetRowStyle: set style of all cells in a row
- ToString: convert a xlsx.Row to a slice of strings
See Col(umn) and Sort example for a quick introduction.
Example ¶
package main import ( "fmt" "github.com/stanim/xlsxtra" ) func main() { fmt.Println(xlsxtra.ColStr[26], xlsxtra.StrCol["AA"]) }
Output: Z 27
Index ¶
- Variables
- func Abs(s string) string
- func ColRange(start, end string) []string
- func Coord(col, row int) string
- func CoordAbs(col, row int) string
- func NewStyle(color string, font *xlsx.Font, border *xlsx.Border, align *xlsx.Alignment) *xlsx.Style
- func NewStyles(colors []string, font *xlsx.Font, border *xlsx.Border, align *xlsx.Alignment) []*xlsx.Style
- func RangeBounds(rg string) (int, int, int, int, error)
- func Sort(sheet *Sheet, start, end int, columns ...int)
- func SortByHeaders(sheet *Sheet, start, end int, col Col, headers ...string) error
- func SplitCoord(coord string) (string, int, error)
- func ToString(cells []*xlsx.Cell) []string
- func Transpose(cells [][]*xlsx.Cell) [][]*xlsx.Cell
- type Col
- func (c Col) Bool(row *Row, header string) (bool, error)
- func (c Col) BoolMap(row *Row, headers []string) (map[string]bool, error)
- func (c Col) Float(row *Row, header string) (float64, error)
- func (c Col) Index(title string) (int, error)
- func (c Col) IndexRow(row *Row, title string) (int, error)
- func (c Col) Indices(headers ...string) ([]int, error)
- func (c Col) Int(row *Row, header string) (int, error)
- func (c Col) String(row *Row, header string) (string, error)
- func (c Col) StringFloatMap(row *Row, header string, dmap map[string]float64, val float64, sep string, ...) error
- type File
- type MultiColumnSort
- type Row
- func (row *Row) AddBool(x ...bool) *xlsx.Cell
- func (row *Row) AddEmpty(n int)
- func (row *Row) AddFloat(format string, x ...float64) *xlsx.Cell
- func (row *Row) AddFormula(format string, formula ...string) *xlsx.Cell
- func (row *Row) AddInt(x ...int) *xlsx.Cell
- func (row *Row) AddString(x ...string) *xlsx.Cell
- func (row *Row) SetStyle(style *xlsx.Style)
- type Sheet
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ColStr maps an integer column index to its name ColStr [maxCol]string // StrCol maps a column name to its integer index StrCol = make(map[string]int) )
Functions ¶
func Abs ¶
Abs converts a coordinate to an absolute coordinate (An invalid string is returned unaltered.)
Example ¶
package main import ( "fmt" "github.com/stanim/xlsxtra" ) func main() { fmt.Println(xlsxtra.Abs("B12")) fmt.Println(xlsxtra.Abs("C5:G20")) fmt.Println(xlsxtra.Abs("Invalid")) }
Output: $B$12 $C$5:$G$20 Invalid
func ColRange ¶
ColRange gives a range of intervals. (Returns empty slice for invalid input.)
Example ¶
package main import ( "fmt" "github.com/stanim/xlsxtra" ) func main() { fmt.Println(xlsxtra.ColRange("X", "AD")) fmt.Println(xlsxtra.ColRange("1", "AD")) }
Output: [X Y Z AA AB AC AD] []
func Coord ¶
Coord converts integer col and row to string coordinate. (col is one based.)
Example ¶
package main import ( "fmt" "github.com/stanim/xlsxtra" ) func main() { fmt.Println(xlsxtra.Coord(2, 12)) fmt.Println(xlsxtra.Coord(0, 12)) }
Output: B12 ?12
func CoordAbs ¶
CoordAbs converts integer col and row to absolute string coordinate. (col is one based.)
Example ¶
package main import ( "fmt" "github.com/stanim/xlsxtra" ) func main() { fmt.Println(xlsxtra.CoordAbs(2, 12)) fmt.Println(xlsxtra.CoordAbs(0, 12)) }
Output: $B$12 ?12
func NewStyle ¶
func NewStyle(color string, font *xlsx.Font, border *xlsx.Border, align *xlsx.Alignment) *xlsx.Style
NewStyle creates a new style with color and boldness
func NewStyles ¶
func NewStyles(colors []string, font *xlsx.Font, border *xlsx.Border, align *xlsx.Alignment) []*xlsx.Style
NewStyles creates styles with color and boldness
func RangeBounds ¶
RangeBounds converts a range string into boundaries: min_col, min_row, max_col, max_row. Cell coordinates will be converted into a range with the cell at both end.
Example ¶
package main import ( "fmt" "github.com/stanim/xlsxtra" ) func main() { fmt.Println(xlsxtra.RangeBounds("A1:E6")) fmt.Println(xlsxtra.RangeBounds("$A$1:$E$6")) fmt.Println(xlsxtra.RangeBounds("A1")) // invalid: no column name given fmt.Println(xlsxtra.RangeBounds("11:E6")) // invalid: row zero does not exist fmt.Println(xlsxtra.RangeBounds("A0:E6")) }
Output: 1 1 5 6 <nil> 1 1 5 6 <nil> 1 1 1 1 <nil> 0 0 0 0 Invalid range "11:E6" 0 0 0 0 Invalid range "A0:E6"
func Sort ¶
Sort sheet rows according to multi column. (Note that columns are one based, not zero based to make reverse sort possible.)
Example ¶
ExampleSort demonstrates multi column sort
package main import ( "fmt" "strings" "github.com/stanim/xlsxtra" ) func main() { sheet, err := xlsxtra.OpenSheet( "xlsxtra_test.xlsx", "sort_test.go") if err != nil { fmt.Println(err) return } // multi column sort xlsxtra.Sort(sheet, 1, -1, 3, // last name -2, // first name (reverse order) 7, // test empty column 6, // ip address ) for _, row := range sheet.Rows { fmt.Println( strings.Join(xlsxtra.ToString(row.Cells), ", ")) } fmt.Println() // by header col := xlsxtra.NewCol(sheet, 1) err = xlsxtra.SortByHeaders(sheet, 1, -1, col, "-amount", // reverse order "first_name", ) if err != nil { fmt.Println(err) return } for _, row := range sheet.Rows { fmt.Println(strings.Join(xlsxtra.ToString(row.Cells), ", ")) } }
Output: id, first_name, last_name, email, gender, amount 9, Donald, Bryant, lharper8@wunderground.com, Female, 100000000 7, Donald, Bryant, dbryant6@redcross.org, Male, 3000000 10, Donald, Bryant, hmarshall9@stumbleupon.com, Male, € 9 4, Teresa, Hunter, thall3@arizona.edu, Female, 6000 5, Joshua, Hunter, jstone4@google.cn, Male, 50000 8, Jacqueline, Hunter, jfields7@dagondesign.com, Female, 20000000 2, Harry, Hunter, hhunter1@webnode.com, Male, 80 6, Rose, Spencer, rjohnson5@odnoklassniki.ru, Female, 400000 1, Jimmy, Spencer, jspencer0@cnet.com, Male, 9 3, Benjamin, Spencer, bmorgan2@unblog.fr, Male, 700 id, first_name, last_name, email, gender, amount 9, Donald, Bryant, lharper8@wunderground.com, Female, 100000000 8, Jacqueline, Hunter, jfields7@dagondesign.com, Female, 20000000 7, Donald, Bryant, dbryant6@redcross.org, Male, 3000000 6, Rose, Spencer, rjohnson5@odnoklassniki.ru, Female, 400000 5, Joshua, Hunter, jstone4@google.cn, Male, 50000 4, Teresa, Hunter, thall3@arizona.edu, Female, 6000 3, Benjamin, Spencer, bmorgan2@unblog.fr, Male, 700 2, Harry, Hunter, hhunter1@webnode.com, Male, 80 10, Donald, Bryant, hmarshall9@stumbleupon.com, Male, € 9 1, Jimmy, Spencer, jspencer0@cnet.com, Male, 9
func SortByHeaders ¶
SortByHeaders sort sheet rows by multiple column header titles. (If a header title is prefixed by "-", it will be reversed sorted.)
func SplitCoord ¶
SplitCoord splits a coordinate string into column and row. (For example "AA19" is split into "AA" & "19")
func ToString ¶
ToString converts row to string slice
Example ¶
package main import ( "fmt" "github.com/stanim/xlsxtra" "github.com/tealeg/xlsx" ) func main() { headers := []string{"Rob", "Robert", "Ken"} sheet, err := xlsx.NewFile().AddSheet("Sheet1") if err != nil { fmt.Println(err) } row := sheet.AddRow() for _, title := range headers { row.AddCell().SetString(title) } fmt.Printf("%v", xlsxtra.ToString(row.Cells)) }
Output: [Rob Robert Ken]
Types ¶
type Col ¶
Col retrieves values by header label from a row
Example ¶
package main import ( "fmt" "github.com/stanim/xlsxtra" "github.com/tealeg/xlsx" ) func main() { type Item struct { Name string Price float64 Amount int } sheet, err := xlsxtra.NewFile().AddSheet("Basket") if err != nil { fmt.Println(err) return } // column header var headers = []string{"item", "price", "amount", "total"} header := sheet.AddRow() for _, title := range headers { header.AddString(title) } style := xlsxtra.NewStyle( "00ff0000", // color &xlsx.Font{Size: 10, Name: "Arial", Bold: true}, // bold nil, // border nil, // alignment ) header.SetStyle(style) // items var items = []Item{ {"chocolate", 4.99, 2}, {"cookies", 6.45, 3}, } var row *xlsxtra.Row for i, item := range items { row = sheet.AddRow() row.AddString(item.Name) row.AddFloat("0.00", item.Price) row.AddInt(item.Amount) row.AddFormula("0.00", fmt.Sprintf("B%d*C%d", i+1, i+1)) } // column Col type col := xlsxtra.NewCol(sheet, 1) price, err := col.Float(row, "price") if err != nil { fmt.Println(err) return } fmt.Println(price) }
Output: 6.45
type File ¶
File extends xlsx.File
func (*File) SheetByIndex ¶
SheetByIndex get sheet by index from spreadsheet
func (*File) SheetByName ¶
SheetByName get sheet by name from spreadsheet
func (*File) SheetRange ¶
SheetRange returns sheet range including end sheet. Negative indices can be used.
type MultiColumnSort ¶
MultiColumnSort implements the Sort interface. It provides multi-column sort for certain rows of a sheet, which are selected by begin and end indices. If End is is -1, the last row of the sheet will be selected.
func NewMultiColumnSort ¶
func NewMultiColumnSort( sheet *Sheet, start, end int) *MultiColumnSort
NewMultiColumnSort creates a new multi column sorter.
func (*MultiColumnSort) Less ¶
func (m *MultiColumnSort) Less(i, j int) bool
Less is part of sort.Interface. It is implemented by looping along the indices until it finds a comparison that is either Less or !Less.
func (*MultiColumnSort) Sort ¶
func (m *MultiColumnSort) Sort(columns ...int)
Sort executes the multi-column sort of the rows
func (*MultiColumnSort) Swap ¶
func (m *MultiColumnSort) Swap(i, j int)
Swap is part of sort.Interface.
type Row ¶
Row of a sheet
func (*Row) AddFormula ¶
AddFormula adds a cell with formula to a row
type Sheet ¶
Sheet extends xlsx.Sheet
func OpenSheet ¶
OpenSheet open a sheet from an xlsx file. If you need to use multiple sheets from one file use the Sheets type instead.
func (*Sheet) Cell ¶
Cell returns a cell based on coordinate string.
Example ¶
package main import ( "fmt" "github.com/stanim/xlsxtra" ) func main() { file := xlsxtra.NewFile() sheet, err := file.AddSheet("Sheet") if err != nil { fmt.Println(err) return } row := sheet.AddRow() cell := row.AddCell() cell.Value = "I am a cell!" cell, err = sheet.Cell("A1") if err != nil { fmt.Println(err) return } fmt.Println(cell.Value) }
Output: I am a cell!
func (*Sheet) CellRange ¶
CellRange returns all cells by row
Example ¶
package main import ( "fmt" "github.com/stanim/xlsxtra" "github.com/tealeg/xlsx" ) func main() { var print = func(cells [][]*xlsx.Cell) { for _, r := range cells { fmt.Printf("|") for _, c := range r { fmt.Printf("%s|", c.Value) } fmt.Println() } } file := xlsxtra.NewFile() sheet, err := file.AddSheet("Sheet") if err != nil { fmt.Println(err) return } data := [][]string{ {"A1", "B1"}, {"A2", "B2"}, } for _, r := range data { row := sheet.AddRow() for _, c := range r { row.AddString(c) } } cells, err := sheet.CellRange("A1:B2") if err != nil { fmt.Println(err) return } print(cells) fmt.Println() print(xlsxtra.Transpose(cells)) }
Output: |A1|B1| |A2|B2| |A1|A2| |B1|B2|