- Make function `TitleToNumber()` exportable, note that function `ToAlphaString()` return value calculation changes, get more info from go doc. Relate issue #63;

- Readme and go doc updated
This commit is contained in:
Ri Xu 2017-06-27 17:53:06 +08:00
parent 35841caaf1
commit 555e2ba9a8
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
13 changed files with 52 additions and 48 deletions

View File

@ -148,7 +148,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// Insert a picture. // Insert a picture.
err = xlsx.AddPicture("Sheet1", "A2", "./image1.gif", "") err = xlsx.AddPicture("Sheet1", "A2", "./image1.png", "")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }

16
cell.go
View File

@ -121,7 +121,7 @@ func (f *File) SetCellFormula(sheet, axis, formula string) {
col := string(strings.Map(letterOnlyMapF, axis)) col := string(strings.Map(letterOnlyMapF, axis))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis)) row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1 xAxis := row - 1
yAxis := titleToNumber(col) yAxis := TitleToNumber(col)
rows := xAxis + 1 rows := xAxis + 1
cell := yAxis + 1 cell := yAxis + 1
@ -178,12 +178,12 @@ func (f *File) MergeCell(sheet, hcell, vcell string) {
hcol := string(strings.Map(letterOnlyMapF, hcell)) hcol := string(strings.Map(letterOnlyMapF, hcell))
hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell)) hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
hyAxis := hrow - 1 hyAxis := hrow - 1
hxAxis := titleToNumber(hcol) hxAxis := TitleToNumber(hcol)
vcol := string(strings.Map(letterOnlyMapF, vcell)) vcol := string(strings.Map(letterOnlyMapF, vcell))
vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell)) vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
vyAxis := vrow - 1 vyAxis := vrow - 1
vxAxis := titleToNumber(vcol) vxAxis := TitleToNumber(vcol)
if vxAxis < hxAxis { if vxAxis < hxAxis {
hcell, vcell = vcell, hcell hcell, vcell = vcell, hcell
@ -199,7 +199,7 @@ func (f *File) MergeCell(sheet, hcell, vcell string) {
if xlsx.MergeCells != nil { if xlsx.MergeCells != nil {
mergeCell := xlsxMergeCell{} mergeCell := xlsxMergeCell{}
// Correct the coordinate area, such correct C1:B3 to B1:C3. // Correct the coordinate area, such correct C1:B3 to B1:C3.
mergeCell.Ref = ToAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1) mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
// Delete the merged cells of the overlapping area. // Delete the merged cells of the overlapping area.
for i := 0; i < len(xlsx.MergeCells.Cells); i++ { for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
if checkCellInArea(hcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0], mergeCell.Ref) { if checkCellInArea(hcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0], mergeCell.Ref) {
@ -212,7 +212,7 @@ func (f *File) MergeCell(sheet, hcell, vcell string) {
} else { } else {
mergeCell := xlsxMergeCell{} mergeCell := xlsxMergeCell{}
// Correct the coordinate area, such correct C1:B3 to B1:C3. // Correct the coordinate area, such correct C1:B3 to B1:C3.
mergeCell.Ref = ToAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1) mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
mergeCells := xlsxMergeCells{} mergeCells := xlsxMergeCells{}
mergeCells.Cells = append(mergeCells.Cells, &mergeCell) mergeCells.Cells = append(mergeCells.Cells, &mergeCell)
xlsx.MergeCells = &mergeCells xlsx.MergeCells = &mergeCells
@ -227,18 +227,18 @@ func checkCellInArea(cell, area string) bool {
col := string(strings.Map(letterOnlyMapF, cell)) col := string(strings.Map(letterOnlyMapF, cell))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell)) row, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
xAxis := row - 1 xAxis := row - 1
yAxis := titleToNumber(col) yAxis := TitleToNumber(col)
ref := strings.Split(area, ":") ref := strings.Split(area, ":")
hCol := string(strings.Map(letterOnlyMapF, ref[0])) hCol := string(strings.Map(letterOnlyMapF, ref[0]))
hRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, ref[0])) hRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, ref[0]))
hyAxis := hRow - 1 hyAxis := hRow - 1
hxAxis := titleToNumber(hCol) hxAxis := TitleToNumber(hCol)
vCol := string(strings.Map(letterOnlyMapF, ref[1])) vCol := string(strings.Map(letterOnlyMapF, ref[1]))
vRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, ref[1])) vRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, ref[1]))
vyAxis := vRow - 1 vyAxis := vRow - 1
vxAxis := titleToNumber(vCol) vxAxis := TitleToNumber(vCol)
if hxAxis <= yAxis && yAxis <= vxAxis && hyAxis <= xAxis && xAxis <= vyAxis { if hxAxis <= yAxis && yAxis <= vxAxis && hyAxis <= xAxis && xAxis <= vyAxis {
result = true result = true

View File

@ -837,7 +837,7 @@ func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rI
fromCol := string(strings.Map(letterOnlyMapF, cell)) fromCol := string(strings.Map(letterOnlyMapF, cell))
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell)) fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
row := fromRow - 1 row := fromRow - 1
col := titleToNumber(fromCol) col := TitleToNumber(fromCol)
width = int(float64(width) * formatSet.XScale) width = int(float64(width) * formatSet.XScale)
height = int(float64(height) * formatSet.YScale) height = int(float64(height) * formatSet.YScale)
colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height) colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height)

