lucky_sheet.go 34 KB

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