lucky_sheet.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. package excel
  2. import (
  3. "encoding/json"
  4. "eta/eta_chart_lib/models/request"
  5. "eta/eta_chart_lib/utils"
  6. "fmt"
  7. "reflect"
  8. "sort"
  9. "strconv"
  10. )
  11. type LuckySheetDataBak struct {
  12. CalcChain []interface{} `json:"calcChain" description:"公式链"`
  13. CellData []LuckySheetCellData `json:"celldata" description:"单元格数据"`
  14. ChWidth int64 `json:"ch_width" description:"工作表区域的宽度"`
  15. Config struct {
  16. BorderInfo []struct {
  17. BorderType string `json:"borderType" description:""`
  18. Color string `json:"color" description:""`
  19. Range []struct {
  20. Column []int64 `json:"column" description:""`
  21. Row []int64 `json:"row" description:""`
  22. } `json:"range" description:""`
  23. RangeType string `json:"rangeType" description:""`
  24. Style string `json:"style" description:""`
  25. Value struct {
  26. B struct {
  27. Color string `json:"color" description:""`
  28. Style string `json:"style" description:""`
  29. } `json:"b" description:""`
  30. ColIndex int64 `json:"col_index" description:""`
  31. L struct {
  32. Color string `json:"color" description:""`
  33. Style string `json:"style" description:""`
  34. } `json:"l" description:""`
  35. R struct {
  36. Color string `json:"color" description:""`
  37. Style string `json:"style" description:""`
  38. } `json:"r" description:""`
  39. RowIndex int64 `json:"row_index" description:""`
  40. T struct {
  41. Color string `json:"color" description:""`
  42. Style string `json:"style" description:""`
  43. } `json:"t" description:""`
  44. } `json:"value" description:"" description:""`
  45. } `json:"borderInfo" description:""`
  46. Colhidden struct{} `json:"colhidden" description:""`
  47. Columnlen map[string]float64 `json:"columnlen" description:""`
  48. CustomHeight struct {
  49. Zero int64 `json:"0"`
  50. } `json:"customHeight" description:""`
  51. CustomWidth struct {
  52. Two int64 `json:"2" description:""`
  53. } `json:"customWidth" description:""`
  54. Merge struct{} `json:"merge" description:""`
  55. Rowlen map[string]float64 `json:"rowlen" description:""`
  56. } `json:"config" description:""`
  57. Data [][]struct {
  58. Ct struct {
  59. Fa string `json:"fa"`
  60. T string `json:"t"`
  61. } `json:"ct"`
  62. M string `json:"m"`
  63. V interface{} `json:"v"`
  64. } `json:"data" description:""`
  65. DataVerification struct{} `json:"dataVerification" description:""`
  66. Filter interface{} `json:"filter" description:""`
  67. FilterSelect interface{} `json:"filter_select" description:""`
  68. Hyperlink struct{} `json:"hyperlink" description:""`
  69. Images struct{} `json:"images" description:""`
  70. Index string `json:"index" description:""`
  71. JfgirdSelectSave []interface{} `json:"jfgird_select_save" description:""`
  72. LuckysheetAlternateformatSave []interface{} `json:"luckysheet_alternateformat_save" description:""`
  73. LuckysheetConditionformatSave []interface{} `json:"luckysheet_conditionformat_save" description:""`
  74. LuckysheetSelectSave []struct {
  75. Column []int64 `json:"column" description:""`
  76. ColumnFocus int64 `json:"column_focus" description:""`
  77. Height int64 `json:"height" description:""`
  78. HeightMove int64 `json:"height_move" description:""`
  79. Left int64 `json:"left" description:""`
  80. LeftMove int64 `json:"left_move" description:""`
  81. Row []int64 `json:"row" description:""`
  82. RowFocus int64 `json:"row_focus" description:""`
  83. Top int64 `json:"top" description:""`
  84. TopMove int64 `json:"top_move" description:""`
  85. Width int64 `json:"width" description:""`
  86. WidthMove int64 `json:"width_move" description:""`
  87. } `json:"luckysheet_select_save" description:"" description:""`
  88. LuckysheetSelectionRange []struct {
  89. Column []int64 `json:"column" description:""`
  90. Row []int64 `json:"row" description:""`
  91. } `json:"luckysheet_selection_range" description:""`
  92. RhHeight float64 `json:"rh_height" description:""`
  93. ScrollLeft float64 `json:"scrollLeft" description:""`
  94. ScrollTop float64 `json:"scrollTop" description:""`
  95. Status int64 `json:"status" description:""`
  96. Visibledatacolumn []int64 `json:"visibledatacolumn" description:""`
  97. Visibledatarow []int64 `json:"visibledatarow" description:""`
  98. ZoomRatio float64 `json:"zoomRatio" description:"sheet缩放比例"`
  99. }
  100. // LuckySheetData sheet表格数据
  101. type LuckySheetData struct {
  102. CellData []LuckySheetCellData `json:"celldata" description:"单元格数据"`
  103. ChWidth int64 `json:"ch_width" description:"工作表区域的宽度"`
  104. Config LuckySheetDataConfig `json:"config" description:""`
  105. //Index int `json:"index" description:"工作表索引"`
  106. RhHeight float64 `json:"rh_height" description:"工作表区域的高度"`
  107. ScrollLeft float64 `json:"scrollLeft" description:"左右滚动条位置"`
  108. ScrollTop float64 `json:"scrollTop" description:"上下滚动条位置"`
  109. Status interface{} `json:"status" description:"激活状态"`
  110. VisibleDataColumn []int64 `json:"visibledatacolumn" description:"所有列的位置信息,递增的列位置数据,初始化无需设置"`
  111. VisibleDataRow []int64 `json:"visibledatarow" description:"所有行的位置信息,递增的行位置数据,初始化无需设置"`
  112. ZoomRatio float64 `json:"zoomRatio" description:"sheet缩放比例"`
  113. }
  114. // LuckySheetDataConfig sheet表单的配置
  115. type LuckySheetDataConfig struct {
  116. BorderInfo []LuckySheetDataConfigBorderInfo `json:"borderInfo" description:"边框"`
  117. Colhidden map[string]int64 `json:"colhidden" description:"隐藏列,示例值:\"colhidden\":{\"30\":0,\"31\":0}"`
  118. Columnlen map[string]float64 `json:"columnlen" description:"每个单元格的列宽"`
  119. //CustomHeight struct {
  120. // Zero int64 `json:"0"`
  121. //} `json:"customHeight" description:""`
  122. //CustomWidth struct {
  123. // Two int64 `json:"2" description:""`
  124. //} `json:"customWidth" description:""`
  125. Merge map[string]LuckySheetDataConfigMerge `json:"merge" description:"合并单元格"`
  126. Rowlen map[string]float64 `json:"rowlen" description:"每个单元格的行高"`
  127. }
  128. // LuckySheetDataConfigMerge 合并单元格设置
  129. type LuckySheetDataConfigMerge struct {
  130. Row int `json:"r" description:"行数"`
  131. Column int `json:"c" description:"列数"`
  132. Rs int `json:"rs" description:"合并的行数"`
  133. Cs int `json:"cs" description:"合并的列数"`
  134. }
  135. // LuckySheetDataConfigBorderInfo 单元格边框信息
  136. type LuckySheetDataConfigBorderInfo struct {
  137. 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"`
  138. Color string `json:"color" description:"边框颜色 color: 16进制颜色值"`
  139. Range []struct {
  140. Column []int64 `json:"column" description:"行"`
  141. Row []int64 `json:"row" description:"列"`
  142. } `json:"range" description:"选区范围 range: 行列信息数组"`
  143. RangeType string `json:"rangeType" description:"选区 rangeType: range | cell "`
  144. 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"`
  145. Value LuckySheetDataConfigBorderInfoCellValue `json:"value" description:"" description:"范围类型分单个单元格的数据"`
  146. }
  147. // LuckySheetDataConfigBorderInfoCellValue 单元格边框信息(范围类型为:单个单元格)
  148. type LuckySheetDataConfigBorderInfoCellValue struct {
  149. B LuckySheetDataConfigBorderInfoCell `json:"b" description:"下边框"`
  150. L LuckySheetDataConfigBorderInfoCell `json:"l" description:"左边框"`
  151. R LuckySheetDataConfigBorderInfoCell `json:"r" description:"右边框"`
  152. T LuckySheetDataConfigBorderInfoCell `json:"t" description:"上边框"`
  153. ColIndex int64 `json:"col_index" description:"第几行"`
  154. RowIndex int64 `json:"row_index" description:"第几列"`
  155. }
  156. // LuckySheetDataConfigBorderInfoCell 单元格边框信息(cell类型)
  157. type LuckySheetDataConfigBorderInfoCell struct {
  158. Color string `json:"color" description:"边框颜色 color: 16进制颜色值"`
  159. 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"`
  160. 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"`
  161. }
  162. // LuckySheetCellData 单个单元格数据
  163. type LuckySheetCellData struct {
  164. Col int64 `json:"c" description:"列"`
  165. Row int64 `json:"r" description:"行"`
  166. Value LuckySheetDataValue `json:"v" description:"单元格内值的数据"`
  167. }
  168. // LuckySheetDataValue 单元格内值的数据
  169. type LuckySheetDataValue struct {
  170. CellType LuckySheetDataCellType `json:"ct" description:"单元格值格式:文本、时间等 "`
  171. Value interface{} `json:"v" description:"原始值"`
  172. Monitor string `json:"m" description:"显示值"`
  173. Background string `json:"bg" description:"背景色,实例值:#fff000"`
  174. 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 华文隶书 "`
  175. 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 华文隶书 "`
  176. FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
  177. Bold int `json:"bl" description:"粗体,0 常规 、 1加粗 "`
  178. Italic int `json:"it" description:"斜体,0 常规 、 1 斜体"`
  179. Fontsize int `description:"字体大小,14"`
  180. CancelLine int ` description:"删除线, 0 常规 、 1 删除线"`
  181. HorizontalType int `description:"水平对齐, 0 居中、1 左、2右"`
  182. VerticalType int ` description:"垂直对齐, 0 中间、1 上、2下"`
  183. Fs interface{} `json:"fs" description:"字体大小,14"`
  184. Cl interface{} `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
  185. Ht interface{} `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
  186. Vt interface{} `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
  187. //TextRotate string `json:"tr" description:"竖排文字, 3"`
  188. //RotateText string `json:"rt" description:"文字旋转角度, 介于0~180之间的整数,包含0和180"`
  189. TextBeak int `description:"文本换行, 0 截断、1溢出、2 自动换行"`
  190. Tb interface{} `json:"tb" description:"文本换行, 0 截断、1溢出、2 自动换行"`
  191. Ps LuckySheetDataCellComment `json:"ps" description:"批注"`
  192. Function string `json:"f" description:"公式"`
  193. MergeCell LuckySheetDataConfigMerge `json:"mc" description:"合并单元格信息"`
  194. }
  195. // LuckySheetDataCellType 单元格值格式:文本、时间等
  196. type LuckySheetDataCellType struct {
  197. Fa string `json:"fa" description:"格式名称,例如:“General”为自动格式"`
  198. T string `json:"t" description:"格式类型,例如:“n”为数字类型"`
  199. S []struct {
  200. 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 华文隶书 "`
  201. 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 华文隶书 "`
  202. FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
  203. Fontsize int `description:"字体大小,14"`
  204. CancelLine int ` description:"删除线, 0 常规 、 1 删除线"`
  205. HorizontalType int `description:"水平对齐, 0 居中、1 左、2右"`
  206. VerticalType int `description:"垂直对齐, 0 中间、1 上、2下"`
  207. Fs interface{} `json:"fs" description:"字体大小,14"`
  208. Cl interface{} `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
  209. Ht interface{} `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
  210. Vt interface{} `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
  211. Un interface{} `json:"un" description:""`
  212. Bold interface{} `json:"bl" description:"粗体,0 常规 、 1加粗 "`
  213. Italic interface{} `json:"it" description:"斜体,0 常规 、 1 斜体"`
  214. Value interface{} `json:"v" description:"原始值"`
  215. } `json:"s"`
  216. }
  217. // LuckySheetDataCellComment 批注
  218. type LuckySheetDataCellComment struct {
  219. Left int `json:"left" description:"批注框距离左边工作表边缘位置"`
  220. Top int `json:"top" description:"批注框距离上边工作表边缘位置"`
  221. Width int `json:"width" description:"批注框宽度"`
  222. Height int `json:"height" description:"批注框高度"`
  223. Value string `json:"value" description:"批注内容"`
  224. IsShow bool `json:"isshow" description:"是否显示批注"`
  225. }
  226. // GetLuckySheetData 获取LuckySheetData的结构体
  227. func GetLuckySheetData(jsonStr string) (item *LuckySheetData, err error) {
  228. err = json.Unmarshal([]byte(jsonStr), &item)
  229. for k, v := range item.CellData {
  230. value := v.Value
  231. value.Fontsize = getIntValueByInterface(value.Fs)
  232. value.CancelLine = getIntValueByInterface(value.Cl)
  233. value.HorizontalType = getIntValueByInterface(value.Ht)
  234. value.VerticalType = getIntValueByInterface(value.Vt)
  235. value.FontFamily = getIntValueByInterface(value.FF)
  236. value.TextBeak = getIntValueByInterface(value.Tb)
  237. if len(value.CellType.S) > 0 {
  238. for kk, vv := range value.CellType.S {
  239. vv.Fontsize = getIntValueByInterface(vv.Fs)
  240. vv.CancelLine = getIntValueByInterface(vv.Cl)
  241. vv.HorizontalType = getIntValueByInterface(vv.Ht)
  242. vv.VerticalType = getIntValueByInterface(vv.Vt)
  243. vv.FontFamily = getIntValueByInterface(vv.FF)
  244. value.CellType.S[kk] = vv
  245. }
  246. }
  247. item.CellData[k].Value = value
  248. }
  249. //边框
  250. if len(item.Config.BorderInfo) > 0 {
  251. for k, v := range item.Config.BorderInfo {
  252. v.Value.T.Style = getIntValueByInterface(v.Value.T.Style)
  253. v.Value.B.Style = getIntValueByInterface(v.Value.B.Style)
  254. v.Value.L.Style = getIntValueByInterface(v.Value.L.Style)
  255. v.Value.R.Style = getIntValueByInterface(v.Value.R.Style)
  256. item.Config.BorderInfo[k] = v
  257. }
  258. }
  259. return
  260. }
  261. // 兼容前端js传递的数据类型
  262. func getIntValueByInterface(valInterface interface{}) (val int) {
  263. if valInterface == nil {
  264. return
  265. }
  266. switch reflect.TypeOf(valInterface).Kind() {
  267. case reflect.String:
  268. tmpValue := reflect.ValueOf(valInterface).String()
  269. tmpValInt, err := strconv.Atoi(tmpValue)
  270. if err != nil {
  271. val = 0
  272. } else {
  273. val = tmpValInt
  274. }
  275. case reflect.Int, reflect.Int32, reflect.Int64:
  276. tmpValue := reflect.ValueOf(valInterface).Int()
  277. val = int(tmpValue)
  278. }
  279. return
  280. }
  281. // TableData 表格数据
  282. type TableData struct {
  283. TableDataList [][]LuckySheetDataValue
  284. RowWidthList []float64
  285. RowHeightList []float64
  286. RemoveTopRow int `description:"移除表格上方的行数"`
  287. RemoveBottomRow int `description:"移除表格下方的行数"`
  288. RemoveLeftColumn int `description:"移除表格左侧的列数"`
  289. RemoveRightColumn int `description:"移除表格右侧的列数"`
  290. MergeList []TableDataMerge `description:"合并数据列"`
  291. }
  292. // TableDataMerge 表格数据合并单元格配置
  293. type TableDataMerge struct {
  294. StartRowIndex int `json:"start_row_index" description:"开始的行下标"`
  295. StartColumnIndex int `json:"start_column" description:"开始的列下标"`
  296. MergeRowNum int `json:"merge_row_num" description:"合并的行数"`
  297. MergeColumnNum int `json:"merge_column_num" description:"合并的列数"`
  298. }
  299. // GetTableDataByLuckySheetDataStr 通过LuckySheet的string数据获取表格数据
  300. func (item *LuckySheetData) GetTableDataByLuckySheetDataStr() (selfTableData TableData, err error) {
  301. luckySheetCellDataList := item.CellData
  302. // 表格数据
  303. tableDataMap := make(map[int64]map[int64]LuckySheetDataValue)
  304. // 最大行,最大列
  305. var maxRow, maxCol int64
  306. for _, v := range luckySheetCellDataList {
  307. //fmt.Println("row:", v.Row, "=====col:", v.Col)
  308. if v.Row > maxRow { //最大行
  309. maxRow = v.Row
  310. }
  311. if v.Col > maxCol { //最大列
  312. maxCol = v.Col
  313. }
  314. var tmpRow map[int64]LuckySheetDataValue
  315. tmpRow, ok := tableDataMap[v.Row]
  316. if ok {
  317. tmpRow[v.Col] = v.Value
  318. } else {
  319. tmpRow = make(map[int64]LuckySheetDataValue)
  320. tmpRow[v.Col] = v.Value
  321. }
  322. tableDataMap[v.Row] = tmpRow
  323. }
  324. tableDataList := make([][]LuckySheetDataValue, 0)
  325. var i int64
  326. // 单元格宽度
  327. configColumnConf := item.Config.Columnlen
  328. rowWidthMap := make(map[int]float64)
  329. rowWidthList := make([]float64, 0) //
  330. // 单元格高度
  331. configRowConf := item.Config.Rowlen
  332. rowHeightList := make([]float64, 0) //
  333. for i = 0; i <= maxRow; i++ {
  334. //列
  335. tmpTableColDataList := make([]LuckySheetDataValue, 0)
  336. // 每个单元格的高度
  337. tmpHeight, ok := configRowConf[fmt.Sprint(i)]
  338. if !ok {
  339. tmpHeight = 0
  340. }
  341. rowHeightList = append(rowHeightList, tmpHeight)
  342. tmpRowData, ok := tableDataMap[i]
  343. // 如果没有该行数据,那么就默认添加空行数据处理
  344. if !ok {
  345. tmpRowData = make(map[int64]LuckySheetDataValue)
  346. }
  347. var j int64
  348. for j = 0; j <= maxCol; j++ {
  349. tmpTableColData, ok := tmpRowData[j]
  350. if !ok {
  351. tmpTableColData = LuckySheetDataValue{}
  352. }
  353. //单元格显示的数据处理
  354. tmpTableColData.Monitor = handleCellVal(tmpTableColData)
  355. tmpTableColDataList = append(tmpTableColDataList, tmpTableColData)
  356. // 每个单元格的宽度
  357. tmpWidth, ok := configColumnConf[fmt.Sprint(j)]
  358. if !ok {
  359. tmpWidth = 0
  360. }
  361. rowIndex := len(tmpTableColDataList) - 1
  362. if _, ok2 := rowWidthMap[rowIndex]; !ok2 {
  363. rowWidthList = append(rowWidthList, tmpWidth)
  364. }
  365. rowWidthMap[rowIndex] = tmpWidth
  366. }
  367. tableDataList = append(tableDataList, tmpTableColDataList)
  368. }
  369. // 数据处理,移除上下左右空行空列
  370. tableDataList, tableRemoveNum, rowHeightList, rowWidthList, tableDataMergeList := handleTableDataList(tableDataList, item.Config.Merge, rowHeightList, rowWidthList)
  371. // 表格数据
  372. {
  373. // 移除空白单元格信息
  374. selfTableData.RemoveTopRow = tableRemoveNum.RemoveTopRow
  375. selfTableData.RemoveBottomRow = tableRemoveNum.RemoveBottomRow
  376. selfTableData.RemoveLeftColumn = tableRemoveNum.RemoveLeftColumn
  377. selfTableData.RemoveRightColumn = tableRemoveNum.RemoveRightColumn
  378. }
  379. selfTableData.TableDataList = tableDataList
  380. selfTableData.RowWidthList = rowWidthList
  381. selfTableData.RowHeightList = rowHeightList
  382. selfTableData.MergeList = tableDataMergeList
  383. return
  384. }
  385. // TableRemoveNum 上下左右移除的空行空列数量
  386. type TableRemoveNum struct {
  387. RemoveTopRow int `description:"移除表格上方的行数"`
  388. RemoveBottomRow int `description:"移除表格下方的行数"`
  389. RemoveLeftColumn int `description:"移除表格左侧的列数"`
  390. RemoveRightColumn int `description:"移除表格右侧的列数"`
  391. }
  392. // handleTableDataList 表格数据处理(移除上下左右的空行空列)
  393. func handleTableDataList(tableDataList [][]LuckySheetDataValue, luckySheetDataConfigMergeList map[string]LuckySheetDataConfigMerge, rowHeightList, rowWidthList []float64) ([][]LuckySheetDataValue, TableRemoveNum, []float64, []float64, []TableDataMerge) {
  394. var removeTopRow, removeBottomRow, removeLeftColumn, removeRightColumn int //上下左右需要移除的空行空列
  395. tableDataMergeList := make([]TableDataMerge, 0) //待合并的单元格信息
  396. //总共多少行
  397. lenRow := len(tableDataList)
  398. //移除上下空行
  399. deleteRowIndexList := make([]int, 0)
  400. if lenRow > 0 {
  401. var flag = false
  402. // 移除上方空列
  403. for rowIndex := 0; rowIndex < lenRow; rowIndex++ {
  404. isDelete := true
  405. for _, v := range tableDataList[rowIndex] {
  406. if v.Monitor != `` || (v.MergeCell.Row != rowIndex && v.MergeCell.Row != 0) {
  407. isDelete = false
  408. flag = true
  409. break
  410. }
  411. }
  412. if flag {
  413. break
  414. }
  415. if isDelete {
  416. removeTopRow++
  417. deleteRowIndexList = append(deleteRowIndexList, rowIndex)
  418. }
  419. }
  420. // 移除下方空行
  421. flag = false
  422. //尾部
  423. deleteBottomRowIndexList := make([]int, 0)
  424. if len(tableDataList) > 1 {
  425. for rowIndex := lenRow - 1; rowIndex >= 0; rowIndex-- {
  426. isDelete := true
  427. for _, v := range tableDataList[rowIndex] {
  428. if v.Monitor != `` {
  429. isDelete = false
  430. flag = true
  431. break
  432. }
  433. }
  434. if flag {
  435. break
  436. }
  437. if isDelete {
  438. deleteBottomRowIndexList = append(deleteBottomRowIndexList, rowIndex)
  439. removeBottomRow++
  440. }
  441. }
  442. }
  443. if len(deleteBottomRowIndexList) > 0 {
  444. deleteRowIndexList = append(deleteRowIndexList, utils.RevSlice(deleteBottomRowIndexList)...)
  445. }
  446. lenDeleteRow := len(deleteRowIndexList)
  447. if lenDeleteRow > 0 {
  448. for rowIndex := lenDeleteRow - 1; rowIndex >= 0; rowIndex-- {
  449. //表格数据
  450. tableDataList = append(tableDataList[:deleteRowIndexList[rowIndex]], tableDataList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  451. //表格高度
  452. rowHeightList = append(rowHeightList[:deleteRowIndexList[rowIndex]], rowHeightList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  453. }
  454. }
  455. }
  456. //移除左右空列
  457. deleteColumnIndexList := make([]int, 0)
  458. if len(tableDataList) > 0 {
  459. var flag = false
  460. lenColumn := len(tableDataList[0])
  461. // 移除左边空列
  462. for columnIndex := 0; columnIndex < lenColumn; columnIndex++ {
  463. isDelete := true
  464. for _, v := range tableDataList {
  465. //如果一列都没有,说明是上面几行是空行,没有数据
  466. if len(v) <= 0 {
  467. continue
  468. }
  469. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  470. isDelete = false
  471. flag = true
  472. break
  473. }
  474. }
  475. if flag {
  476. break
  477. }
  478. if isDelete {
  479. removeLeftColumn++
  480. deleteColumnIndexList = append(deleteColumnIndexList, columnIndex)
  481. }
  482. }
  483. // 移除右方空列
  484. flag = false
  485. //右边
  486. deleteTailColumnIndexList := make([]int, 0)
  487. // 数据要大于1列才会处理
  488. if lenColumn > 1 {
  489. for columnIndex := lenColumn - 1; columnIndex >= 0; columnIndex-- {
  490. isDelete := true
  491. for _, v := range tableDataList {
  492. //如果一列都没有,说明是上面几行是空行,没有数据
  493. if len(v) <= 0 {
  494. continue
  495. }
  496. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  497. isDelete = false
  498. flag = true
  499. break
  500. }
  501. }
  502. if flag {
  503. break
  504. }
  505. if isDelete {
  506. deleteTailColumnIndexList = append(deleteTailColumnIndexList, columnIndex)
  507. removeRightColumn++
  508. }
  509. }
  510. }
  511. if len(deleteTailColumnIndexList) > 0 {
  512. deleteColumnIndexList = append(deleteColumnIndexList, utils.RevSlice(deleteTailColumnIndexList)...)
  513. }
  514. lenDeleteColumn := len(deleteColumnIndexList)
  515. if lenDeleteColumn > 0 {
  516. for columnIndex := lenDeleteColumn - 1; columnIndex >= 0; columnIndex-- {
  517. //表格数据
  518. for k, v := range tableDataList {
  519. tableDataList[k] = append(v[:deleteColumnIndexList[columnIndex]], v[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  520. }
  521. //表格宽度
  522. rowWidthList = append(rowWidthList[:deleteColumnIndexList[columnIndex]], rowWidthList[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  523. }
  524. }
  525. }
  526. //处理合并单元格
  527. for _, v := range luckySheetDataConfigMergeList {
  528. indexRow := v.Row - removeTopRow
  529. indexColumn := v.Column - removeLeftColumn
  530. tableDataMerge := TableDataMerge{
  531. StartRowIndex: indexRow,
  532. StartColumnIndex: indexColumn,
  533. MergeRowNum: v.Rs - 1,
  534. MergeColumnNum: v.Cs - 1,
  535. }
  536. tableDataMergeList = append(tableDataMergeList, tableDataMerge)
  537. }
  538. return tableDataList, TableRemoveNum{
  539. RemoveTopRow: removeTopRow,
  540. RemoveBottomRow: removeBottomRow,
  541. RemoveLeftColumn: removeLeftColumn,
  542. RemoveRightColumn: removeRightColumn,
  543. }, rowHeightList, rowWidthList, tableDataMergeList
  544. }
  545. // handleCellVal 处理单元格数据
  546. func handleCellVal(tmpTableColData LuckySheetDataValue) (valueStr string) {
  547. valueStr = tmpTableColData.Monitor
  548. if valueStr == `` {
  549. //valueStr = fmt.Sprint(cellInfo.Value)
  550. if valueStr == `` && tmpTableColData.CellType.S != nil {
  551. //不是设置在单元格上面,而是设置在文本上
  552. for _, cellS := range tmpTableColData.CellType.S {
  553. valueStr += fmt.Sprint(cellS.Value)
  554. }
  555. }
  556. }
  557. return
  558. }
  559. type CellPosition struct {
  560. RowIndex int
  561. ColumnIndex int
  562. }
  563. // HandleTableCell 前端d毛需要我根据合并单元格处理掉多余的单元格
  564. func HandleTableCell(oldTableData TableData) (newTableData TableData) {
  565. newTableData = oldTableData
  566. mergeList := oldTableData.MergeList
  567. lenMergeList := len(mergeList)
  568. if lenMergeList <= 0 {
  569. return
  570. }
  571. deleteRowMap := make(map[int]map[int]int)
  572. deleteRowList := make([]int, 0)
  573. deleteColumnMap := make(map[int][]int, 0)
  574. for i := lenMergeList - 1; i >= 0; i-- {
  575. tmpMerge := mergeList[i]
  576. //
  577. for rowIndex := tmpMerge.MergeRowNum; rowIndex >= 0; rowIndex-- {
  578. tmpColumnMap := make(map[int]int)
  579. if columnMap, ok := deleteRowMap[tmpMerge.StartRowIndex+rowIndex]; ok {
  580. tmpColumnMap = columnMap
  581. } else {
  582. deleteRowList = append(deleteRowList, tmpMerge.StartRowIndex+rowIndex)
  583. }
  584. deleteColumnList := make([]int, 0)
  585. if columnList, ok := deleteColumnMap[tmpMerge.StartRowIndex+rowIndex]; ok {
  586. deleteColumnList = columnList
  587. }
  588. for columnIndex := tmpMerge.MergeColumnNum; columnIndex >= 0; columnIndex-- {
  589. if rowIndex == 0 && columnIndex == 0 {
  590. continue
  591. }
  592. tmpColumnMap[tmpMerge.StartColumnIndex+columnIndex] = tmpMerge.StartColumnIndex + columnIndex
  593. deleteColumnList = append(deleteColumnList, tmpMerge.StartColumnIndex+columnIndex)
  594. }
  595. // 待删除的行
  596. deleteRowMap[tmpMerge.StartRowIndex+rowIndex] = tmpColumnMap
  597. // 该行待删除的列
  598. deleteColumnMap[tmpMerge.StartRowIndex+rowIndex] = deleteColumnList
  599. }
  600. }
  601. sort.Ints(deleteRowList)
  602. //for k, v := range deleteRowList {
  603. // fmt.Println("k:", k, "v:", v)
  604. //}
  605. //return
  606. //fmt.Println("===============")
  607. for i := len(deleteRowList) - 1; i >= 0; i-- {
  608. rowIndex := deleteRowList[i]
  609. deleteColumnList := deleteColumnMap[rowIndex]
  610. sort.Ints(deleteColumnList)
  611. for i := len(deleteColumnList) - 1; i >= 0; i-- {
  612. columnIndex := deleteColumnList[i]
  613. // 最后一行合并单元格时,就不再次移除合并的单元格,避免数组越界
  614. if rowIndex >= len(newTableData.TableDataList) {
  615. continue
  616. }
  617. tmpColumnDataList := newTableData.TableDataList[rowIndex]
  618. // 最后一列合并单元格时,就不再次移除合并的单元格,避免数组越界
  619. if columnIndex >= len(tmpColumnDataList) {
  620. continue
  621. }
  622. newTableData.TableDataList[rowIndex] = append(tmpColumnDataList[:columnIndex], tmpColumnDataList[columnIndex+1:]...) // 删除开头N个元素
  623. //fmt.Println("row:", rowIndex, "===column:", columnIndex)
  624. }
  625. }
  626. return
  627. }
  628. // GetTableDataByLuckySheetDataStrBak 通过LuckySheet的string数据获取表格数据(备份:2022-08-23 10:30:32)
  629. func (item *LuckySheetData) GetTableDataByLuckySheetDataStrBak() (selfTableData TableData, err error) {
  630. luckySheetCellDataList := item.CellData
  631. // 表格数据
  632. tableDataMap := make(map[int64]map[int64]LuckySheetDataValue)
  633. // 最大行,最大列
  634. var maxRow, maxCol int64
  635. for _, v := range luckySheetCellDataList {
  636. //fmt.Println("row:", v.Row, "=====col:", v.Col)
  637. if v.Row > maxRow { //最大行
  638. maxRow = v.Row
  639. }
  640. if v.Col > maxCol { //最大列
  641. maxCol = v.Col
  642. }
  643. var tmpRow map[int64]LuckySheetDataValue
  644. tmpRow, ok := tableDataMap[v.Row]
  645. if ok {
  646. tmpRow[v.Col] = v.Value
  647. } else {
  648. tmpRow = make(map[int64]LuckySheetDataValue)
  649. tmpRow[v.Col] = v.Value
  650. }
  651. tableDataMap[v.Row] = tmpRow
  652. }
  653. tableDataList := make([][]LuckySheetDataValue, 0)
  654. var i int64
  655. // 单元格宽度
  656. configColumnConf := item.Config.Columnlen
  657. rowWidthMap := make(map[int]float64)
  658. rowWidthList := make([]float64, 0) //
  659. // 单元格高度
  660. configRowConf := item.Config.Rowlen
  661. rowHeightList := make([]float64, 0) //
  662. for i = 0; i <= maxRow; i++ {
  663. //列
  664. tmpTableColDataList := make([]LuckySheetDataValue, 0)
  665. // 每个单元格的高度
  666. tmpHeight, ok := configRowConf[fmt.Sprint(i)]
  667. if !ok {
  668. tmpHeight = 0
  669. }
  670. rowHeightList = append(rowHeightList, tmpHeight)
  671. tmpRowData, ok := tableDataMap[i]
  672. // 如果没有该行数据,那么就默认添加空行数据处理
  673. if !ok {
  674. tmpRowData = make(map[int64]LuckySheetDataValue)
  675. }
  676. var j int64
  677. for j = 0; j <= maxCol; j++ {
  678. tmpTableColData, ok := tmpRowData[j]
  679. if !ok {
  680. tmpTableColData = LuckySheetDataValue{}
  681. }
  682. //单元格显示的数据处理
  683. tmpTableColData.Monitor = handleCellVal(tmpTableColData)
  684. tmpTableColDataList = append(tmpTableColDataList, tmpTableColData)
  685. // 每个单元格的宽度
  686. tmpWidth, ok := configColumnConf[fmt.Sprint(j)]
  687. if !ok {
  688. tmpWidth = 0
  689. }
  690. rowIndex := len(tmpTableColDataList) - 1
  691. if _, ok2 := rowWidthMap[rowIndex]; !ok2 {
  692. rowWidthList = append(rowWidthList, tmpWidth)
  693. }
  694. rowWidthMap[rowIndex] = tmpWidth
  695. }
  696. tableDataList = append(tableDataList, tmpTableColDataList)
  697. }
  698. //总共多少行
  699. lenRow := len(tableDataList)
  700. //移除上下空行
  701. deleteRowIndexList := make([]int, 0)
  702. if lenRow > 0 {
  703. var flag = false
  704. // 移除上方空列
  705. for rowIndex := 0; rowIndex < lenRow; rowIndex++ {
  706. isDelete := true
  707. for _, v := range tableDataList[rowIndex] {
  708. if v.Monitor != `` {
  709. isDelete = false
  710. flag = true
  711. break
  712. }
  713. }
  714. if flag {
  715. break
  716. }
  717. if isDelete {
  718. selfTableData.RemoveTopRow++
  719. deleteRowIndexList = append(deleteRowIndexList, rowIndex)
  720. }
  721. }
  722. // 移除下方空行
  723. flag = false
  724. //尾部
  725. deleteBottomRowIndexList := make([]int, 0)
  726. for rowIndex := lenRow - 1; rowIndex >= 0; rowIndex-- {
  727. isDelete := true
  728. for _, v := range tableDataList[rowIndex] {
  729. if v.Monitor != `` || (v.MergeCell.Row != rowIndex && v.MergeCell.Row != 0) {
  730. isDelete = false
  731. flag = true
  732. break
  733. }
  734. }
  735. if flag {
  736. break
  737. }
  738. if isDelete {
  739. deleteBottomRowIndexList = append(deleteBottomRowIndexList, rowIndex)
  740. selfTableData.RemoveBottomRow++
  741. }
  742. }
  743. if len(deleteBottomRowIndexList) > 0 {
  744. deleteRowIndexList = append(deleteRowIndexList, utils.RevSlice(deleteBottomRowIndexList)...)
  745. }
  746. lenDeleteRow := len(deleteRowIndexList)
  747. if lenDeleteRow > 0 {
  748. for rowIndex := lenDeleteRow - 1; rowIndex >= 0; rowIndex-- {
  749. //表格数据
  750. tableDataList = append(tableDataList[:deleteRowIndexList[rowIndex]], tableDataList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  751. //表格高度
  752. rowHeightList = append(rowHeightList[:deleteRowIndexList[rowIndex]], rowHeightList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  753. }
  754. }
  755. }
  756. //移除前后空列
  757. deleteColumnIndexList := make([]int, 0)
  758. if len(tableDataList) > 0 {
  759. var flag = false
  760. lenColumn := len(tableDataList[0])
  761. // 移除前方空列
  762. for columnIndex := 0; columnIndex < lenColumn; columnIndex++ {
  763. isDelete := true
  764. for _, v := range tableDataList {
  765. //如果一列都没有,说明是上面几行是空行,没有数据
  766. if len(v) <= 0 {
  767. continue
  768. }
  769. if v[columnIndex].Monitor != `` {
  770. isDelete = false
  771. flag = true
  772. break
  773. }
  774. }
  775. if flag {
  776. break
  777. }
  778. if isDelete {
  779. selfTableData.RemoveLeftColumn++
  780. deleteColumnIndexList = append(deleteColumnIndexList, columnIndex)
  781. }
  782. }
  783. // 移除后方空列
  784. flag = false
  785. //后方
  786. deleteTailColumnIndexList := make([]int, 0)
  787. for columnIndex := lenColumn - 1; columnIndex >= 0; columnIndex-- {
  788. isDelete := true
  789. for _, v := range tableDataList {
  790. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  791. isDelete = false
  792. flag = true
  793. break
  794. }
  795. }
  796. if flag {
  797. break
  798. }
  799. if isDelete {
  800. deleteTailColumnIndexList = append(deleteTailColumnIndexList, columnIndex)
  801. selfTableData.RemoveRightColumn++
  802. }
  803. }
  804. if len(deleteTailColumnIndexList) > 0 {
  805. deleteColumnIndexList = append(deleteColumnIndexList, utils.RevSlice(deleteTailColumnIndexList)...)
  806. }
  807. lenDeleteColumn := len(deleteColumnIndexList)
  808. if lenDeleteColumn > 0 {
  809. for columnIndex := lenDeleteColumn - 1; columnIndex >= 0; columnIndex-- {
  810. //表格数据
  811. for k, v := range tableDataList {
  812. tableDataList[k] = append(v[:deleteColumnIndexList[columnIndex]], v[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  813. }
  814. //表格宽度
  815. rowWidthList = append(rowWidthList[:deleteColumnIndexList[columnIndex]], rowWidthList[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  816. }
  817. }
  818. }
  819. //处理合并单元格
  820. tableDataMergeList := make([]TableDataMerge, 0)
  821. for _, v := range item.Config.Merge {
  822. indexRow := v.Row - selfTableData.RemoveTopRow
  823. indexColumn := v.Column - selfTableData.RemoveLeftColumn
  824. tableDataMerge := TableDataMerge{
  825. StartRowIndex: indexRow,
  826. StartColumnIndex: indexColumn,
  827. MergeRowNum: v.Rs - 1,
  828. MergeColumnNum: v.Cs - 1,
  829. }
  830. tableDataMergeList = append(tableDataMergeList, tableDataMerge)
  831. }
  832. // 表格数据
  833. selfTableData.TableDataList = tableDataList
  834. selfTableData.RowWidthList = rowWidthList
  835. selfTableData.RowHeightList = rowHeightList
  836. selfTableData.MergeList = tableDataMergeList
  837. return
  838. }
  839. // GetTableDataByCustomData 通过自定义表格数据获取表格数据
  840. func GetTableDataByCustomData(excelType int, data request.TableDataReq, lang string) (selfTableData TableData, err error) {
  841. tableDataList := make([][]LuckySheetDataValue, 0)
  842. mergeList := make([]TableDataMerge, 0)
  843. // 指标数
  844. lenEdb := len(data.Data)
  845. if lenEdb <= 0 {
  846. return
  847. }
  848. // 日期数
  849. lenCol := len(data.Data[0].Data)
  850. if lenCol <= 0 {
  851. return
  852. }
  853. // 指标列:1;指标行:2
  854. if excelType == 1 {
  855. mergeList = append(mergeList, TableDataMerge{
  856. StartRowIndex: 0,
  857. StartColumnIndex: 0,
  858. MergeRowNum: 1,
  859. MergeColumnNum: 0,
  860. })
  861. // 第一行
  862. {
  863. firstCol := make([]LuckySheetDataValue, 0)
  864. dateStr := "日期"
  865. switch lang {
  866. case utils.EnLangVersion:
  867. dateStr = "Date"
  868. }
  869. firstCol = append(firstCol, LuckySheetDataValue{
  870. Value: dateStr,
  871. Monitor: dateStr,
  872. MergeCell: LuckySheetDataConfigMerge{
  873. Row: 0, //行数
  874. Column: 0, //列数
  875. Rs: 2, //合并的行数
  876. Cs: 1, //合并的列数
  877. },
  878. })
  879. for _, v := range data.Data {
  880. edbName := v.EdbName
  881. if v.EdbAliasName != `` {
  882. edbName = v.EdbAliasName
  883. } else {
  884. switch lang {
  885. case utils.EnLangVersion:
  886. edbName = v.EdbNameEn
  887. }
  888. }
  889. firstCol = append(firstCol, LuckySheetDataValue{
  890. Value: edbName,
  891. Monitor: edbName,
  892. MergeCell: LuckySheetDataConfigMerge{},
  893. })
  894. }
  895. tableDataList = append(tableDataList, firstCol)
  896. }
  897. // 第二行
  898. {
  899. secondCol := make([]LuckySheetDataValue, 0)
  900. secondCol = append(secondCol, LuckySheetDataValue{})
  901. for _, v := range data.Data {
  902. var name string
  903. switch lang {
  904. case utils.EnLangVersion:
  905. name = v.UnitEn + " / " + utils.GetFrequencyEn(v.Frequency)
  906. default:
  907. name = v.Unit + " / " + v.Frequency
  908. }
  909. secondCol = append(secondCol, LuckySheetDataValue{
  910. Value: name,
  911. Monitor: name,
  912. MergeCell: LuckySheetDataConfigMerge{},
  913. })
  914. }
  915. tableDataList = append(tableDataList, secondCol)
  916. }
  917. // 开始数据了
  918. {
  919. for i := 0; i < lenCol; i++ {
  920. dataCol := make([]LuckySheetDataValue, 0)
  921. // 日期
  922. dataCol = append(dataCol, LuckySheetDataValue{
  923. Value: data.Data[0].Data[i].DataTime,
  924. Monitor: data.Data[0].Data[i].DataTime,
  925. MergeCell: LuckySheetDataConfigMerge{},
  926. })
  927. // 数据值
  928. for _, v := range data.Data {
  929. background := ``
  930. if v.Data[i].DataType == 5 {
  931. background = "#ffefdd"
  932. }
  933. dataCol = append(dataCol, LuckySheetDataValue{
  934. Value: v.Data[i].Value,
  935. Monitor: v.Data[i].ShowValue,
  936. MergeCell: LuckySheetDataConfigMerge{},
  937. Background: background,
  938. })
  939. }
  940. tableDataList = append(tableDataList, dataCol)
  941. }
  942. }
  943. // 开始文本行了
  944. {
  945. for _, textColList := range data.TextRowData {
  946. dataCol := make([]LuckySheetDataValue, 0)
  947. for _, v := range textColList {
  948. dataCol = append(dataCol, LuckySheetDataValue{
  949. Value: v.Value,
  950. Monitor: v.ShowValue,
  951. MergeCell: LuckySheetDataConfigMerge{},
  952. })
  953. }
  954. tableDataList = append(tableDataList, dataCol)
  955. }
  956. }
  957. } else {
  958. // 指标行
  959. mergeList = append(mergeList, TableDataMerge{
  960. StartRowIndex: 0,
  961. StartColumnIndex: 0,
  962. MergeRowNum: 0,
  963. MergeColumnNum: 1,
  964. })
  965. // 第一行
  966. {
  967. firstCol := make([]LuckySheetDataValue, 0)
  968. dateStr := "日期"
  969. switch lang {
  970. case utils.EnLangVersion:
  971. dateStr = "Date"
  972. }
  973. firstCol = append(firstCol, LuckySheetDataValue{
  974. Value: dateStr,
  975. Monitor: dateStr,
  976. MergeCell: LuckySheetDataConfigMerge{
  977. Row: 0, //行数
  978. Column: 0, //列数
  979. Rs: 1, //合并的行数
  980. Cs: 2, //合并的列数
  981. },
  982. })
  983. firstCol = append(firstCol, LuckySheetDataValue{})
  984. // 日期列
  985. for _, v := range data.Data[0].Data {
  986. firstCol = append(firstCol, LuckySheetDataValue{
  987. Value: v.DataTime,
  988. Monitor: v.DataTime,
  989. MergeCell: LuckySheetDataConfigMerge{},
  990. })
  991. }
  992. // 文本列
  993. for _, textColList := range data.TextRowData {
  994. firstCol = append(firstCol, LuckySheetDataValue{
  995. Value: textColList[0].Value,
  996. Monitor: textColList[0].ShowValue,
  997. MergeCell: LuckySheetDataConfigMerge{},
  998. })
  999. }
  1000. tableDataList = append(tableDataList, firstCol)
  1001. }
  1002. // 日期列+文本列+两列表头(日期这个文案表头)
  1003. //colLen := lenCol+2+len(data.TextRowData)
  1004. for i := 0; i < lenEdb; i++ {
  1005. dataCol := make([]LuckySheetDataValue, 0)
  1006. // 指标信息
  1007. tmpEdbInfo := data.Data[i]
  1008. // 指标名称
  1009. {
  1010. edbName := tmpEdbInfo.EdbName
  1011. if tmpEdbInfo.EdbAliasName != `` {
  1012. edbName = tmpEdbInfo.EdbAliasName
  1013. } else {
  1014. switch lang {
  1015. case utils.EnLangVersion:
  1016. edbName = tmpEdbInfo.EdbNameEn
  1017. }
  1018. }
  1019. dataCol = append(dataCol, LuckySheetDataValue{
  1020. Value: edbName,
  1021. Monitor: edbName,
  1022. MergeCell: LuckySheetDataConfigMerge{},
  1023. })
  1024. }
  1025. // 指标单位、频度
  1026. {
  1027. var name string
  1028. switch lang {
  1029. case utils.EnLangVersion:
  1030. name = tmpEdbInfo.UnitEn + " / " + utils.GetFrequencyEn(tmpEdbInfo.Frequency)
  1031. default:
  1032. name = tmpEdbInfo.Unit + " / " + tmpEdbInfo.Frequency
  1033. }
  1034. dataCol = append(dataCol, LuckySheetDataValue{
  1035. Value: name,
  1036. Monitor: name,
  1037. MergeCell: LuckySheetDataConfigMerge{},
  1038. })
  1039. }
  1040. // 指标数据列
  1041. for _, tmpData := range tmpEdbInfo.Data {
  1042. background := ``
  1043. if tmpData.DataType == 5 {
  1044. background = "#ffefdd"
  1045. }
  1046. dataCol = append(dataCol, LuckySheetDataValue{
  1047. Value: tmpData.Value,
  1048. Monitor: tmpData.ShowValue,
  1049. MergeCell: LuckySheetDataConfigMerge{},
  1050. Background: background,
  1051. })
  1052. }
  1053. // 文本列
  1054. for _, textColList := range data.TextRowData {
  1055. dataCol = append(dataCol, LuckySheetDataValue{
  1056. Value: textColList[i+1].Value,
  1057. Monitor: textColList[i+1].ShowValue,
  1058. MergeCell: LuckySheetDataConfigMerge{},
  1059. })
  1060. }
  1061. tableDataList = append(tableDataList, dataCol)
  1062. }
  1063. }
  1064. selfTableData.MergeList = mergeList
  1065. selfTableData.TableDataList = tableDataList
  1066. return
  1067. }
  1068. // GetTableDataByMixedTableData 通过混合表格数据获取表格数据
  1069. func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hideMerged bool) (selfTableData TableData, err error) {
  1070. tableDataList := make([][]LuckySheetDataValue, 0)
  1071. mergeList := make([]TableDataMerge, 0)
  1072. // 开始文本行了
  1073. {
  1074. for _, row := range config {
  1075. dataCol := make([]LuckySheetDataValue, 0)
  1076. for _, cell := range row {
  1077. tmp := LuckySheetDataValue{
  1078. Value: cell.Value,
  1079. Monitor: cell.ShowValue,
  1080. MergeCell: LuckySheetDataConfigMerge{},
  1081. }
  1082. // 前端需要隐藏被合并的单元格, 混合表格/平衡表通过这个字段判断, 不通过HandleTableCell方法隐藏
  1083. if cell.MerData != nil {
  1084. if hideMerged && cell.MerData.Type == "merged" {
  1085. continue
  1086. }
  1087. tmp.MergeCell.Rs = cell.MerData.Mer.Rowspan
  1088. tmp.MergeCell.Cs = cell.MerData.Mer.Colspan
  1089. tmp.MergeCell.Row = cell.MerData.Mer.Row
  1090. tmp.MergeCell.Column = cell.MerData.Mer.Col
  1091. }
  1092. if cell.ShowStyle != "" {
  1093. tmp.Monitor = cell.ShowFormatValue
  1094. }
  1095. dataCol = append(dataCol, tmp)
  1096. }
  1097. tableDataList = append(tableDataList, dataCol)
  1098. }
  1099. }
  1100. selfTableData.MergeList = mergeList
  1101. selfTableData.TableDataList = tableDataList
  1102. return
  1103. }