lucky_sheet.go 46 KB

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