- 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)
}
// Insert a picture.
err = xlsx.AddPicture("Sheet1", "A2", "./image1.gif", "")
err = xlsx.AddPicture("Sheet1", "A2", "./image1.png", "")
if err != nil {
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))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1
yAxis := titleToNumber(col)
yAxis := TitleToNumber(col)
rows := xAxis + 1
cell := yAxis + 1
@ -178,12 +178,12 @@ func (f *File) MergeCell(sheet, hcell, vcell string) {
hcol := string(strings.Map(letterOnlyMapF, hcell))
hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
hyAxis := hrow - 1
hxAxis := titleToNumber(hcol)
hxAxis := TitleToNumber(hcol)
vcol := string(strings.Map(letterOnlyMapF, vcell))
vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
vyAxis := vrow - 1
vxAxis := titleToNumber(vcol)
vxAxis := TitleToNumber(vcol)
if vxAxis < hxAxis {
hcell, vcell = vcell, hcell
@ -199,7 +199,7 @@ func (f *File) MergeCell(sheet, hcell, vcell string) {
if xlsx.MergeCells != nil {
mergeCell := xlsxMergeCell{}
// 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.
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) {
@ -212,7 +212,7 @@ func (f *File) MergeCell(sheet, hcell, vcell string) {
} else {
mergeCell := xlsxMergeCell{}
// 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.Cells = append(mergeCells.Cells, &mergeCell)
xlsx.MergeCells = &mergeCells
@ -227,18 +227,18 @@ func checkCellInArea(cell, area string) bool {
col := string(strings.Map(letterOnlyMapF, cell))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
xAxis := row - 1
yAxis := titleToNumber(col)
yAxis := TitleToNumber(col)
ref := strings.Split(area, ":")
hCol := string(strings.Map(letterOnlyMapF, ref[0]))
hRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, ref[0]))
hyAxis := hRow - 1
hxAxis := titleToNumber(hCol)
hxAxis := TitleToNumber(hCol)
vCol := string(strings.Map(letterOnlyMapF, ref[1]))
vRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, ref[1]))
vyAxis := vRow - 1
vxAxis := titleToNumber(vCol)
vxAxis := TitleToNumber(vCol)
if hxAxis <= yAxis && yAxis <= vxAxis && hyAxis <= xAxis && xAxis <= vyAxis {
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))
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
row := fromRow - 1
col := titleToNumber(fromCol)
col := TitleToNumber(fromCol)
width = int(float64(width) * formatSet.XScale)
height = int(float64(height) * formatSet.YScale)
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 {
xlsx := f.workSheetReader(sheet)
col := titleToNumber(strings.ToUpper(column)) + 1
col := TitleToNumber(strings.ToUpper(column)) + 1
visible := true
if xlsx.Cols == nil {
return visible
@ -41,7 +41,7 @@ func (f *File) GetColVisible(sheet, column string) bool {
//
func (f *File) SetColVisible(sheet, column string, visible bool) {
xlsx := f.workSheetReader(sheet)
c := titleToNumber(strings.ToUpper(column)) + 1
c := TitleToNumber(strings.ToUpper(column)) + 1
col := xlsxCol{
Min: 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) {
min := titleToNumber(strings.ToUpper(startcol)) + 1
max := titleToNumber(strings.ToUpper(endcol)) + 1
min := TitleToNumber(strings.ToUpper(startcol)) + 1
max := TitleToNumber(strings.ToUpper(endcol)) + 1
if min > max {
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))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
xAxis := row - 1
yAxis := titleToNumber(col)
yAxis := TitleToNumber(col)
vml := vmlDrawing{
XMLNSv: "urn:schemas-microsoft-com:vml",
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))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1
yAxis := titleToNumber(col)
yAxis := TitleToNumber(col)
rows := xAxis + 1
cell := yAxis + 1
@ -173,7 +173,7 @@ func (f *File) SetCellInt(sheet, axis string, value int) {
col := string(strings.Map(letterOnlyMapF, axis))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1
yAxis := titleToNumber(col)
yAxis := TitleToNumber(col)
rows := xAxis + 1
cell := yAxis + 1
@ -211,7 +211,7 @@ func (f *File) SetCellStr(sheet, axis, value string) {
col := string(strings.Map(letterOnlyMapF, axis))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1
yAxis := titleToNumber(col)
yAxis := TitleToNumber(col)
rows := xAxis + 1
cell := yAxis + 1
@ -242,7 +242,7 @@ func (f *File) SetCellDefault(sheet, axis, value string) {
col := string(strings.Map(letterOnlyMapF, axis))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1
yAxis := titleToNumber(col)
yAxis := TitleToNumber(col)
rows := xAxis + 1
cell := yAxis + 1
@ -269,7 +269,7 @@ func completeCol(xlsx *xlsxWorksheet, row, cell int) {
if len(v.C) < cell {
start := len(v.C)
for iii := start; iii < cell; iii++ {
buffer.WriteString(ToAlphaString(iii + 1))
buffer.WriteString(ToAlphaString(iii))
buffer.WriteString(strconv.Itoa(k + 1))
xlsx.SheetData.Row[k].C = append(xlsx.SheetData.Row[k].C, xlsxC{
R: buffer.String(),
@ -301,7 +301,7 @@ func completeRow(xlsx *xlsxWorksheet, row, cell int) {
start := len(xlsx.SheetData.Row[ii].C)
if start == 0 {
for iii := start; iii < cell; iii++ {
buffer.WriteString(ToAlphaString(iii + 1))
buffer.WriteString(ToAlphaString(iii))
buffer.WriteString(strconv.Itoa(ii + 1))
xlsx.SheetData.Row[ii].C = append(xlsx.SheetData.Row[ii].C, xlsxC{
R: buffer.String(),
@ -383,13 +383,13 @@ func checkRow(xlsx *xlsxWorksheet) {
}
endR := string(strings.Map(letterOnlyMapF, 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 {
oldRow := xlsx.SheetData.Row[k].C
xlsx.SheetData.Row[k].C = xlsx.SheetData.Row[k].C[:0]
tmp := []xlsxC{}
for i := 0; i <= endCol; i++ {
buffer.WriteString(ToAlphaString(i + 1))
buffer.WriteString(ToAlphaString(i))
buffer.WriteString(strconv.Itoa(endRow))
tmp = append(tmp, xlsxC{
R: buffer.String(),
@ -398,7 +398,7 @@ func checkRow(xlsx *xlsxWorksheet) {
}
xlsx.SheetData.Row[k].C = tmp
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
}
}

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
// 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 {
if value < 0 {
return ""
}
var ans string
i := value
i := value + 1
for i > 0 {
ans = string((i-1)%26+65) + ans
i = (i - 1) / 26
@ -68,8 +68,13 @@ func ToAlphaString(value int) string {
return ans
}
// titleToNumber provides function to convert Excel sheet column title to int.
func titleToNumber(s string) int {
// TitleToNumber provides function to convert Excel sheet column title to 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
sum := 0
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))
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
row := fromRow - 1
col := titleToNumber(fromCol)
col := TitleToNumber(fromCol)
width = int(float64(width) * formatSet.XScale)
height = int(float64(height) * formatSet.YScale)
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))
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
row := fromRow - 1
col := titleToNumber(fromCol)
col := TitleToNumber(fromCol)
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)
cr := r.R - 1
for _, colCell := range r.C {
c := titleToNumber(strings.Map(letterOnlyMapF, colCell.R))
c := TitleToNumber(strings.Map(letterOnlyMapF, colCell.R))
val, _ := colCell.getValueFrom(f, d)
rows[cr][c] = val
}
@ -88,7 +88,7 @@ func (f *File) getTotalRowsCols(sheet string) (int, int) {
decoder.DecodeElement(&r, &startElement)
tr = r.R
for _, colCell := range r.C {
col := titleToNumber(strings.Map(letterOnlyMapF, colCell.R))
col := TitleToNumber(strings.Map(letterOnlyMapF, colCell.R))
if col > tc {
tc = col
}

View File

@ -276,7 +276,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
fromCol := string(strings.Map(letterOnlyMapF, cell))
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
row := fromRow - 1
col := titleToNumber(fromCol)
col := TitleToNumber(fromCol)
width := int(float64(formatSet.Width) * formatSet.Format.XScale)
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)

View File

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

View File

@ -767,12 +767,12 @@ func (f *File) setCellStyle(sheet, hcell, vcell string, styleID int) {
hcol := string(strings.Map(letterOnlyMapF, hcell))
hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
hyAxis := hrow - 1
hxAxis := titleToNumber(hcol)
hxAxis := TitleToNumber(hcol)
vcol := string(strings.Map(letterOnlyMapF, vcell))
vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
vyAxis := vrow - 1
vxAxis := titleToNumber(vcol)
vxAxis := TitleToNumber(vcol)
if vxAxis < hxAxis {
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.
hcell = ToAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1)
vcell = ToAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1)
hcell = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1)
vcell = ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
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))
hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
hyAxis := hrow - 1
hxAxis := titleToNumber(hcol)
hxAxis := TitleToNumber(hcol)
vcol := string(strings.Map(letterOnlyMapF, vcell))
vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
vyAxis := vrow - 1
vxAxis := titleToNumber(vcol)
vxAxis := TitleToNumber(vcol)
if vxAxis < hxAxis {
vxAxis, hxAxis = hxAxis, vxAxis
}
@ -108,12 +108,12 @@ func (f *File) addTable(sheet, tableXML string, hxAxis, hyAxis, vxAxis, vyAxis,
vyAxis++
}
// 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{}
idx := 0
for i := hxAxis; i <= vxAxis; i++ {
idx++
cell := ToAlphaString(i+1) + strconv.Itoa(hyAxis+1)
cell := ToAlphaString(i) + strconv.Itoa(hyAxis+1)
name := f.GetCellValue(sheet, cell)
if _, err := strconv.Atoi(name); err == nil {
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))
hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
hyAxis := hrow - 1
hxAxis := titleToNumber(hcol)
hxAxis := TitleToNumber(hcol)
vcol := string(strings.Map(letterOnlyMapF, vcell))
vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
vyAxis := vrow - 1
vxAxis := titleToNumber(vcol)
vxAxis := TitleToNumber(vcol)
if vxAxis < hxAxis {
vxAxis, hxAxis = hxAxis, vxAxis
@ -254,7 +254,7 @@ func (f *File) AutoFilter(sheet, hcell, vcell, format string) error {
if vyAxis < hyAxis {
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
err := f.autoFilter(sheet, ref, refRange, hxAxis, formatSet)
return err
@ -275,7 +275,7 @@ func (f *File) autoFilter(sheet, ref string, refRange, hxAxis int, formatSet *fo
if formatSet.Column == "" || formatSet.Expression == "" {
return nil
}
col := titleToNumber(formatSet.Column)
col := TitleToNumber(formatSet.Column)
offset := col - hxAxis
if offset < 0 || offset > refRange {
return fmt.Errorf("Incorrect index of column '%s'", formatSet.Column)