lucky_sheet.go 39 KB

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