lucky_sheet.go 35 KB

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