lucky_sheet.go 45 KB

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