lucky_sheet.go 47 KB

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