From 555e2ba9a82d6974077681c7ab34ce0fa93d351d Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Tue, 27 Jun 2017 17:53:06 +0800 Subject: [PATCH] - 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 --- README.md | 2 +- cell.go | 16 ++++++++-------- chart.go | 2 +- col.go | 8 ++++---- comment.go | 2 +- excelize.go | 18 +++++++++--------- lib.go | 15 ++++++++++----- picture.go | 4 ++-- rows.go | 4 ++-- shape.go | 2 +- sheet.go | 3 +-- styles.go | 8 ++++---- table.go | 16 ++++++++-------- 13 files changed, 52 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 2ab87e1..2cefd0c 100644 --- a/README.md +++ b/README.md @@ -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) } diff --git a/cell.go b/cell.go index 0145b8e..a8f6861 100644 --- a/cell.go +++ b/cell.go @@ -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 diff --git a/chart.go b/chart.go index 09cbbad..f83e0a7 100644 --- a/chart.go +++ b/chart.go @@ -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) diff --git a/col.go b/col.go index 66e28b8..2081e40 100644 --- a/col.go +++ b/col.go @@ -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 } diff --git a/comment.go b/comment.go index 4010942..3f57c84 100644 --- a/comment.go +++ b/comment.go @@ -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", diff --git a/excelize.go b/excelize.go index 2554565..f8b938c 100644 --- a/excelize.go +++ b/excelize.go @@ -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 } } diff --git a/lib.go b/lib.go index 6058758..5649f65 100644 --- a/lib.go +++ b/lib.go @@ -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-- { diff --git a/picture.go b/picture.go index a61c1e2..da9bb4e 100644 --- a/picture.go +++ b/picture.go @@ -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) diff --git a/rows.go b/rows.go index f594092..1d94a6c 100644 --- a/rows.go +++ b/rows.go @@ -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 } diff --git a/shape.go b/shape.go index 99ab291..f93cce9 100644 --- a/shape.go +++ b/shape.go @@ -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) diff --git a/sheet.go b/sheet.go index a951680..24e4d75 100644 --- a/sheet.go +++ b/sheet.go @@ -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 diff --git a/styles.go b/styles.go index 04774c2..6db97c1 100644 --- a/styles.go +++ b/styles.go @@ -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) diff --git a/table.go b/table.go index 5c8325b..1417cd9 100644 --- a/table.go +++ b/table.go @@ -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)