lucky_sheet.go 46 KB

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