lucky_sheet_table.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package excel
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. // HandleTableCell 前端d毛需要我根据合并单元格处理掉多余的单元格
  7. func HandleTableCell(oldTableData TableData) (newTableData TableData) {
  8. newTableData = oldTableData
  9. mergeList := oldTableData.MergeList
  10. lenMergeList := len(mergeList)
  11. if lenMergeList <= 0 {
  12. return
  13. }
  14. deleteRowMap := make(map[int]map[int]int)
  15. deleteRowList := make([]int, 0)
  16. deleteColumnMap := make(map[int][]int, 0)
  17. for i := lenMergeList - 1; i >= 0; i-- {
  18. tmpMerge := mergeList[i]
  19. //
  20. for rowIndex := tmpMerge.MergeRowNum; rowIndex >= 0; rowIndex-- {
  21. tmpColumnMap := make(map[int]int)
  22. if columnMap, ok := deleteRowMap[tmpMerge.StartRowIndex+rowIndex]; ok {
  23. tmpColumnMap = columnMap
  24. } else {
  25. deleteRowList = append(deleteRowList, tmpMerge.StartRowIndex+rowIndex)
  26. }
  27. deleteColumnList := make([]int, 0)
  28. if columnList, ok := deleteColumnMap[tmpMerge.StartRowIndex+rowIndex]; ok {
  29. deleteColumnList = columnList
  30. }
  31. for columnIndex := tmpMerge.MergeColumnNum; columnIndex >= 0; columnIndex-- {
  32. if rowIndex == 0 && columnIndex == 0 {
  33. continue
  34. }
  35. tmpColumnMap[tmpMerge.StartColumnIndex+columnIndex] = tmpMerge.StartColumnIndex + columnIndex
  36. deleteColumnList = append(deleteColumnList, tmpMerge.StartColumnIndex+columnIndex)
  37. }
  38. // 待删除的行
  39. deleteRowMap[tmpMerge.StartRowIndex+rowIndex] = tmpColumnMap
  40. // 该行待删除的列
  41. deleteColumnMap[tmpMerge.StartRowIndex+rowIndex] = deleteColumnList
  42. }
  43. }
  44. sort.Ints(deleteRowList)
  45. for i := len(deleteRowList) - 1; i >= 0; i-- {
  46. rowIndex := deleteRowList[i]
  47. deleteColumnList := deleteColumnMap[rowIndex]
  48. sort.Ints(deleteColumnList)
  49. for i := len(deleteColumnList) - 1; i >= 0; i-- {
  50. columnIndex := deleteColumnList[i]
  51. // 最后一行合并单元格时,就不再次移除合并的单元格,避免数组越界
  52. if rowIndex >= len(newTableData.TableDataList) {
  53. continue
  54. }
  55. tmpColumnDataList := newTableData.TableDataList[rowIndex]
  56. // 最后一列合并单元格时,就不再次移除合并的单元格,避免数组越界
  57. if columnIndex >= len(tmpColumnDataList) {
  58. continue
  59. }
  60. newTableData.TableDataList[rowIndex] = append(tmpColumnDataList[:columnIndex], tmpColumnDataList[columnIndex+1:]...) // 删除开头N个元素
  61. //fmt.Println("row:", rowIndex, "===column:", columnIndex)
  62. }
  63. }
  64. // 数据值处理
  65. for rowIndex, colList := range newTableData.TableDataList {
  66. for colIndex, v := range colList {
  67. v.Monitor = handleCellVal(v)
  68. colList[colIndex] = v
  69. }
  70. newTableData.TableDataList[rowIndex] = colList
  71. }
  72. return
  73. }
  74. // handleCellVal 处理单元格数据
  75. func handleCellVal(tmpTableColData LuckySheetDataValue) (valueStr string) {
  76. valueStr = tmpTableColData.Monitor
  77. if valueStr == `` {
  78. //valueStr = fmt.Sprint(cellInfo.Value)
  79. if valueStr == `` && tmpTableColData.CellType.S != nil {
  80. //不是设置在单元格上面,而是设置在文本上
  81. for _, cellS := range tmpTableColData.CellType.S {
  82. valueStr += fmt.Sprint(cellS.Value)
  83. }
  84. }
  85. }
  86. return
  87. }