Procházet zdrojové kódy

Make Column letter functions public

Ryan Hollis před 8 roky
rodič
revize
4ac5c0795c
3 změnil soubory, kde provedl 16 přidání a 17 odebrání
  1. 7 7
      lib.go
  2. 2 2
      lib_test.go
  3. 7 8
      sheet.go

+ 7 - 7
lib.go

@@ -25,7 +25,7 @@ func (e *XLSXReaderError) Error() string {
 
 
 // getRangeFromString is an internal helper function that converts
 // getRangeFromString is an internal helper function that converts
 // XLSX internal range syntax to a pair of integers.  For example,
 // XLSX internal range syntax to a pair of integers.  For example,
-// the range string "1:3" yield the upper and lower intergers 1 and 3.
+// the range string "1:3" yield the upper and lower integers 1 and 3.
 func getRangeFromString(rangeString string) (lower int, upper int, error error) {
 func getRangeFromString(rangeString string) (lower int, upper int, error error) {
 	var parts []string
 	var parts []string
 	parts = strings.SplitN(rangeString, ":", 2)
 	parts = strings.SplitN(rangeString, ":", 2)
@@ -46,9 +46,9 @@ func getRangeFromString(rangeString string) (lower int, upper int, error error)
 	return lower, upper, error
 	return lower, upper, error
 }
 }
 
 
-// lettersToNumeric is used to convert a character based column
+// ColLettersToIndex is used to convert a character based column
 // reference to a zero based numeric column identifier.
 // reference to a zero based numeric column identifier.
-func lettersToNumeric(letters string) int {
+func ColLettersToIndex(letters string) int {
 	sum, mul, n := 0, 1, 0
 	sum, mul, n := 0, 1, 0
 	for i := len(letters) - 1; i >= 0; i, mul, n = i-1, mul*26, 1 {
 	for i := len(letters) - 1; i >= 0; i, mul, n = i-1, mul*26, 1 {
 		c := letters[i]
 		c := letters[i]
@@ -134,9 +134,9 @@ func intToBase26(x int) (parts []int) {
 	return parts
 	return parts
 }
 }
 
 
-// numericToLetters is used to convert a zero based, numeric column
+// ColIndexToLetters is used to convert a zero based, numeric column
 // indentifier into a character code.
 // indentifier into a character code.
-func numericToLetters(colRef int) string {
+func ColIndexToLetters(colRef int) string {
 	parts := intToBase26(colRef)
 	parts := intToBase26(colRef)
 	return formatColumnName(smooshBase26Slice(parts))
 	return formatColumnName(smooshBase26Slice(parts))
 }
 }
@@ -172,14 +172,14 @@ func GetCoordsFromCellIDString(cellIDString string) (x, y int, error error) {
 		return x, y, error
 		return x, y, error
 	}
 	}
 	y -= 1 // Zero based
 	y -= 1 // Zero based
-	x = lettersToNumeric(letterPart)
+	x = ColLettersToIndex(letterPart)
 	return x, y, error
 	return x, y, error
 }
 }
 
 
 // GetCellIDStringFromCoords returns the Excel format cell name that
 // GetCellIDStringFromCoords returns the Excel format cell name that
 // represents a pair of zero based cartesian coordinates.
 // represents a pair of zero based cartesian coordinates.
 func GetCellIDStringFromCoords(x, y int) string {
 func GetCellIDStringFromCoords(x, y int) string {
-	letterPart := numericToLetters(x)
+	letterPart := ColIndexToLetters(x)
 	numericPart := y + 1
 	numericPart := y + 1
 	return fmt.Sprintf("%s%d", letterPart, numericPart)
 	return fmt.Sprintf("%s%d", letterPart, numericPart)
 }
 }

+ 2 - 2
lib_test.go

@@ -141,7 +141,7 @@ func (l *LibSuite) TestLettersToNumeric(c *C) {
 		"BA": 52, "BZ": 77, "ZA": 26*26 + 0, "ZZ": 26*26 + 25,
 		"BA": 52, "BZ": 77, "ZA": 26*26 + 0, "ZZ": 26*26 + 25,
 		"AAA": 26*26 + 26 + 0, "AMI": 1022}
 		"AAA": 26*26 + 26 + 0, "AMI": 1022}
 	for input, ans := range cases {
 	for input, ans := range cases {
-		output := lettersToNumeric(input)
+		output := ColLettersToIndex(input)
 		c.Assert(output, Equals, ans)
 		c.Assert(output, Equals, ans)
 	}
 	}
 }
 }
