lucky_sheet.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. package excel
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_chart_lib/utils"
  6. "sort"
  7. )
  8. type LuckySheetDataBak struct {
  9. CalcChain []interface{} `json:"calcChain" description:"公式链"`
  10. CellData []LuckySheetCellData `json:"celldata" description:"单元格数据"`
  11. ChWidth int64 `json:"ch_width" description:"工作表区域的宽度"`
  12. Config struct {
  13. BorderInfo []struct {
  14. BorderType string `json:"borderType" description:""`
  15. Color string `json:"color" description:""`
  16. Range []struct {
  17. Column []int64 `json:"column" description:""`
  18. Row []int64 `json:"row" description:""`
  19. } `json:"range" description:""`
  20. RangeType string `json:"rangeType" description:""`
  21. Style string `json:"style" description:""`
  22. Value struct {
  23. B struct {
  24. Color string `json:"color" description:""`
  25. Style string `json:"style" description:""`
  26. } `json:"b" description:""`
  27. ColIndex int64 `json:"col_index" description:""`
  28. L struct {
  29. Color string `json:"color" description:""`
  30. Style string `json:"style" description:""`
  31. } `json:"l" description:""`
  32. R struct {
  33. Color string `json:"color" description:""`
  34. Style string `json:"style" description:""`
  35. } `json:"r" description:""`
  36. RowIndex int64 `json:"row_index" description:""`
  37. T struct {
  38. Color string `json:"color" description:""`
  39. Style string `json:"style" description:""`
  40. } `json:"t" description:""`
  41. } `json:"value" description:"" description:""`
  42. } `json:"borderInfo" description:""`
  43. Colhidden struct{} `json:"colhidden" description:""`
  44. Columnlen map[string]float64 `json:"columnlen" description:""`
  45. CustomHeight struct {
  46. Zero int64 `json:"0"`
  47. } `json:"customHeight" description:""`
  48. CustomWidth struct {
  49. Two int64 `json:"2" description:""`
  50. } `json:"customWidth" description:""`
  51. Merge struct{} `json:"merge" description:""`
  52. Rowlen map[string]float64 `json:"rowlen" description:""`
  53. } `json:"config" description:""`
  54. Data [][]struct {
  55. Ct struct {
  56. Fa string `json:"fa"`
  57. T string `json:"t"`
  58. } `json:"ct"`
  59. M string `json:"m"`
  60. V interface{} `json:"v"`
  61. } `json:"data" description:""`
  62. DataVerification struct{} `json:"dataVerification" description:""`
  63. Filter interface{} `json:"filter" description:""`
  64. FilterSelect interface{} `json:"filter_select" description:""`
  65. Hyperlink struct{} `json:"hyperlink" description:""`
  66. Images struct{} `json:"images" description:""`
  67. Index string `json:"index" description:""`
  68. JfgirdSelectSave []interface{} `json:"jfgird_select_save" description:""`
  69. LuckysheetAlternateformatSave []interface{} `json:"luckysheet_alternateformat_save" description:""`
  70. LuckysheetConditionformatSave []interface{} `json:"luckysheet_conditionformat_save" description:""`
  71. LuckysheetSelectSave []struct {
  72. Column []int64 `json:"column" description:""`
  73. ColumnFocus int64 `json:"column_focus" description:""`
  74. Height int64 `json:"height" description:""`
  75. HeightMove int64 `json:"height_move" description:""`
  76. Left int64 `json:"left" description:""`
  77. LeftMove int64 `json:"left_move" description:""`
  78. Row []int64 `json:"row" description:""`
  79. RowFocus int64 `json:"row_focus" description:""`
  80. Top int64 `json:"top" description:""`
  81. TopMove int64 `json:"top_move" description:""`
  82. Width int64 `json:"width" description:""`
  83. WidthMove int64 `json:"width_move" description:""`
  84. } `json:"luckysheet_select_save" description:"" description:""`
  85. LuckysheetSelectionRange []struct {
  86. Column []int64 `json:"column" description:""`
  87. Row []int64 `json:"row" description:""`
  88. } `json:"luckysheet_selection_range" description:""`
  89. RhHeight int64 `json:"rh_height" description:""`
  90. ScrollLeft int64 `json:"scrollLeft" description:""`
  91. ScrollTop int64 `json:"scrollTop" description:""`
  92. Status int64 `json:"status" description:""`
  93. Visibledatacolumn []int64 `json:"visibledatacolumn" description:""`
  94. Visibledatarow []int64 `json:"visibledatarow" description:""`
  95. ZoomRatio int64 `json:"zoomRatio" description:""`
  96. }
  97. // LuckySheetData sheet表格数据
  98. type LuckySheetData struct {
  99. CellData []LuckySheetCellData `json:"celldata" description:"单元格数据"`
  100. ChWidth int64 `json:"ch_width" description:"工作表区域的宽度"`
  101. Config LuckySheetDataConfig `json:"config" description:""`
  102. //Index int `json:"index" description:"工作表索引"`
  103. RhHeight int64 `json:"rh_height" description:"工作表区域的高度"`
  104. ScrollLeft int64 `json:"scrollLeft" description:"左右滚动条位置"`
  105. ScrollTop int64 `json:"scrollTop" description:"上下滚动条位置"`
  106. Status int64 `json:"status" description:"激活状态"`
  107. VisibleDataColumn []int64 `json:"visibledatacolumn" description:"所有列的位置信息,递增的列位置数据,初始化无需设置"`
  108. VisibleDataRow []int64 `json:"visibledatarow" description:"所有行的位置信息,递增的行位置数据,初始化无需设置"`
  109. ZoomRatio int64 `json:"zoomRatio" description:"缩放比例"`
  110. }
  111. // LuckySheetDataConfig sheet表单的配置
  112. type LuckySheetDataConfig struct {
  113. BorderInfo []LuckySheetDataConfigBorderInfo `json:"borderInfo" description:"边框"`
  114. Colhidden map[string]int64 `json:"colhidden" description:"隐藏列,示例值:\"colhidden\":{\"30\":0,\"31\":0}"`
  115. Columnlen map[string]float64 `json:"columnlen" description:"每个单元格的列宽"`
  116. //CustomHeight struct {
  117. // Zero int64 `json:"0"`
  118. //} `json:"customHeight" description:""`
  119. //CustomWidth struct {
  120. // Two int64 `json:"2" description:""`
  121. //} `json:"customWidth" description:""`
  122. Merge map[string]LuckySheetDataConfigMerge `json:"merge" description:"合并单元格"`
  123. Rowlen map[string]float64 `json:"rowlen" description:"每个单元格的行高"`
  124. }
  125. // LuckySheetDataConfigMerge 合并单元格设置
  126. type LuckySheetDataConfigMerge struct {
  127. Row int `json:"r" description:"行数"`
  128. Column int `json:"c" description:"列数"`
  129. Rs int `json:"rs" description:"合并的行数"`
  130. Cs int `json:"cs" description:"合并的列数"`
  131. }
  132. // LuckySheetDataConfigBorderInfo 单元格边框信息
  133. type LuckySheetDataConfigBorderInfo struct {
  134. BorderType string `json:"borderType" description:"边框类型 border-left | border-right | border-top | border-bottom | border-all | border-outside | border-inside | border-horizontal | border-vertical | border-none"`
  135. Color string `json:"color" description:"边框颜色 color: 16进制颜色值"`
  136. Range []struct {
  137. Column []int64 `json:"column" description:"行"`
  138. Row []int64 `json:"row" description:"列"`
  139. } `json:"range" description:"选区范围 range: 行列信息数组"`
  140. RangeType string `json:"rangeType" description:"选区 rangeType: range | cell "`
  141. Style string `json:"style" description:"边框粗细 style: 1 Thin | 2 Hair | 3 Dotted | 4 Dashed | 5 DashDot | 6 DashDotDot | 7 Double | 8 Medium | 9 MediumDashed | 10 MediumDashDot | 11 MediumDashDotDot | 12 SlantedDashDot | 13 Thick ,和aspose.cells的getLineStyle()的值对应的话,需要自己做个转换,参考 aspose.cells"`
  142. Value LuckySheetDataConfigBorderInfoCellValue `json:"value" description:"" description:"范围类型分单个单元格的数据"`
  143. }
  144. // LuckySheetDataConfigBorderInfoCellValue 单元格边框信息(范围类型为:单个单元格)
  145. type LuckySheetDataConfigBorderInfoCellValue struct {
  146. B LuckySheetDataConfigBorderInfoCell `json:"b" description:"下边框"`
  147. L LuckySheetDataConfigBorderInfoCell `json:"l" description:"左边框"`
  148. R LuckySheetDataConfigBorderInfoCell `json:"r" description:"右边框"`
  149. T LuckySheetDataConfigBorderInfoCell `json:"t" description:"上边框"`
  150. ColIndex int64 `json:"col_index" description:"第几行"`
  151. RowIndex int64 `json:"row_index" description:"第几列"`
  152. }
  153. // LuckySheetDataConfigBorderInfoCell 单元格边框信息(cell类型)
  154. type LuckySheetDataConfigBorderInfoCell struct {
  155. Color string `json:"color" description:"边框颜色 color: 16进制颜色值"`
  156. Style string `json:"style" description:"边框粗细 style: 1 Thin | 2 Hair | 3 Dotted | 4 Dashed | 5 DashDot | 6 DashDotDot | 7 Double | 8 Medium | 9 MediumDashed | 10 MediumDashDot | 11 MediumDashDotDot | 12 SlantedDashDot | 13 Thick ,和aspose.cells的getLineStyle()的值对应的话,需要自己做个转换,参考 aspose.cells"`
  157. }
  158. // LuckySheetCellData 单个单元格数据
  159. type LuckySheetCellData struct {
  160. Col int64 `json:"c" description:"列"`
  161. Row int64 `json:"r" description:"行"`
  162. Value LuckySheetDataValue `json:"v" description:"单元格内值的数据"`
  163. }
  164. // LuckySheetDataValue 单元格内值的数据
  165. type LuckySheetDataValue struct {
  166. CellType LuckySheetDataCellType `json:"ct" description:"单元格值格式:文本、时间等 "`
  167. Value interface{} `json:"v" description:"原始值"`
  168. Monitor string `json:"m" description:"显示值"`
  169. Background string `json:"bg" description:"背景色,实例值:#fff000"`
  170. FontFamily int `json:"ff" description:"字体,0 Times New Roman、 1 Arial、2 Tahoma 、3 Verdana、4 微软雅黑、5 宋体(Song)、6 黑体(ST Heiti)、7 楷体(ST Kaiti)、 8 仿宋(ST FangSong)、9 新宋体(ST Song)、10 华文新魏、11 华文行楷、12 华文隶书 "`
  171. FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
  172. Bold int `json:"bl" description:"粗体,0 常规 、 1加粗 "`
  173. Italic int `json:"it" description:"斜体,0 常规 、 1 斜体"`
  174. Fontsize int `json:"fs" description:"字体大小,14"`
  175. CancelLine int `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
  176. HorizontalType int `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
  177. VerticalType int `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
  178. //TextRotate string `json:"tr" description:"竖排文字, 3"`
  179. //RotateText string `json:"rt" description:"文字旋转角度, 介于0~180之间的整数,包含0和180"`
  180. TextBeak int `json:"tb" description:"文本换行, 0 截断、1溢出、2 自动换行"`
  181. Ps LuckySheetDataCellComment `json:"ps" description:"批注"`
  182. Function string `json:"f" description:"公式"`
  183. MergeCell LuckySheetDataConfigMerge `json:"mc" description:"合并单元格信息"`
  184. }
  185. // LuckySheetDataCellType 单元格值格式:文本、时间等
  186. type LuckySheetDataCellType struct {
  187. Fa string `json:"fa" description:"格式名称,例如:“General”为自动格式"`
  188. T string `json:"t" description:"格式类型,例如:“n”为数字类型"`
  189. S []struct {
  190. FontFamily string `json:"ff" description:"字体,0 Times New Roman、 1 Arial、2 Tahoma 、3 Verdana、4 微软雅黑、5 宋体(Song)、6 黑体(ST Heiti)、7 楷体(ST Kaiti)、 8 仿宋(ST FangSong)、9 新宋体(ST Song)、10 华文新魏、11 华文行楷、12 华文隶书 "`
  191. FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
  192. Fontsize int `json:"fs" description:"字体大小,14"`
  193. CancelLine int `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
  194. HorizontalType int `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
  195. VerticalType int `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
  196. Un int `json:"un" description:""`
  197. Bold int `json:"bl" description:"粗体,0 常规 、 1加粗 "`
  198. Italic int `json:"it" description:"斜体,0 常规 、 1 斜体"`
  199. Value interface{} `json:"v" description:"原始值"`
  200. } `json:"s"`
  201. }
  202. // LuckySheetDataCellComment 批注
  203. type LuckySheetDataCellComment struct {
  204. Left int `json:"left" description:"批注框距离左边工作表边缘位置"`
  205. Top int `json:"top" description:"批注框距离上边工作表边缘位置"`
  206. Width int `json:"width" description:"批注框宽度"`
  207. Height int `json:"height" description:"批注框高度"`
  208. Value string `json:"value" description:"批注内容"`
  209. IsShow bool `json:"isshow" description:"是否显示批注"`
  210. }
  211. // GetLuckySheetData 获取LuckySheetData的结构体
  212. func GetLuckySheetData(jsonStr string) (item *LuckySheetData, err error) {
  213. err = json.Unmarshal([]byte(jsonStr), &item)
  214. return
  215. }
  216. // TableData 表格数据
  217. type TableData struct {
  218. TableDataList [][]LuckySheetDataValue
  219. RowWidthList []float64
  220. RowHeightList []float64
  221. RemoveTopRow int `description:"移除表格上方的行数"`
  222. RemoveBottomRow int `description:"移除表格下方的行数"`
  223. RemoveLeftColumn int `description:"移除表格左侧的列数"`
  224. RemoveRightColumn int `description:"移除表格右侧的列数"`
  225. MergeList []TableDataMerge `description:"合并数据列"`
  226. }
  227. // TableDataMerge 表格数据合并单元格配置
  228. type TableDataMerge struct {
  229. StartRowIndex int `json:"start_row_index" description:"开始的行下标"`
  230. StartColumnIndex int `json:"start_column" description:"开始的列下标"`
  231. MergeRowNum int `json:"merge_row_num" description:"合并的行数"`
  232. MergeColumnNum int `json:"merge_column_num" description:"合并的列数"`
  233. }
  234. // GetTableDataByLuckySheetDataStr 通过LuckySheet的string数据获取表格数据
  235. func (item *LuckySheetData) GetTableDataByLuckySheetDataStr() (selfTableData TableData, err error) {
  236. luckySheetCellDataList := item.CellData
  237. // 表格数据
  238. tableDataMap := make(map[int64]map[int64]LuckySheetDataValue)
  239. // 最大行,最大列
  240. var maxRow, maxCol int64
  241. for _, v := range luckySheetCellDataList {
  242. //fmt.Println("row:", v.Row, "=====col:", v.Col)
  243. if v.Row > maxRow { //最大行
  244. maxRow = v.Row
  245. }
  246. if v.Col > maxCol { //最大列
  247. maxCol = v.Col
  248. }
  249. var tmpRow map[int64]LuckySheetDataValue
  250. tmpRow, ok := tableDataMap[v.Row]
  251. if ok {
  252. tmpRow[v.Col] = v.Value
  253. } else {
  254. tmpRow = make(map[int64]LuckySheetDataValue)
  255. tmpRow[v.Col] = v.Value
  256. }
  257. tableDataMap[v.Row] = tmpRow
  258. }
  259. tableDataList := make([][]LuckySheetDataValue, 0)
  260. var i int64
  261. // 单元格宽度
  262. configColumnConf := item.Config.Columnlen
  263. rowWidthMap := make(map[int]float64)
  264. rowWidthList := make([]float64, 0) //
  265. // 单元格高度
  266. configRowConf := item.Config.Rowlen
  267. rowHeightList := make([]float64, 0) //
  268. for i = 0; i <= maxRow; i++ {
  269. //列
  270. tmpTableColDataList := make([]LuckySheetDataValue, 0)
  271. // 每个单元格的高度
  272. tmpHeight, ok := configRowConf[fmt.Sprint(i)]
  273. if !ok {
  274. tmpHeight = 0
  275. }
  276. rowHeightList = append(rowHeightList, tmpHeight)
  277. tmpRowData, ok := tableDataMap[i]
  278. // 如果没有该行数据,那么就默认添加空行数据处理
  279. if !ok {
  280. tmpRowData = make(map[int64]LuckySheetDataValue)
  281. }
  282. var j int64
  283. for j = 0; j <= maxCol; j++ {
  284. tmpTableColData, ok := tmpRowData[j]
  285. if !ok {
  286. tmpTableColData = LuckySheetDataValue{}
  287. }
  288. //单元格显示的数据处理
  289. tmpTableColData.Monitor = handleCellVal(tmpTableColData)
  290. tmpTableColDataList = append(tmpTableColDataList, tmpTableColData)
  291. // 每个单元格的宽度
  292. tmpWidth, ok := configColumnConf[fmt.Sprint(j)]
  293. if !ok {
  294. tmpWidth = 0
  295. }
  296. rowIndex := len(tmpTableColDataList) - 1
  297. if _, ok2 := rowWidthMap[rowIndex]; !ok2 {
  298. rowWidthList = append(rowWidthList, tmpWidth)
  299. }
  300. rowWidthMap[rowIndex] = tmpWidth
  301. }
  302. tableDataList = append(tableDataList, tmpTableColDataList)
  303. }
  304. // 数据处理,移除上下左右空行空列
  305. tableDataList, tableRemoveNum, rowHeightList, rowWidthList, tableDataMergeList := handleTableDataList(tableDataList, item.Config.Merge, rowHeightList, rowWidthList)
  306. // 表格数据
  307. {
  308. // 移除空白单元格信息
  309. selfTableData.RemoveTopRow = tableRemoveNum.RemoveTopRow
  310. selfTableData.RemoveBottomRow = tableRemoveNum.RemoveBottomRow
  311. selfTableData.RemoveLeftColumn = tableRemoveNum.RemoveLeftColumn
  312. selfTableData.RemoveRightColumn = tableRemoveNum.RemoveRightColumn
  313. }
  314. selfTableData.TableDataList = tableDataList
  315. selfTableData.RowWidthList = rowWidthList
  316. selfTableData.RowHeightList = rowHeightList
  317. selfTableData.MergeList = tableDataMergeList
  318. return
  319. }
  320. // TableRemoveNum 上下左右移除的空行空列数量
  321. type TableRemoveNum struct {
  322. RemoveTopRow int `description:"移除表格上方的行数"`
  323. RemoveBottomRow int `description:"移除表格下方的行数"`
  324. RemoveLeftColumn int `description:"移除表格左侧的列数"`
  325. RemoveRightColumn int `description:"移除表格右侧的列数"`
  326. }
  327. // handleTableDataList 表格数据处理(移除上下左右的空行空列)
  328. func handleTableDataList(tableDataList [][]LuckySheetDataValue, luckySheetDataConfigMergeList map[string]LuckySheetDataConfigMerge, rowHeightList, rowWidthList []float64) ([][]LuckySheetDataValue, TableRemoveNum, []float64, []float64, []TableDataMerge) {
  329. var removeTopRow, removeBottomRow, removeLeftColumn, removeRightColumn int //上下左右需要移除的空行空列
  330. tableDataMergeList := make([]TableDataMerge, 0) //待合并的单元格信息
  331. //总共多少行
  332. lenRow := len(tableDataList)
  333. //移除上下空行
  334. deleteRowIndexList := make([]int, 0)
  335. if lenRow > 0 {
  336. var flag = false
  337. // 移除上方空列
  338. for rowIndex := 0; rowIndex < lenRow; rowIndex++ {
  339. isDelete := true
  340. for _, v := range tableDataList[rowIndex] {
  341. if v.Monitor != `` || (v.MergeCell.Row != rowIndex && v.MergeCell.Row != 0) {
  342. isDelete = false
  343. flag = true
  344. break
  345. }
  346. }
  347. if flag {
  348. break
  349. }
  350. if isDelete {
  351. removeTopRow++
  352. deleteRowIndexList = append(deleteRowIndexList, rowIndex)
  353. }
  354. }
  355. // 移除下方空行
  356. flag = false
  357. //尾部
  358. deleteBottomRowIndexList := make([]int, 0)
  359. for rowIndex := lenRow - 1; rowIndex >= 0; rowIndex-- {
  360. isDelete := true
  361. for _, v := range tableDataList[rowIndex] {
  362. if v.Monitor != `` {
  363. isDelete = false
  364. flag = true
  365. break
  366. }
  367. }
  368. if flag {
  369. break
  370. }
  371. if isDelete {
  372. deleteBottomRowIndexList = append(deleteBottomRowIndexList, rowIndex)
  373. removeBottomRow++
  374. }
  375. }
  376. if len(deleteBottomRowIndexList) > 0 {
  377. deleteRowIndexList = append(deleteRowIndexList, utils.RevSlice(deleteBottomRowIndexList)...)
  378. }
  379. lenDeleteRow := len(deleteRowIndexList)
  380. if lenDeleteRow > 0 {
  381. for rowIndex := lenDeleteRow - 1; rowIndex >= 0; rowIndex-- {
  382. //表格数据
  383. tableDataList = append(tableDataList[:deleteRowIndexList[rowIndex]], tableDataList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  384. //表格高度
  385. rowHeightList = append(rowHeightList[:deleteRowIndexList[rowIndex]], rowHeightList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  386. }
  387. }
  388. }
  389. //移除左右空列
  390. deleteColumnIndexList := make([]int, 0)
  391. if len(tableDataList) > 0 {
  392. var flag = false
  393. lenColumn := len(tableDataList[0])
  394. // 移除左边空列
  395. for columnIndex := 0; columnIndex < lenColumn; columnIndex++ {
  396. isDelete := true
  397. for _, v := range tableDataList {
  398. //如果一列都没有,说明是上面几行是空行,没有数据
  399. if len(v) <= 0 {
  400. continue
  401. }
  402. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  403. isDelete = false
  404. flag = true
  405. break
  406. }
  407. }
  408. if flag {
  409. break
  410. }
  411. if isDelete {
  412. removeLeftColumn++
  413. deleteColumnIndexList = append(deleteColumnIndexList, columnIndex)
  414. }
  415. }
  416. // 移除右方空列
  417. flag = false
  418. //右边
  419. deleteTailColumnIndexList := make([]int, 0)
  420. for columnIndex := lenColumn - 1; columnIndex >= 0; columnIndex-- {
  421. isDelete := true
  422. for _, v := range tableDataList {
  423. //如果一列都没有,说明是上面几行是空行,没有数据
  424. if len(v) <= 0 {
  425. continue
  426. }
  427. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  428. isDelete = false
  429. flag = true
  430. break
  431. }
  432. }
  433. if flag {
  434. break
  435. }
  436. if isDelete {
  437. deleteTailColumnIndexList = append(deleteTailColumnIndexList, columnIndex)
  438. removeRightColumn++
  439. }
  440. }
  441. if len(deleteTailColumnIndexList) > 0 {
  442. deleteColumnIndexList = append(deleteColumnIndexList, utils.RevSlice(deleteTailColumnIndexList)...)
  443. }
  444. lenDeleteColumn := len(deleteColumnIndexList)
  445. if lenDeleteColumn > 0 {
  446. for columnIndex := lenDeleteColumn - 1; columnIndex >= 0; columnIndex-- {
  447. //表格数据
  448. for k, v := range tableDataList {
  449. tableDataList[k] = append(v[:deleteColumnIndexList[columnIndex]], v[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  450. }
  451. //表格宽度
  452. rowWidthList = append(rowWidthList[:deleteColumnIndexList[columnIndex]], rowWidthList[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  453. }
  454. }
  455. }
  456. //处理合并单元格
  457. for _, v := range luckySheetDataConfigMergeList {
  458. indexRow := v.Row - removeTopRow
  459. indexColumn := v.Column - removeLeftColumn
  460. tableDataMerge := TableDataMerge{
  461. StartRowIndex: indexRow,
  462. StartColumnIndex: indexColumn,
  463. MergeRowNum: v.Rs - 1,
  464. MergeColumnNum: v.Cs - 1,
  465. }
  466. tableDataMergeList = append(tableDataMergeList, tableDataMerge)
  467. }
  468. return tableDataList, TableRemoveNum{
  469. RemoveTopRow: removeTopRow,
  470. RemoveBottomRow: removeBottomRow,
  471. RemoveLeftColumn: removeLeftColumn,
  472. RemoveRightColumn: removeRightColumn,
  473. }, rowHeightList, rowWidthList, tableDataMergeList
  474. }
  475. // handleCellVal 处理单元格数据
  476. func handleCellVal(tmpTableColData LuckySheetDataValue) (valueStr string) {
  477. valueStr = tmpTableColData.Monitor
  478. if valueStr == `` {
  479. //valueStr = fmt.Sprint(cellInfo.Value)
  480. if valueStr == `` && tmpTableColData.CellType.S != nil {
  481. //不是设置在单元格上面,而是设置在文本上
  482. for _, cellS := range tmpTableColData.CellType.S {
  483. valueStr += fmt.Sprint(cellS.Value)
  484. }
  485. }
  486. }
  487. return
  488. }
  489. type CellPosition struct {
  490. RowIndex int
  491. ColumnIndex int
  492. }
  493. // HandleTableCell 前端d毛需要我根据合并单元格处理掉多余的单元格
  494. func HandleTableCell(oldTableData TableData) (newTableData TableData) {
  495. newTableData = oldTableData
  496. mergeList := oldTableData.MergeList
  497. lenMergeList := len(mergeList)
  498. if lenMergeList <= 0 {
  499. return
  500. }
  501. deleteRowMap := make(map[int]map[int]int)
  502. deleteRowList := make([]int, 0)
  503. deleteColumnMap := make(map[int][]int, 0)
  504. for i := lenMergeList - 1; i >= 0; i-- {
  505. tmpMerge := mergeList[i]
  506. //
  507. for rowIndex := tmpMerge.MergeRowNum; rowIndex >= 0; rowIndex-- {
  508. tmpColumnMap := make(map[int]int)
  509. if columnMap, ok := deleteRowMap[tmpMerge.StartRowIndex+rowIndex]; ok {
  510. tmpColumnMap = columnMap
  511. } else {
  512. deleteRowList = append(deleteRowList, tmpMerge.StartRowIndex+rowIndex)
  513. }
  514. deleteColumnList := make([]int, 0)
  515. if columnList, ok := deleteColumnMap[tmpMerge.StartRowIndex+rowIndex]; ok {
  516. deleteColumnList = columnList
  517. }
  518. for columnIndex := tmpMerge.MergeColumnNum; columnIndex >= 0; columnIndex-- {
  519. if rowIndex == 0 && columnIndex == 0 {
  520. continue
  521. }
  522. tmpColumnMap[tmpMerge.StartColumnIndex+columnIndex] = tmpMerge.StartColumnIndex + columnIndex
  523. deleteColumnList = append(deleteColumnList, tmpMerge.StartColumnIndex+columnIndex)
  524. }
  525. // 待删除的行
  526. deleteRowMap[tmpMerge.StartRowIndex+rowIndex] = tmpColumnMap
  527. // 该行待删除的列
  528. deleteColumnMap[tmpMerge.StartRowIndex+rowIndex] = deleteColumnList
  529. }
  530. }
  531. sort.Ints(deleteRowList)
  532. //for k, v := range deleteRowList {
  533. // fmt.Println("k:", k, "v:", v)
  534. //}
  535. //return
  536. //fmt.Println("===============")
  537. for i := len(deleteRowList) - 1; i >= 0; i-- {
  538. rowIndex := deleteRowList[i]
  539. deleteColumnList := deleteColumnMap[rowIndex]
  540. sort.Ints(deleteColumnList)
  541. for i := len(deleteColumnList) - 1; i >= 0; i-- {
  542. columnIndex := deleteColumnList[i]
  543. tmpColumnDataList := newTableData.TableDataList[rowIndex]
  544. newTableData.TableDataList[rowIndex] = append(tmpColumnDataList[:columnIndex], tmpColumnDataList[columnIndex+1:]...) // 删除开头N个元素
  545. //fmt.Println("row:", rowIndex, "===column:", columnIndex)
  546. }
  547. }
  548. return
  549. }
  550. // GetTableDataByLuckySheetDataStrBak 通过LuckySheet的string数据获取表格数据(备份:2022-08-23 10:30:32)
  551. func (item *LuckySheetData) GetTableDataByLuckySheetDataStrBak() (selfTableData TableData, err error) {
  552. luckySheetCellDataList := item.CellData
  553. // 表格数据
  554. tableDataMap := make(map[int64]map[int64]LuckySheetDataValue)
  555. // 最大行,最大列
  556. var maxRow, maxCol int64
  557. for _, v := range luckySheetCellDataList {
  558. //fmt.Println("row:", v.Row, "=====col:", v.Col)
  559. if v.Row > maxRow { //最大行
  560. maxRow = v.Row
  561. }
  562. if v.Col > maxCol { //最大列
  563. maxCol = v.Col
  564. }
  565. var tmpRow map[int64]LuckySheetDataValue
  566. tmpRow, ok := tableDataMap[v.Row]
  567. if ok {
  568. tmpRow[v.Col] = v.Value
  569. } else {
  570. tmpRow = make(map[int64]LuckySheetDataValue)
  571. tmpRow[v.Col] = v.Value
  572. }
  573. tableDataMap[v.Row] = tmpRow
  574. }
  575. tableDataList := make([][]LuckySheetDataValue, 0)
  576. var i int64
  577. // 单元格宽度
  578. configColumnConf := item.Config.Columnlen
  579. rowWidthMap := make(map[int]float64)
  580. rowWidthList := make([]float64, 0) //
  581. // 单元格高度
  582. configRowConf := item.Config.Rowlen
  583. rowHeightList := make([]float64, 0) //
  584. for i = 0; i <= maxRow; i++ {
  585. //列
  586. tmpTableColDataList := make([]LuckySheetDataValue, 0)
  587. // 每个单元格的高度
  588. tmpHeight, ok := configRowConf[fmt.Sprint(i)]
  589. if !ok {
  590. tmpHeight = 0
  591. }
  592. rowHeightList = append(rowHeightList, tmpHeight)
  593. tmpRowData, ok := tableDataMap[i]
  594. // 如果没有该行数据,那么就默认添加空行数据处理
  595. if !ok {
  596. tmpRowData = make(map[int64]LuckySheetDataValue)
  597. }
  598. var j int64
  599. for j = 0; j <= maxCol; j++ {
  600. tmpTableColData, ok := tmpRowData[j]
  601. if !ok {
  602. tmpTableColData = LuckySheetDataValue{}
  603. }
  604. //单元格显示的数据处理
  605. tmpTableColData.Monitor = handleCellVal(tmpTableColData)
  606. tmpTableColDataList = append(tmpTableColDataList, tmpTableColData)
  607. // 每个单元格的宽度
  608. tmpWidth, ok := configColumnConf[fmt.Sprint(j)]
  609. if !ok {
  610. tmpWidth = 0
  611. }
  612. rowIndex := len(tmpTableColDataList) - 1
  613. if _, ok2 := rowWidthMap[rowIndex]; !ok2 {
  614. rowWidthList = append(rowWidthList, tmpWidth)
  615. }
  616. rowWidthMap[rowIndex] = tmpWidth
  617. }
  618. tableDataList = append(tableDataList, tmpTableColDataList)
  619. }
  620. //总共多少行
  621. lenRow := len(tableDataList)
  622. //移除上下空行
  623. deleteRowIndexList := make([]int, 0)
  624. if lenRow > 0 {
  625. var flag = false
  626. // 移除上方空列
  627. for rowIndex := 0; rowIndex < lenRow; rowIndex++ {
  628. isDelete := true
  629. for _, v := range tableDataList[rowIndex] {
  630. if v.Monitor != `` {
  631. isDelete = false
  632. flag = true
  633. break
  634. }
  635. }
  636. if flag {
  637. break
  638. }
  639. if isDelete {
  640. selfTableData.RemoveTopRow++
  641. deleteRowIndexList = append(deleteRowIndexList, rowIndex)
  642. }
  643. }
  644. // 移除下方空行
  645. flag = false
  646. //尾部
  647. deleteBottomRowIndexList := make([]int, 0)
  648. for rowIndex := lenRow - 1; rowIndex >= 0; rowIndex-- {
  649. isDelete := true
  650. for _, v := range tableDataList[rowIndex] {
  651. if v.Monitor != `` || (v.MergeCell.Row != rowIndex && v.MergeCell.Row != 0) {
  652. isDelete = false
  653. flag = true
  654. break
  655. }
  656. }
  657. if flag {
  658. break
  659. }
  660. if isDelete {
  661. deleteBottomRowIndexList = append(deleteBottomRowIndexList, rowIndex)
  662. selfTableData.RemoveBottomRow++
  663. }
  664. }
  665. if len(deleteBottomRowIndexList) > 0 {
  666. deleteRowIndexList = append(deleteRowIndexList, utils.RevSlice(deleteBottomRowIndexList)...)
  667. }
  668. lenDeleteRow := len(deleteRowIndexList)
  669. if lenDeleteRow > 0 {
  670. for rowIndex := lenDeleteRow - 1; rowIndex >= 0; rowIndex-- {
  671. //表格数据
  672. tableDataList = append(tableDataList[:deleteRowIndexList[rowIndex]], tableDataList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  673. //表格高度
  674. rowHeightList = append(rowHeightList[:deleteRowIndexList[rowIndex]], rowHeightList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  675. }
  676. }
  677. }
  678. //移除前后空列
  679. deleteColumnIndexList := make([]int, 0)
  680. if len(tableDataList) > 0 {
  681. var flag = false
  682. lenColumn := len(tableDataList[0])
  683. // 移除前方空列
  684. for columnIndex := 0; columnIndex < lenColumn; columnIndex++ {
  685. isDelete := true
  686. for _, v := range tableDataList {
  687. //如果一列都没有,说明是上面几行是空行,没有数据
  688. if len(v) <= 0 {
  689. continue
  690. }
  691. if v[columnIndex].Monitor != `` {
  692. isDelete = false
  693. flag = true
  694. break
  695. }
  696. }
  697. if flag {
  698. break
  699. }
  700. if isDelete {
  701. selfTableData.RemoveLeftColumn++
  702. deleteColumnIndexList = append(deleteColumnIndexList, columnIndex)
  703. }
  704. }
  705. // 移除后方空列
  706. flag = false
  707. //后方
  708. deleteTailColumnIndexList := make([]int, 0)
  709. for columnIndex := lenColumn - 1; columnIndex >= 0; columnIndex-- {
  710. isDelete := true
  711. for _, v := range tableDataList {
  712. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  713. isDelete = false
  714. flag = true
  715. break
  716. }
  717. }
  718. if flag {
  719. break
  720. }
  721. if isDelete {
  722. deleteTailColumnIndexList = append(deleteTailColumnIndexList, columnIndex)
  723. selfTableData.RemoveRightColumn++
  724. }
  725. }
  726. if len(deleteTailColumnIndexList) > 0 {
  727. deleteColumnIndexList = append(deleteColumnIndexList, utils.RevSlice(deleteTailColumnIndexList)...)
  728. }
  729. lenDeleteColumn := len(deleteColumnIndexList)
  730. if lenDeleteColumn > 0 {
  731. for columnIndex := lenDeleteColumn - 1; columnIndex >= 0; columnIndex-- {
  732. //表格数据
  733. for k, v := range tableDataList {
  734. tableDataList[k] = append(v[:deleteColumnIndexList[columnIndex]], v[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  735. }
  736. //表格宽度
  737. rowWidthList = append(rowWidthList[:deleteColumnIndexList[columnIndex]], rowWidthList[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  738. }
  739. }
  740. }
  741. //处理合并单元格
  742. tableDataMergeList := make([]TableDataMerge, 0)
  743. for _, v := range item.Config.Merge {
  744. indexRow := v.Row - selfTableData.RemoveTopRow
  745. indexColumn := v.Column - selfTableData.RemoveLeftColumn
  746. tableDataMerge := TableDataMerge{
  747. StartRowIndex: indexRow,
  748. StartColumnIndex: indexColumn,
  749. MergeRowNum: v.Rs - 1,
  750. MergeColumnNum: v.Cs - 1,
  751. }
  752. tableDataMergeList = append(tableDataMergeList, tableDataMerge)
  753. }
  754. // 表格数据
  755. selfTableData.TableDataList = tableDataList
  756. selfTableData.RowWidthList = rowWidthList
  757. selfTableData.RowHeightList = rowHeightList
  758. selfTableData.MergeList = tableDataMergeList
  759. return
  760. }