8
col.go
View File

@ -21,7 +21,7 @@ const (
// //
func (f *File) GetColVisible(sheet, column string) bool { func (f *File) GetColVisible(sheet, column string) bool {
xlsx := f.workSheetReader(sheet) xlsx := f.workSheetReader(sheet)
col := titleToNumber(strings.ToUpper(column)) + 1 col := TitleToNumber(strings.ToUpper(column)) + 1
visible := true visible := true
if xlsx.Cols == nil { if xlsx.Cols == nil {
return visible return visible
@ -41,7 +41,7 @@ func (f *File) GetColVisible(sheet, column string) bool {
// //
func (f *File) SetColVisible(sheet, column string, visible bool) { func (f *File) SetColVisible(sheet, column string, visible bool) {
xlsx := f.workSheetReader(sheet) xlsx := f.workSheetReader(sheet)
c := titleToNumber(strings.ToUpper(column)) + 1 c := TitleToNumber(strings.ToUpper(column)) + 1
col := xlsxCol{ col := xlsxCol{
Min: c, Min: c,
Max: c, Max: c,
@ -78,8 +78,8 @@ func (f *File) SetColVisible(sheet, column string, visible bool) {
// } // }
// //
func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) { func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) {
min := titleToNumber(strings.ToUpper(startcol)) + 1 min := TitleToNumber(strings.ToUpper(startcol)) + 1
max := titleToNumber(strings.ToUpper(endcol)) + 1 max := TitleToNumber(strings.ToUpper(endcol)) + 1
if min > max { if min > max {
min, max = max, min min, max = max, min
} }

View File

@ -56,7 +56,7 @@ func (f *File) addDrawingVML(commentID int, drawingVML, cell string) {
col := string(strings.Map(letterOnlyMapF, cell)) col := string(strings.Map(letterOnlyMapF, cell))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell)) row, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
xAxis := row - 1 xAxis := row - 1
yAxis := titleToNumber(col) yAxis := TitleToNumber(col)
vml := vmlDrawing{ vml := vmlDrawing{
XMLNSv: "urn:schemas-microsoft-com:vml", XMLNSv: "urn:schemas-microsoft-com:vml",
XMLNSo: "urn:schemas-microsoft-com:office:office", XMLNSo: "urn:schemas-microsoft-com:office:office",

View File

@ -122,7 +122,7 @@ func (f *File) getCellStyle(sheet, axis string) int {
col := string(strings.Map(letterOnlyMapF, axis)) col := string(strings.Map(letterOnlyMapF, axis))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis)) row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1 xAxis := row - 1
yAxis := titleToNumber(col) yAxis := TitleToNumber(col)
rows := xAxis + 1 rows := xAxis + 1
cell := yAxis + 1 cell := yAxis + 1
@ -173,7 +173,7 @@ func (f *File) SetCellInt(sheet, axis string, value int) {
col := string(strings.Map(letterOnlyMapF, axis)) col := string(strings.Map(letterOnlyMapF, axis))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis)) row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1 xAxis := row - 1
yAxis := titleToNumber(col) yAxis := TitleToNumber(col)
rows := xAxis + 1 rows := xAxis + 1
cell := yAxis + 1 cell := yAxis + 1
@ -211,7 +211,7 @@ func (f *File) SetCellStr(sheet, axis, value string) {
col := string(strings.Map(letterOnlyMapF, axis)) col := string(strings.Map(letterOnlyMapF, axis))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis)) row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1 xAxis := row - 1
yAxis := titleToNumber(col) yAxis := TitleToNumber(col)
rows := xAxis + 1 rows := xAxis + 1
cell := yAxis + 1 cell := yAxis + 1
@ -242,7 +242,7 @@ func (f *File) SetCellDefault(sheet, axis, value string) {
col := string(strings.Map(letterOnlyMapF, axis)) col := string(strings.Map(letterOnlyMapF, axis))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis)) row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1 xAxis := row - 1
yAxis := titleToNumber(col) yAxis := TitleToNumber(col)
rows := xAxis + 1 rows := xAxis + 1
cell := yAxis + 1 cell := yAxis + 1
@ -269,7 +269,7 @@ func completeCol(xlsx *xlsxWorksheet, row, cell int) {
if len(v.C) < cell { if len(v.C) < cell {
start := len(v.C) start := len(v.C)
for iii := start; iii < cell; iii++ { for iii := start; iii < cell; iii++ {
buffer.WriteString(ToAlphaString(iii + 1)) buffer.WriteString(ToAlphaString(iii))
buffer.WriteString(strconv.Itoa(k + 1)) buffer.WriteString(strconv.Itoa(k + 1))
xlsx.SheetData.Row[k].C = append(xlsx.SheetData.Row[k].C, xlsxC{ xlsx.SheetData.Row[k].C = append(xlsx.SheetData.Row[k].C, xlsxC{
R: buffer.String(), R: buffer.String(),
@ -301,7 +301,7 @@ func completeRow(xlsx *xlsxWorksheet, row, cell int) {
start := len(xlsx.SheetData.Row[ii].C) start := len(xlsx.SheetData.Row[ii].C)
if start == 0 { if start == 0 {
for iii := start; iii < cell; iii++ { for iii := start; iii < cell; iii++ {
buffer.WriteString(ToAlphaString(iii + 1)) buffer.WriteString(ToAlphaString(iii))
buffer.WriteString(strconv.Itoa(ii + 1)) buffer.WriteString(strconv.Itoa(ii + 1))
xlsx.SheetData.Row[ii].C = append(xlsx.SheetData.Row[ii].C, xlsxC{ xlsx.SheetData.Row[ii].C = append(xlsx.SheetData.Row[ii].C, xlsxC{
R: buffer.String(), R: buffer.String(),
@ -383,13 +383,13 @@ func checkRow(xlsx *xlsxWorksheet) {
} }
endR := string(strings.Map(letterOnlyMapF, v.C[lenCol-1].R)) endR := string(strings.Map(letterOnlyMapF, v.C[lenCol-1].R))
endRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, v.C[lenCol-1].R)) endRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, v.C[lenCol-1].R))
endCol := titleToNumber(endR) + 1 endCol := TitleToNumber(endR) + 1
if lenCol < endCol { if lenCol < endCol {
oldRow := xlsx.SheetData.Row[k].C oldRow := xlsx.SheetData.Row[k].C
xlsx.SheetData.Row[k].C = xlsx.SheetData.Row[k].C[:0] xlsx.SheetData.Row[k].C = xlsx.SheetData.Row[k].C[:0]
tmp := []xlsxC{} tmp := []xlsxC{}
for i := 0; i <= endCol; i++ { for i := 0; i <= endCol; i++ {
buffer.WriteString(ToAlphaString(i + 1)) buffer.WriteString(ToAlphaString(i))
buffer.WriteString(strconv.Itoa(endRow)) buffer.WriteString(strconv.Itoa(endRow))
tmp = append(tmp, xlsxC{ tmp = append(tmp, xlsxC{
R: buffer.String(), R: buffer.String(),
@ -398,7 +398,7 @@ func checkRow(xlsx *xlsxWorksheet) {
} }
xlsx.SheetData.Row[k].C = tmp xlsx.SheetData.Row[k].C = tmp
for _, y := range oldRow { for _, y := range oldRow {
colAxis := titleToNumber(string(strings.Map(letterOnlyMapF, y.R))) colAxis := TitleToNumber(string(strings.Map(letterOnlyMapF, y.R)))
xlsx.SheetData.Row[k].C[colAxis] = y xlsx.SheetData.Row[k].C[colAxis] = y
} }
} }

15
lib.go
View File

@ -51,16 +51,16 @@ func readFile(file *zip.File) string {
} }
// ToAlphaString provides function to convert integer to Excel sheet column // ToAlphaString provides function to convert integer to Excel sheet column
// title. For example convert 37 to column title AK: // title. For example convert 36 to column title AK:
// //
// excelize.ToAlphaString(37) // excelize.ToAlphaString(36)
// //
func ToAlphaString(value int) string { func ToAlphaString(value int) string {
if value < 0 { if value < 0 {
return "" return ""
} }
var ans string var ans string
i := value i := value + 1
for i > 0 { for i > 0 {
ans = string((i-1)%26+65) + ans ans = string((i-1)%26+65) + ans
i = (i - 1) / 26 i = (i - 1) / 26
@ -68,8 +68,13 @@ func ToAlphaString(value int) string {
return ans return ans
} }
// titleToNumber provides function to convert Excel sheet column title to int. // TitleToNumber provides function to convert Excel sheet column title to int
func titleToNumber(s string) int { // (this function doesn't do value check currently). For example convert AK to
// column title 36:
//
// excelize.TitleToNumber("AK")
//
func TitleToNumber(s string) int {
weight := 0.0 weight := 0.0
sum := 0 sum := 0
for i := len(s) - 1; i >= 0; i-- { for i := len(s) - 1; i >= 0; i-- {

View File

@ -174,7 +174,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he
fromCol := string(strings.Map(letterOnlyMapF, cell)) fromCol := string(strings.Map(letterOnlyMapF, cell))
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell)) fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
row := fromRow - 1 row := fromRow - 1
col := titleToNumber(fromCol) col := TitleToNumber(fromCol)
width = int(float64(width) * formatSet.XScale) width = int(float64(width) * formatSet.XScale)
height = int(float64(height) * formatSet.YScale) height = int(float64(height) * formatSet.YScale)
colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height) colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height)
@ -391,7 +391,7 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte) {
fromCol := string(strings.Map(letterOnlyMapF, cell)) fromCol := string(strings.Map(letterOnlyMapF, cell))
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell)) fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
row := fromRow - 1 row := fromRow - 1
col := titleToNumber(fromCol) col := TitleToNumber(fromCol)
drawingRelationships := strings.Replace(strings.Replace(target, "../drawings", "xl/drawings/_rels", -1), ".xml", ".xml.rels", -1) drawingRelationships := strings.Replace(strings.Replace(target, "../drawings", "xl/drawings/_rels", -1), ".xml", ".xml.rels", -1)

View File

@ -56,7 +56,7 @@ func (f *File) GetRows(sheet string) [][]string {
decoder.DecodeElement(&r, &startElement) decoder.DecodeElement(&r, &startElement)
cr := r.R - 1 cr := r.R - 1
for _, colCell := range r.C { for _, colCell := range r.C {
c := titleToNumber(strings.Map(letterOnlyMapF, colCell.R)) c := TitleToNumber(strings.Map(letterOnlyMapF, colCell.R))
val, _ := colCell.getValueFrom(f, d) val, _ := colCell.getValueFrom(f, d)
rows[cr][c] = val rows[cr][c] = val
} }
@ -88,7 +88,7 @@ func (f *File) getTotalRowsCols(sheet string) (int, int) {
decoder.DecodeElement(&r, &startElement) decoder.DecodeElement(&r, &startElement)
tr = r.R tr = r.R
for _, colCell := range r.C { for _, colCell := range r.C {
col := titleToNumber(strings.Map(letterOnlyMapF, colCell.R)) col := TitleToNumber(strings.Map(letterOnlyMapF, colCell.R))
if col > tc { if col > tc {
tc = col tc = col
} }

View File

@ -276,7 +276,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
fromCol := string(strings.Map(letterOnlyMapF, cell)) fromCol := string(strings.Map(letterOnlyMapF, cell))
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell)) fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
row := fromRow - 1 row := fromRow - 1
col := titleToNumber(fromCol) col := TitleToNumber(fromCol)
width := int(float64(formatSet.Width) * formatSet.Format.XScale) width := int(float64(formatSet.Width) * formatSet.Format.XScale)
height := int(float64(formatSet.Height) * formatSet.Format.YScale) height := int(float64(formatSet.Height) * formatSet.Format.YScale)
colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, formatSet.Format.OffsetX, formatSet.Format.OffsetY, width, height) colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, col, row, formatSet.Format.OffsetX, formatSet.Format.OffsetY, width, height)

View File

@ -438,8 +438,7 @@ func (f *File) copySheet(from, to int) {
// SetSheetVisible provides function to set worksheet visible by given worksheet // SetSheetVisible provides function to set worksheet visible by given worksheet
// name. A workbook must contain at least one visible worksheet. If the given // name. A workbook must contain at least one visible worksheet. If the given
// worksheet has been activated, this setting will be invalidated. Sheet state // worksheet has been activated, this setting will be invalidated. Sheet state
// values as defined by http://msdn.microsoft.com/en- // values as defined by http://msdn.microsoft.com/en-us/library/office/documentformat.openxml.spreadsheet.sheetstatevalues.aspx
// us/library/office/documentformat.openxml.spreadsheet.sheetstatevalues.aspx
// //
// visible // visible
// hidden // hidden

View File

@ -767,12 +767,12 @@ func (f *File) setCellStyle(sheet, hcell, vcell string, styleID int) {
hcol := string(strings.Map(letterOnlyMapF, hcell)) hcol := string(strings.Map(letterOnlyMapF, hcell))
hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell)) hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
hyAxis := hrow - 1 hyAxis := hrow - 1
hxAxis := titleToNumber(hcol) hxAxis := TitleToNumber(hcol)
vcol := string(strings.Map(letterOnlyMapF, vcell)) vcol := string(strings.Map(letterOnlyMapF, vcell))
vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell)) vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
vyAxis := vrow - 1 vyAxis := vrow - 1
vxAxis := titleToNumber(vcol) vxAxis := TitleToNumber(vcol)
if vxAxis < hxAxis { if vxAxis < hxAxis {
hcell, vcell = vcell, hcell hcell, vcell = vcell, hcell
@ -785,8 +785,8 @@ func (f *File) setCellStyle(sheet, hcell, vcell string, styleID int) {
} }
// Correct the coordinate area, such correct C1:B3 to B1:C3. // Correct the coordinate area, such correct C1:B3 to B1:C3.
hcell = ToAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) hcell = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1)
vcell = ToAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1) vcell = ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
xlsx := f.workSheetReader(sheet) xlsx := f.workSheetReader(sheet)

View File

@ -47,12 +47,12 @@ func (f *File) AddTable(sheet, hcell, vcell, format string) {
hcol := string(strings.Map(letterOnlyMapF, hcell)) hcol := string(strings.Map(letterOnlyMapF, hcell))
hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell)) hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
hyAxis := hrow - 1 hyAxis := hrow - 1
hxAxis := titleToNumber(hcol) hxAxis := TitleToNumber(hcol)
vcol := string(strings.Map(letterOnlyMapF, vcell)) vcol := string(strings.Map(letterOnlyMapF, vcell))
vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell)) vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
vyAxis := vrow - 1 vyAxis := vrow - 1
vxAxis := titleToNumber(vcol) vxAxis := TitleToNumber(vcol)
if vxAxis < hxAxis { if vxAxis < hxAxis {
vxAxis, hxAxis = hxAxis, vxAxis vxAxis, hxAxis = hxAxis, vxAxis
} }
@ -108,12 +108,12 @@ func (f *File) addTable(sheet, tableXML string, hxAxis, hyAxis, vxAxis, vyAxis,
vyAxis++ vyAxis++
} }
// Correct table reference coordinate area, such correct C1:B3 to B1:C3. // Correct table reference coordinate area, such correct C1:B3 to B1:C3.
ref := ToAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1) ref := ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
tableColumn := []*xlsxTableColumn{} tableColumn := []*xlsxTableColumn{}
idx := 0 idx := 0
for i := hxAxis; i <= vxAxis; i++ { for i := hxAxis; i <= vxAxis; i++ {
idx++ idx++
cell := ToAlphaString(i+1) + strconv.Itoa(hyAxis+1) cell := ToAlphaString(i) + strconv.Itoa(hyAxis+1)
name := f.GetCellValue(sheet, cell) name := f.GetCellValue(sheet, cell)
if _, err := strconv.Atoi(name); err == nil { if _, err := strconv.Atoi(name); err == nil {
f.SetCellStr(sheet, cell, name) f.SetCellStr(sheet, cell, name)
@ -240,12 +240,12 @@ func (f *File) AutoFilter(sheet, hcell, vcell, format string) error {
hcol := string(strings.Map(letterOnlyMapF, hcell)) hcol := string(strings.Map(letterOnlyMapF, hcell))
hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell)) hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
hyAxis := hrow - 1 hyAxis := hrow - 1
hxAxis := titleToNumber(hcol) hxAxis := TitleToNumber(hcol)
vcol := string(strings.Map(letterOnlyMapF, vcell)) vcol := string(strings.Map(letterOnlyMapF, vcell))
vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell)) vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
vyAxis := vrow - 1 vyAxis := vrow - 1
vxAxis := titleToNumber(vcol) vxAxis := TitleToNumber(vcol)
if vxAxis < hxAxis { if vxAxis < hxAxis {
vxAxis, hxAxis = hxAxis, vxAxis vxAxis, hxAxis = hxAxis, vxAxis
@ -254,7 +254,7 @@ func (f *File) AutoFilter(sheet, hcell, vcell, format string) error {
if vyAxis < hyAxis { if vyAxis < hyAxis {
vyAxis, hyAxis = hyAxis, vyAxis vyAxis, hyAxis = hyAxis, vyAxis
} }
ref := ToAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1) ref := ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
refRange := vxAxis - hxAxis refRange := vxAxis - hxAxis
err := f.autoFilter(sheet, ref, refRange, hxAxis, formatSet) err := f.autoFilter(sheet, ref, refRange, hxAxis, formatSet)
return err return err
@ -275,7 +275,7 @@ func (f *File) autoFilter(sheet, ref string, refRange, hxAxis int, formatSet *fo
if formatSet.Column == "" || formatSet.Expression == "" { if formatSet.Column == "" || formatSet.Expression == "" {
return nil return nil
} }
col := titleToNumber(formatSet.Column) col := TitleToNumber(formatSet.Column)
offset := col - hxAxis offset := col - hxAxis
if offset < 0 || offset > refRange { if offset < 0 || offset > refRange {
return fmt.Errorf("Incorrect index of column '%s'", formatSet.Column) return fmt.Errorf("Incorrect index of column '%s'", formatSet.Column)