@@ -158,7 +158,7 @@ func (l *LibSuite) TestNumericToLetters(c *C) {
 		"ZZ":  26*26 + 25,
 		"ZZ":  26*26 + 25,
 		"AAA": 26*26 + 26 + 0, "AMI": 1022}
 		"AAA": 26*26 + 26 + 0, "AMI": 1022}
 	for ans, input := range cases {
 	for ans, input := range cases {
-		output := numericToLetters(input)
+		output := ColIndexToLetters(input)
 		c.Assert(output, Equals, ans)
 		c.Assert(output, Equals, ans)
 	}
 	}
 
 

+ 7 - 8
sheet.go

@@ -129,7 +129,7 @@ func (s *Sheet) handleMerged() {
 	for r, row := range s.Rows {
 	for r, row := range s.Rows {
 		for c, cell := range row.Cells {
 		for c, cell := range row.Cells {
 			if cell.HMerge > 0 || cell.VMerge > 0 {
 			if cell.HMerge > 0 || cell.VMerge > 0 {
-				coord := fmt.Sprintf("%s%d", numericToLetters(c), r+1)
+				coord := GetCellIDStringFromCoords(c, r)
 				merged[coord] = cell
 				merged[coord] = cell
 			}
 			}
 		}
 		}
@@ -288,7 +288,7 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 				maxCell = c
 				maxCell = c
 			}
 			}
 			xC := xlsxC{}
 			xC := xlsxC{}
-			xC.R = fmt.Sprintf("%s%d", numericToLetters(c), r+1)
+			xC.R = GetCellIDStringFromCoords(c, r)
 			switch cell.cellType {
 			switch cell.cellType {
 			case CellTypeString:
 			case CellTypeString:
 				if len(cell.Value) > 0 {
 				if len(cell.Value) > 0 {
@@ -325,10 +325,10 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 			if cell.HMerge > 0 || cell.VMerge > 0 {
 			if cell.HMerge > 0 || cell.VMerge > 0 {
 				// r == rownum, c == colnum
 				// r == rownum, c == colnum
 				mc := xlsxMergeCell{}
 				mc := xlsxMergeCell{}
-				start := fmt.Sprintf("%s%d", numericToLetters(c), r+1)
-				endcol := c + cell.HMerge
-				endrow := r + cell.VMerge + 1
-				end := fmt.Sprintf("%s%d", numericToLetters(endcol), endrow)
+				start := GetCellIDStringFromCoords(c, r)
+				endCol := c + cell.HMerge
+				endRow := r + cell.VMerge
+				end := GetCellIDStringFromCoords(endCol, endRow)
 				mc.Ref = start + ":" + end
 				mc.Ref = start + ":" + end
 				if worksheet.MergeCells == nil {
 				if worksheet.MergeCells == nil {
 					worksheet.MergeCells = &xlsxMergeCells{}
 					worksheet.MergeCells = &xlsxMergeCells{}
@@ -356,8 +356,7 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 
 
 	worksheet.SheetData = xSheet
 	worksheet.SheetData = xSheet
 	dimension := xlsxDimension{}
 	dimension := xlsxDimension{}
-	dimension.Ref = fmt.Sprintf("A1:%s%d",
-		numericToLetters(maxCell), maxRow+1)
+	dimension.Ref = "A1:" + GetCellIDStringFromCoords(maxCell, maxRow)
 	if dimension.Ref == "A1:A1" {
 	if dimension.Ref == "A1:A1" {
 		dimension.Ref = "A1"
 		dimension.Ref = "A1"
 	}
 	}