diff options
| author | Veniamin Albaev <albenik@gmail.com> | 2019-03-19 19:14:41 +0300 |
|---|---|---|
| committer | xuri <xuri.me@gmail.com> | 2019-03-20 00:14:41 +0800 |
| commit | dc01264562e6e88d77a28042408029770ea32df4 (patch) | |
| tree | f3d8fd1627fb71676bab59fe2fa1c9b076b360d8 /sheet.go | |
| parent | 092f16c744c40e85be5cf6128dfb35c96e7df78b (diff) | |
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
Diffstat (limited to 'sheet.go')
| -rw-r--r-- | sheet.go | 48 |
1 files changed, 36 insertions, 12 deletions
@@ -14,7 +14,6 @@ import ( "encoding/json" "encoding/xml" "errors" - "fmt" "io/ioutil" "os" "path" @@ -100,16 +99,16 @@ func (f *File) workBookWriter() { // workSheetWriter provides a function to save xl/worksheets/sheet%d.xml after // serialize structure. func (f *File) workSheetWriter() { - for path, sheet := range f.Sheet { + for p, sheet := range f.Sheet { if sheet != nil { for k, v := range sheet.SheetData.Row { - f.Sheet[path].SheetData.Row[k].C = trimCell(v.C) + f.Sheet[p].SheetData.Row[k].C = trimCell(v.C) } output, _ := xml.Marshal(sheet) - f.saveFileList(path, replaceWorkSheetsRelationshipsNameSpaceBytes(output)) - ok := f.checked[path] + f.saveFileList(p, replaceWorkSheetsRelationshipsNameSpaceBytes(output)) + ok := f.checked[p] if ok { - f.checked[path] = false + f.checked[p] = false } } } @@ -679,7 +678,9 @@ func (f *File) SearchSheet(sheet, value string, reg ...bool) []string { regSearch = r } xlsx := f.workSheetReader(sheet) - result := []string{} + var ( + result []string + ) name, ok := f.sheetMap[trimSheetName(sheet)] if !ok { return result @@ -716,7 +717,9 @@ func (f *File) SearchSheet(sheet, value string, reg ...bool) []string { continue } } - result = append(result, fmt.Sprintf("%s%d", strings.Map(letterOnlyMapF, colCell.R), r.R)) + + cellCol, _ := MustCellNameToCoordinates(colCell.R) + result = append(result, MustCoordinatesToCellName(cellCol, r.R)) } } default: @@ -775,7 +778,7 @@ func (f *File) UnprotectSheet(sheet string) { // trimSheetName provides a function to trim invaild characters by given worksheet // name. func trimSheetName(name string) string { - r := []rune{} + var r []rune for _, v := range name { switch v { case 58, 92, 47, 63, 42, 91, 93: // replace :\/?*[] @@ -852,7 +855,7 @@ func (p *PageLayoutPaperSize) getPageLayout(ps *xlsxPageSetUp) { // // Available options: // PageLayoutOrientation(string) -// PageLayoutPaperSize(int) +// PageLayoutPaperSize(int) // // The following shows the paper size sorted by excelize index number: // @@ -1021,10 +1024,31 @@ func (f *File) workSheetRelsReader(path string) *xlsxWorkbookRels { // workSheetRelsWriter provides a function to save // xl/worksheets/_rels/sheet%d.xml.rels after serialize structure. func (f *File) workSheetRelsWriter() { - for path, r := range f.WorkSheetRels { + for p, r := range f.WorkSheetRels { if r != nil { v, _ := xml.Marshal(r) - f.saveFileList(path, v) + f.saveFileList(p, v) + } + } +} + +// fillSheetData fill missing row and cell XML data to made it continous from first cell [1, 1] to last cell [col, row] +func prepareSheetXML(xlsx *xlsxWorksheet, col int, row int) { + rowCount := len(xlsx.SheetData.Row) + if rowCount < row { + // append missing rows + for rowIdx := rowCount; rowIdx < row; rowIdx++ { + xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{R: rowIdx + 1}) + } + } + for rowIdx := range xlsx.SheetData.Row { + rowData := &xlsx.SheetData.Row[rowIdx] // take reference + cellCount := len(rowData.C) + if cellCount < col { + for colIdx := cellCount; colIdx < col; colIdx++ { + cellName, _ := CoordinatesToCellName(colIdx+1, rowIdx+1) + rowData.C = append(rowData.C, xlsxC{R: cellName}) + } } } } |
