lucky_sheet.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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 `json:"tb" description:"文本换行, 0 截断、1溢出、2 自动换行"`
  188. Ps LuckySheetDataCellComment `json:"ps" description:"批注"`
  189. Function string `json:"f" description:"公式"`
  190. MergeCell LuckySheetDataConfigMerge `json:"mc" description:"合并单元格信息"`
  191. }
  192. // LuckySheetDataCellType 单元格值格式:文本、时间等
  193. type LuckySheetDataCellType struct {
  194. Fa string `json:"fa" description:"格式名称,例如:“General”为自动格式"`
  195. T string `json:"t" description:"格式类型,例如:“n”为数字类型"`
  196. S []struct {
  197. 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 华文隶书 "`
  198. 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 华文隶书 "`
  199. FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
  200. Fontsize int `description:"字体大小,14"`
  201. CancelLine int ` description:"删除线, 0 常规 、 1 删除线"`
  202. HorizontalType int `description:"水平对齐, 0 居中、1 左、2右"`
  203. VerticalType int `description:"垂直对齐, 0 中间、1 上、2下"`
  204. Fs int `json:"fs" description:"字体大小,14"`
  205. Cl int `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
  206. Ht int `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
  207. Vt int `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
  208. Un int `json:"un" description:""`
  209. Bold int `json:"bl" description:"粗体,0 常规 、 1加粗 "`
  210. Italic int `json:"it" description:"斜体,0 常规 、 1 斜体"`
  211. Value interface{} `json:"v" description:"原始值"`
  212. } `json:"s"`
  213. }
  214. // LuckySheetDataCellComment 批注
  215. type LuckySheetDataCellComment struct {
  216. Left int `json:"left" description:"批注框距离左边工作表边缘位置"`
  217. Top int `json:"top" description:"批注框距离上边工作表边缘位置"`
  218. Width int `json:"width" description:"批注框宽度"`
  219. Height int `json:"height" description:"批注框高度"`
  220. Value string `json:"value" description:"批注内容"`
  221. IsShow bool `json:"isshow" description:"是否显示批注"`
  222. }
  223. // GetLuckySheetData 获取LuckySheetData的结构体
  224. func GetLuckySheetData(jsonStr string) (item *LuckySheetData, err error) {
  225. err = json.Unmarshal([]byte(jsonStr), &item)
  226. for k, v := range item.CellData {
  227. value := v.Value
  228. value.Fontsize = getIntValueByInterface(value.Fs)
  229. value.CancelLine = getIntValueByInterface(value.Cl)
  230. value.HorizontalType = getIntValueByInterface(value.Ht)
  231. value.VerticalType = getIntValueByInterface(value.Vt)
  232. value.FontFamily = getIntValueByInterface(value.FF)
  233. if len(value.CellType.S) > 0 {
  234. for kk, vv := range value.CellType.S {
  235. vv.Fontsize = getIntValueByInterface(vv.Fs)
  236. vv.CancelLine = getIntValueByInterface(vv.Cl)
  237. vv.HorizontalType = getIntValueByInterface(vv.Ht)
  238. vv.VerticalType = getIntValueByInterface(vv.Vt)
  239. vv.FontFamily = getIntValueByInterface(vv.FF)
  240. value.CellType.S[kk] = vv
  241. }
  242. }
  243. item.CellData[k].Value = value
  244. }
  245. return
  246. }
  247. // 兼容前端js传递的数据类型
  248. func getIntValueByInterface(valInterface interface{}) (val int) {
  249. if valInterface == nil {
  250. return
  251. }
  252. switch reflect.TypeOf(valInterface).Kind() {
  253. case reflect.String:
  254. tmpValue := reflect.ValueOf(valInterface).String()
  255. tmpValInt, err := strconv.Atoi(tmpValue)
  256. if err != nil {
  257. val = 0
  258. } else {
  259. val = tmpValInt
  260. }
  261. case reflect.Int, reflect.Int32, reflect.Int64:
  262. tmpValue := reflect.ValueOf(valInterface).Int()
  263. val = int(tmpValue)
  264. }
  265. return
  266. }
  267. // TableData 表格数据
  268. type TableData struct {
  269. TableDataList [][]LuckySheetDataValue
  270. RowWidthList []float64
  271. RowHeightList []float64
  272. RemoveTopRow int `description:"移除表格上方的行数"`
  273. RemoveBottomRow int `description:"移除表格下方的行数"`
  274. RemoveLeftColumn int `description:"移除表格左侧的列数"`
  275. RemoveRightColumn int `description:"移除表格右侧的列数"`
  276. MergeList []TableDataMerge `description:"合并数据列"`
  277. }
  278. // TableDataMerge 表格数据合并单元格配置
  279. type TableDataMerge struct {
  280. StartRowIndex int `json:"start_row_index" description:"开始的行下标"`
  281. StartColumnIndex int `json:"start_column" description:"开始的列下标"`
  282. MergeRowNum int `json:"merge_row_num" description:"合并的行数"`
  283. MergeColumnNum int `json:"merge_column_num" description:"合并的列数"`
  284. }
  285. // GetTableDataByLuckySheetDataStr 通过LuckySheet的string数据获取表格数据
  286. func (item *LuckySheetData) GetTableDataByLuckySheetDataStr() (selfTableData TableData, err error) {
  287. luckySheetCellDataList := item.CellData
  288. // 表格数据
  289. tableDataMap := make(map[int64]map[int64]LuckySheetDataValue)
  290. // 最大行,最大列
  291. var maxRow, maxCol int64
  292. for _, v := range luckySheetCellDataList {
  293. //fmt.Println("row:", v.Row, "=====col:", v.Col)
  294. if v.Row > maxRow { //最大行
  295. maxRow = v.Row
  296. }
  297. if v.Col > maxCol { //最大列
  298. maxCol = v.Col
  299. }
  300. var tmpRow map[int64]LuckySheetDataValue
  301. tmpRow, ok := tableDataMap[v.Row]
  302. if ok {
  303. tmpRow[v.Col] = v.Value
  304. } else {
  305. tmpRow = make(map[int64]LuckySheetDataValue)
  306. tmpRow[v.Col] = v.Value
  307. }
  308. tableDataMap[v.Row] = tmpRow
  309. }
  310. tableDataList := make([][]LuckySheetDataValue, 0)
  311. var i int64
  312. // 单元格宽度
  313. configColumnConf := item.Config.Columnlen
  314. rowWidthMap := make(map[int]float64)
  315. rowWidthList := make([]float64, 0) //
  316. // 单元格高度
  317. configRowConf := item.Config.Rowlen
  318. rowHeightList := make([]float64, 0) //
  319. for i = 0; i <= maxRow; i++ {
  320. //列
  321. tmpTableColDataList := make([]LuckySheetDataValue, 0)
  322. // 每个单元格的高度
  323. tmpHeight, ok := configRowConf[fmt.Sprint(i)]
  324. if !ok {
  325. tmpHeight = 0
  326. }
  327. rowHeightList = append(rowHeightList, tmpHeight)
  328. tmpRowData, ok := tableDataMap[i]
  329. // 如果没有该行数据,那么就默认添加空行数据处理
  330. if !ok {
  331. tmpRowData = make(map[int64]LuckySheetDataValue)
  332. }
  333. var j int64
  334. for j = 0; j <= maxCol; j++ {
  335. tmpTableColData, ok := tmpRowData[j]
  336. if !ok {
  337. tmpTableColData = LuckySheetDataValue{}
  338. }
  339. //单元格显示的数据处理
  340. tmpTableColData.Monitor = handleCellVal(tmpTableColData)
  341. tmpTableColDataList = append(tmpTableColDataList, tmpTableColData)
  342. // 每个单元格的宽度
  343. tmpWidth, ok := configColumnConf[fmt.Sprint(j)]
  344. if !ok {
  345. tmpWidth = 0
  346. }
  347. rowIndex := len(tmpTableColDataList) - 1
  348. if _, ok2 := rowWidthMap[rowIndex]; !ok2 {
  349. rowWidthList = append(rowWidthList, tmpWidth)
  350. }
  351. rowWidthMap[rowIndex] = tmpWidth
  352. }
  353. tableDataList = append(tableDataList, tmpTableColDataList)
  354. }
  355. // 数据处理,移除上下左右空行空列
  356. tableDataList, tableRemoveNum, rowHeightList, rowWidthList, tableDataMergeList := handleTableDataList(tableDataList, item.Config.Merge, rowHeightList, rowWidthList)
  357. // 表格数据
  358. {
  359. // 移除空白单元格信息
  360. selfTableData.RemoveTopRow = tableRemoveNum.RemoveTopRow
  361. selfTableData.RemoveBottomRow = tableRemoveNum.RemoveBottomRow
  362. selfTableData.RemoveLeftColumn = tableRemoveNum.RemoveLeftColumn
  363. selfTableData.RemoveRightColumn = tableRemoveNum.RemoveRightColumn
  364. }
  365. selfTableData.TableDataList = tableDataList
  366. selfTableData.RowWidthList = rowWidthList
  367. selfTableData.RowHeightList = rowHeightList
  368. selfTableData.MergeList = tableDataMergeList
  369. return
  370. }
  371. // TableRemoveNum 上下左右移除的空行空列数量
  372. type TableRemoveNum struct {
  373. RemoveTopRow int `description:"移除表格上方的行数"`
  374. RemoveBottomRow int `description:"移除表格下方的行数"`
  375. RemoveLeftColumn int `description:"移除表格左侧的列数"`
  376. RemoveRightColumn int `description:"移除表格右侧的列数"`
  377. }
  378. // handleTableDataList 表格数据处理(移除上下左右的空行空列)
  379. func handleTableDataList(tableDataList [][]LuckySheetDataValue, luckySheetDataConfigMergeList map[string]LuckySheetDataConfigMerge, rowHeightList, rowWidthList []float64) ([][]LuckySheetDataValue, TableRemoveNum, []float64, []float64, []TableDataMerge) {
  380. var removeTopRow, removeBottomRow, removeLeftColumn, removeRightColumn int //上下左右需要移除的空行空列
  381. tableDataMergeList := make([]TableDataMerge, 0) //待合并的单元格信息
  382. //总共多少行
  383. lenRow := len(tableDataList)
  384. //移除上下空行
  385. deleteRowIndexList := make([]int, 0)
  386. if lenRow > 0 {
  387. var flag = false
  388. // 移除上方空列
  389. for rowIndex := 0; rowIndex < lenRow; rowIndex++ {
  390. isDelete := true
  391. for _, v := range tableDataList[rowIndex] {
  392. if v.Monitor != `` || (v.MergeCell.Row != rowIndex && v.MergeCell.Row != 0) {
  393. isDelete = false
  394. flag = true
  395. break
  396. }
  397. }
  398. if flag {
  399. break
  400. }
  401. if isDelete {
  402. removeTopRow++
  403. deleteRowIndexList = append(deleteRowIndexList, rowIndex)
  404. }
  405. }
  406. // 移除下方空行
  407. flag = false
  408. //尾部
  409. deleteBottomRowIndexList := make([]int, 0)
  410. for rowIndex := lenRow - 1; rowIndex >= 0; rowIndex-- {
  411. isDelete := true
  412. for _, v := range tableDataList[rowIndex] {
  413. if v.Monitor != `` {
  414. isDelete = false
  415. flag = true
  416. break
  417. }
  418. }
  419. if flag {
  420. break
  421. }
  422. if isDelete {
  423. deleteBottomRowIndexList = append(deleteBottomRowIndexList, rowIndex)
  424. removeBottomRow++
  425. }
  426. }
  427. if len(deleteBottomRowIndexList) > 0 {
  428. deleteRowIndexList = append(deleteRowIndexList, utils.RevSlice(deleteBottomRowIndexList)...)
  429. }
  430. lenDeleteRow := len(deleteRowIndexList)
  431. if lenDeleteRow > 0 {
  432. for rowIndex := lenDeleteRow - 1; rowIndex >= 0; rowIndex-- {
  433. //表格数据
  434. tableDataList = append(tableDataList[:deleteRowIndexList[rowIndex]], tableDataList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  435. //表格高度
  436. rowHeightList = append(rowHeightList[:deleteRowIndexList[rowIndex]], rowHeightList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  437. }
  438. }
  439. }
  440. //移除左右空列
  441. deleteColumnIndexList := make([]int, 0)
  442. if len(tableDataList) > 0 {
  443. var flag = false
  444. lenColumn := len(tableDataList[0])
  445. // 移除左边空列
  446. for columnIndex := 0; columnIndex < lenColumn; columnIndex++ {
  447. isDelete := true
  448. for _, v := range tableDataList {
  449. //如果一列都没有,说明是上面几行是空行,没有数据
  450. if len(v) <= 0 {
  451. continue
  452. }
  453. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  454. isDelete = false
  455. flag = true
  456. break
  457. }
  458. }
  459. if flag {
  460. break
  461. }
  462. if isDelete {
  463. removeLeftColumn++
  464. deleteColumnIndexList = append(deleteColumnIndexList, columnIndex)
  465. }
  466. }
  467. // 移除右方空列
  468. flag = false
  469. //右边
  470. deleteTailColumnIndexList := make([]int, 0)
  471. for columnIndex := lenColumn - 1; columnIndex >= 0; columnIndex-- {
  472. isDelete := true
  473. for _, v := range tableDataList {
  474. //如果一列都没有,说明是上面几行是空行,没有数据
  475. if len(v) <= 0 {
  476. continue
  477. }
  478. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  479. isDelete = false
  480. flag = true
  481. break
  482. }
  483. }
  484. if flag {
  485. break
  486. }
  487. if isDelete {
  488. deleteTailColumnIndexList = append(deleteTailColumnIndexList, columnIndex)
  489. removeRightColumn++
  490. }
  491. }
  492. if len(deleteTailColumnIndexList) > 0 {
  493. deleteColumnIndexList = append(deleteColumnIndexList, utils.RevSlice(deleteTailColumnIndexList)...)
  494. }
  495. lenDeleteColumn := len(deleteColumnIndexList)
  496. if lenDeleteColumn > 0 {
  497. for columnIndex := lenDeleteColumn - 1; columnIndex >= 0; columnIndex-- {
  498. //表格数据
  499. for k, v := range tableDataList {
  500. tableDataList[k] = append(v[:deleteColumnIndexList[columnIndex]], v[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  501. }
  502. //表格宽度
  503. rowWidthList = append(rowWidthList[:deleteColumnIndexList[columnIndex]], rowWidthList[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  504. }
  505. }
  506. }
  507. //处理合并单元格
  508. for _, v := range luckySheetDataConfigMergeList {
  509. indexRow := v.Row - removeTopRow
  510. indexColumn := v.Column - removeLeftColumn
  511. tableDataMerge := TableDataMerge{
  512. StartRowIndex: indexRow,
  513. StartColumnIndex: indexColumn,
  514. MergeRowNum: v.Rs - 1,
  515. MergeColumnNum: v.Cs - 1,
  516. }
  517. tableDataMergeList = append(tableDataMergeList, tableDataMerge)
  518. }
  519. return tableDataList, TableRemoveNum{
  520. RemoveTopRow: removeTopRow,
  521. RemoveBottomRow: removeBottomRow,
  522. RemoveLeftColumn: removeLeftColumn,
  523. RemoveRightColumn: removeRightColumn,
  524. }, rowHeightList, rowWidthList, tableDataMergeList
  525. }
  526. // handleCellVal 处理单元格数据
  527. func handleCellVal(tmpTableColData LuckySheetDataValue) (valueStr string) {
  528. valueStr = tmpTableColData.Monitor
  529. if valueStr == `` {
  530. //valueStr = fmt.Sprint(cellInfo.Value)
  531. if valueStr == `` && tmpTableColData.CellType.S != nil {
  532. //不是设置在单元格上面,而是设置在文本上
  533. for _, cellS := range tmpTableColData.CellType.S {
  534. valueStr += fmt.Sprint(cellS.Value)
  535. }
  536. }
  537. }
  538. return
  539. }
  540. type CellPosition struct {
  541. RowIndex int
  542. ColumnIndex int
  543. }
  544. // HandleTableCell 前端d毛需要我根据合并单元格处理掉多余的单元格
  545. func HandleTableCell(oldTableData TableData) (newTableData TableData) {
  546. newTableData = oldTableData
  547. mergeList := oldTableData.MergeList
  548. lenMergeList := len(mergeList)
  549. if lenMergeList <= 0 {
  550. return
  551. }
  552. deleteRowMap := make(map[int]map[int]int)
  553. deleteRowList := make([]int, 0)
  554. deleteColumnMap := make(map[int][]int, 0)
  555. for i := lenMergeList - 1; i >= 0; i-- {
  556. tmpMerge := mergeList[i]
  557. //
  558. for rowIndex := tmpMerge.MergeRowNum; rowIndex >= 0; rowIndex-- {
  559. tmpColumnMap := make(map[int]int)
  560. if columnMap, ok := deleteRowMap[tmpMerge.StartRowIndex+rowIndex]; ok {
  561. tmpColumnMap = columnMap
  562. } else {
  563. deleteRowList = append(deleteRowList, tmpMerge.StartRowIndex+rowIndex)
  564. }
  565. deleteColumnList := make([]int, 0)
  566. if columnList, ok := deleteColumnMap[tmpMerge.StartRowIndex+rowIndex]; ok {
  567. deleteColumnList = columnList
  568. }
  569. for columnIndex := tmpMerge.MergeColumnNum; columnIndex >= 0; columnIndex-- {
  570. if rowIndex == 0 && columnIndex == 0 {
  571. continue
  572. }
  573. tmpColumnMap[tmpMerge.StartColumnIndex+columnIndex] = tmpMerge.StartColumnIndex + columnIndex
  574. deleteColumnList = append(deleteColumnList, tmpMerge.StartColumnIndex+columnIndex)
  575. }
  576. // 待删除的行
  577. deleteRowMap[tmpMerge.StartRowIndex+rowIndex] = tmpColumnMap
  578. // 该行待删除的列
  579. deleteColumnMap[tmpMerge.StartRowIndex+rowIndex] = deleteColumnList
  580. }
  581. }
  582. sort.Ints(deleteRowList)
  583. //for k, v := range deleteRowList {
  584. // fmt.Println("k:", k, "v:", v)
  585. //}
  586. //return
  587. //fmt.Println("===============")
  588. for i := len(deleteRowList) - 1; i >= 0; i-- {
  589. rowIndex := deleteRowList[i]
  590. deleteColumnList := deleteColumnMap[rowIndex]
  591. sort.Ints(deleteColumnList)
  592. for i := len(deleteColumnList) - 1; i >= 0; i-- {
  593. columnIndex := deleteColumnList[i]
  594. tmpColumnDataList := newTableData.TableDataList[rowIndex]
  595. newTableData.TableDataList[rowIndex] = append(tmpColumnDataList[:columnIndex], tmpColumnDataList[columnIndex+1:]...) // 删除开头N个元素
  596. //fmt.Println("row:", rowIndex, "===column:", columnIndex)
  597. }
  598. }
  599. return
  600. }
  601. // GetTableDataByLuckySheetDataStrBak 通过LuckySheet的string数据获取表格数据(备份:2022-08-23 10:30:32)
  602. func (item *LuckySheetData) GetTableDataByLuckySheetDataStrBak() (selfTableData TableData, err error) {
  603. luckySheetCellDataList := item.CellData
  604. // 表格数据
  605. tableDataMap := make(map[int64]map[int64]LuckySheetDataValue)
  606. // 最大行,最大列
  607. var maxRow, maxCol int64
  608. for _, v := range luckySheetCellDataList {
  609. //fmt.Println("row:", v.Row, "=====col:", v.Col)
  610. if v.Row > maxRow { //最大行
  611. maxRow = v.Row
  612. }
  613. if v.Col > maxCol { //最大列
  614. maxCol = v.Col
  615. }
  616. var tmpRow map[int64]LuckySheetDataValue
  617. tmpRow, ok := tableDataMap[v.Row]
  618. if ok {
  619. tmpRow[v.Col] = v.Value
  620. } else {
  621. tmpRow = make(map[int64]LuckySheetDataValue)
  622. tmpRow[v.Col] = v.Value
  623. }
  624. tableDataMap[v.Row] = tmpRow
  625. }
  626. tableDataList := make([][]LuckySheetDataValue, 0)
  627. var i int64
  628. // 单元格宽度
  629. configColumnConf := item.Config.Columnlen
  630. rowWidthMap := make(map[int]float64)
  631. rowWidthList := make([]float64, 0) //
  632. // 单元格高度
  633. configRowConf := item.Config.Rowlen
  634. rowHeightList := make([]float64, 0) //
  635. for i = 0; i <= maxRow; i++ {
  636. //列
  637. tmpTableColDataList := make([]LuckySheetDataValue, 0)
  638. // 每个单元格的高度
  639. tmpHeight, ok := configRowConf[fmt.Sprint(i)]
  640. if !ok {
  641. tmpHeight = 0
  642. }
  643. rowHeightList = append(rowHeightList, tmpHeight)
  644. tmpRowData, ok := tableDataMap[i]
  645. // 如果没有该行数据,那么就默认添加空行数据处理
  646. if !ok {
  647. tmpRowData = make(map[int64]LuckySheetDataValue)
  648. }
  649. var j int64
  650. for j = 0; j <= maxCol; j++ {
  651. tmpTableColData, ok := tmpRowData[j]
  652. if !ok {
  653. tmpTableColData = LuckySheetDataValue{}
  654. }
  655. //单元格显示的数据处理
  656. tmpTableColData.Monitor = handleCellVal(tmpTableColData)
  657. tmpTableColDataList = append(tmpTableColDataList, tmpTableColData)
  658. // 每个单元格的宽度
  659. tmpWidth, ok := configColumnConf[fmt.Sprint(j)]
  660. if !ok {
  661. tmpWidth = 0
  662. }
  663. rowIndex := len(tmpTableColDataList) - 1
  664. if _, ok2 := rowWidthMap[rowIndex]; !ok2 {
  665. rowWidthList = append(rowWidthList, tmpWidth)
  666. }
  667. rowWidthMap[rowIndex] = tmpWidth
  668. }
  669. tableDataList = append(tableDataList, tmpTableColDataList)
  670. }
  671. //总共多少行
  672. lenRow := len(tableDataList)
  673. //移除上下空行
  674. deleteRowIndexList := make([]int, 0)
  675. if lenRow > 0 {
  676. var flag = false
  677. // 移除上方空列
  678. for rowIndex := 0; rowIndex < lenRow; rowIndex++ {
  679. isDelete := true
  680. for _, v := range tableDataList[rowIndex] {
  681. if v.Monitor != `` {
  682. isDelete = false
  683. flag = true
  684. break
  685. }
  686. }
  687. if flag {
  688. break
  689. }
  690. if isDelete {
  691. selfTableData.RemoveTopRow++
  692. deleteRowIndexList = append(deleteRowIndexList, rowIndex)
  693. }
  694. }
  695. // 移除下方空行
  696. flag = false
  697. //尾部
  698. deleteBottomRowIndexList := make([]int, 0)
  699. for rowIndex := lenRow - 1; rowIndex >= 0; rowIndex-- {
  700. isDelete := true
  701. for _, v := range tableDataList[rowIndex] {
  702. if v.Monitor != `` || (v.MergeCell.Row != rowIndex && v.MergeCell.Row != 0) {
  703. isDelete = false
  704. flag = true
  705. break
  706. }
  707. }
  708. if flag {
  709. break
  710. }
  711. if isDelete {
  712. deleteBottomRowIndexList = append(deleteBottomRowIndexList, rowIndex)
  713. selfTableData.RemoveBottomRow++
  714. }
  715. }
  716. if len(deleteBottomRowIndexList) > 0 {
  717. deleteRowIndexList = append(deleteRowIndexList, utils.RevSlice(deleteBottomRowIndexList)...)
  718. }
  719. lenDeleteRow := len(deleteRowIndexList)
  720. if lenDeleteRow > 0 {
  721. for rowIndex := lenDeleteRow - 1; rowIndex >= 0; rowIndex-- {
  722. //表格数据
  723. tableDataList = append(tableDataList[:deleteRowIndexList[rowIndex]], tableDataList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  724. //表格高度
  725. rowHeightList = append(rowHeightList[:deleteRowIndexList[rowIndex]], rowHeightList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  726. }
  727. }
  728. }
  729. //移除前后空列
  730. deleteColumnIndexList := make([]int, 0)
  731. if len(tableDataList) > 0 {
  732. var flag = false
  733. lenColumn := len(tableDataList[0])
  734. // 移除前方空列
  735. for columnIndex := 0; columnIndex < lenColumn; columnIndex++ {
  736. isDelete := true
  737. for _, v := range tableDataList {
  738. //如果一列都没有,说明是上面几行是空行,没有数据
  739. if len(v) <= 0 {
  740. continue
  741. }
  742. if v[columnIndex].Monitor != `` {
  743. isDelete = false
  744. flag = true
  745. break
  746. }
  747. }
  748. if flag {
  749. break
  750. }
  751. if isDelete {
  752. selfTableData.RemoveLeftColumn++
  753. deleteColumnIndexList = append(deleteColumnIndexList, columnIndex)
  754. }
  755. }
  756. // 移除后方空列
  757. flag = false
  758. //后方
  759. deleteTailColumnIndexList := make([]int, 0)
  760. for columnIndex := lenColumn - 1; columnIndex >= 0; columnIndex-- {
  761. isDelete := true
  762. for _, v := range tableDataList {
  763. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  764. isDelete = false
  765. flag = true
  766. break
  767. }
  768. }
  769. if flag {
  770. break
  771. }
  772. if isDelete {
  773. deleteTailColumnIndexList = append(deleteTailColumnIndexList, columnIndex)
  774. selfTableData.RemoveRightColumn++
  775. }
  776. }
  777. if len(deleteTailColumnIndexList) > 0 {
  778. deleteColumnIndexList = append(deleteColumnIndexList, utils.RevSlice(deleteTailColumnIndexList)...)
  779. }
  780. lenDeleteColumn := len(deleteColumnIndexList)
  781. if lenDeleteColumn > 0 {
  782. for columnIndex := lenDeleteColumn - 1; columnIndex >= 0; columnIndex-- {
  783. //表格数据
  784. for k, v := range tableDataList {
  785. tableDataList[k] = append(v[:deleteColumnIndexList[columnIndex]], v[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  786. }
  787. //表格宽度
  788. rowWidthList = append(rowWidthList[:deleteColumnIndexList[columnIndex]], rowWidthList[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  789. }
  790. }
  791. }
  792. //处理合并单元格
  793. tableDataMergeList := make([]TableDataMerge, 0)
  794. for _, v := range item.Config.Merge {
  795. indexRow := v.Row - selfTableData.RemoveTopRow
  796. indexColumn := v.Column - selfTableData.RemoveLeftColumn
  797. tableDataMerge := TableDataMerge{
  798. StartRowIndex: indexRow,
  799. StartColumnIndex: indexColumn,
  800. MergeRowNum: v.Rs - 1,
  801. MergeColumnNum: v.Cs - 1,
  802. }
  803. tableDataMergeList = append(tableDataMergeList, tableDataMerge)
  804. }
  805. // 表格数据
  806. selfTableData.TableDataList = tableDataList
  807. selfTableData.RowWidthList = rowWidthList
  808. selfTableData.RowHeightList = rowHeightList
  809. selfTableData.MergeList = tableDataMergeList
  810. return
  811. }