From dc01264562e6e88d77a28042408029770ea32df4 Mon Sep 17 00:00:00 2001 From: Veniamin Albaev Date: Tue, 19 Mar 2019 19:14:41 +0300 Subject: Huge refactorig for consistent col/row numbering (#356) * Huge refactorig for consistent col/row numbering Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes. But have to go deeper, do fixes, after do related fixes and again and again. Major improvements: 1. Tests made stronger again (But still be weak). 2. "Empty" returns for incorrect input replaces with panic. 3. Check for correct col/row/cell naming & addressing by default. 4. Removed huge amount of duplicated code. 5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all, and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName(). 6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc). * Formatting fixes --- styles.go | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) (limited to 'styles.go') diff --git a/styles.go b/styles.go index 7ffc8ff..50b30b8 100644 --- a/styles.go +++ b/styles.go @@ -2263,6 +2263,14 @@ func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, a return style.CellXfs.Count - 1 } +// GetCellStyle provides a function to get cell style index by given worksheet +// name and cell coordinates. +func (f *File) GetCellStyle(sheet, axis string) int { + xlsx := f.workSheetReader(sheet) + cellData, col, _ := f.prepareCell(xlsx, sheet, axis) + return f.prepareCellStyle(xlsx, col, cellData.S) +} + // SetCellStyle provides a function to add style attribute for cells by given // worksheet name, coordinate area and style ID. Note that diagonalDown and // diagonalUp type border should be use same color in the same coordinate @@ -2329,42 +2337,36 @@ func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, a // xlsx.SetCellStyle("Sheet1", "H9", "H9", style) // func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) { - hcell = strings.ToUpper(hcell) - vcell = strings.ToUpper(vcell) - - // Coordinate conversion, convert C1:B3 to 2,0,1,2. - hcol := string(strings.Map(letterOnlyMapF, hcell)) - hrow, err := strconv.Atoi(strings.Map(intOnlyMapF, hcell)) + hcol, hrow, err := CellNameToCoordinates(hcell) if err != nil { - return + panic(err) } - hyAxis := hrow - 1 - hxAxis := TitleToNumber(hcol) - vcol := string(strings.Map(letterOnlyMapF, vcell)) - vrow, err := strconv.Atoi(strings.Map(intOnlyMapF, vcell)) + vcol, vrow, err := CellNameToCoordinates(vcell) if err != nil { - return + panic(err) } - vyAxis := vrow - 1 - vxAxis := TitleToNumber(vcol) - // Correct the coordinate area, such correct C1:B3 to B1:C3. - if vxAxis < hxAxis { - vxAxis, hxAxis = hxAxis, vxAxis + // Normalize the coordinate area, such correct C1:B3 to B1:C3. + if vcol < hcol { + vcol, hcol = hcol, vcol } - if vyAxis < hyAxis { - vyAxis, hyAxis = hyAxis, vyAxis + if vrow < hrow { + vrow, hrow = hrow, vrow } - xlsx := f.workSheetReader(sheet) + hcolIdx := hcol - 1 + hrowIdx := hrow - 1 + + vcolIdx := vcol - 1 + vrowIdx := vrow - 1 - completeRow(xlsx, vyAxis+1, vxAxis+1) - completeCol(xlsx, vyAxis+1, vxAxis+1) + xlsx := f.workSheetReader(sheet) + prepareSheetXML(xlsx, vcol, vrow) - for r := hyAxis; r <= vyAxis; r++ { - for k := hxAxis; k <= vxAxis; k++ { + for r := hrowIdx; r <= vrowIdx; r++ { + for k := hcolIdx; k <= vcolIdx; k++ { xlsx.SheetData.Row[r].C[k].S = styleID } } -- cgit v1.2.1