lucky_sheet.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package excel
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. "strconv"
  7. )
  8. type LuckySheetDataBak struct {
  9. CalcChain []interface{} `json:"calcChain" description:"公式链"`
  10. CellData []LuckySheetCellData `json:"celldata" description:"单元格数据"`
  11. ChWidth int64 `json:"ch_width" description:"工作表区域的宽度"`
  12. Config struct {
  13. BorderInfo []struct {
  14. BorderType string `json:"borderType" description:""`
  15. Color string `json:"color" description:""`
  16. Range []struct {
  17. Column []int64 `json:"column" description:""`
  18. Row []int64 `json:"row" description:""`
  19. } `json:"range" description:""`
  20. RangeType string `json:"rangeType" description:""`
  21. Style string `json:"style" description:""`
  22. Value struct {
  23. B struct {
  24. Color string `json:"color" description:""`
  25. Style string `json:"style" description:""`
  26. } `json:"b" description:""`
  27. ColIndex int64 `json:"col_index" description:""`
  28. L struct {
  29. Color string `json:"color" description:""`
  30. Style string `json:"style" description:""`
  31. } `json:"l" description:""`
  32. R struct {
  33. Color string `json:"color" description:""`
  34. Style string `json:"style" description:""`
  35. } `json:"r" description:""`
  36. RowIndex int64 `json:"row_index" description:""`
  37. T struct {
  38. Color string `json:"color" description:""`
  39. Style string `json:"style" description:""`
  40. } `json:"t" description:""`
  41. } `json:"value" description:"" description:""`
  42. } `json:"borderInfo" description:""`
  43. Colhidden struct{} `json:"colhidden" description:""`
  44. Columnlen map[string]float64 `json:"columnlen" description:""`
  45. CustomHeight struct {
  46. Zero int64 `json:"0"`
  47. } `json:"customHeight" description:""`
  48. CustomWidth struct {
  49. Two int64 `json:"2" description:""`
  50. } `json:"customWidth" description:""`
  51. Merge struct{} `json:"merge" description:""`
  52. Rowlen map[string]float64 `json:"rowlen" description:""`
  53. } `json:"config" description:""`
  54. Data [][]struct {
  55. Ct struct {
  56. Fa string `json:"fa"`
  57. T string `json:"t"`
  58. } `json:"ct"`
  59. M string `json:"m"`
  60. V interface{} `json:"v"`
  61. } `json:"data" description:""`
  62. DataVerification struct{} `json:"dataVerification" description:""`
  63. Filter interface{} `json:"filter" description:""`
  64. FilterSelect interface{} `json:"filter_select" description:""`
  65. Hyperlink struct{} `json:"hyperlink" description:""`
  66. Images struct{} `json:"images" description:""`
  67. Index string `json:"index" description:""`
  68. JfgirdSelectSave []interface{} `json:"jfgird_select_save" description:""`
  69. LuckysheetAlternateformatSave []interface{} `json:"luckysheet_alternateformat_save" description:""`
  70. LuckysheetConditionformatSave []interface{} `json:"luckysheet_conditionformat_save" description:""`
  71. LuckysheetSelectSave []struct {
  72. Column []int64 `json:"column" description:""`
  73. ColumnFocus int64 `json:"column_focus" description:""`
  74. Height int64 `json:"height" description:""`
  75. HeightMove int64 `json:"height_move" description:""`
  76. Left int64 `json:"left" description:""`
  77. LeftMove int64 `json:"left_move" description:""`
  78. Row []int64 `json:"row" description:""`
  79. RowFocus int64 `json:"row_focus" description:""`
  80. Top int64 `json:"top" description:""`
  81. TopMove int64 `json:"top_move" description:""`
  82. Width int64 `json:"width" description:""`
  83. WidthMove int64 `json:"width_move" description:""`
  84. } `json:"luckysheet_select_save" description:"" description:""`
  85. LuckysheetSelectionRange []struct {
  86. Column []int64 `json:"column" description:""`
  87. Row []int64 `json:"row" description:""`
  88. } `json:"luckysheet_selection_range" description:""`
  89. RhHeight float64 `json:"rh_height" description:""`
  90. ScrollLeft float64 `json:"scrollLeft" description:""`
  91. ScrollTop float64 `json:"scrollTop" description:""`
  92. Status int64 `json:"status" description:""`
  93. Visibledatacolumn []int64 `json:"visibledatacolumn" description:""`
  94. Visibledatarow []int64 `json:"visibledatarow" description:""`
  95. ZoomRatio float64 `json:"zoomRatio" description:"sheet缩放比例"`
  96. }
  97. type LuckySheetData struct {
  98. CellData []LuckySheetCellData `json:"celldata" description:"单元格数据"`
  99. ChWidth int64 `json:"ch_width" description:"工作表区域的宽度"`
  100. Config LuckySheetDataConfig `json:"config" description:""`
  101. RhHeight float64 `json:"rh_height" description:"工作表区域的高度"`
  102. ScrollLeft float64 `json:"scrollLeft" description:"左右滚动条位置"`
  103. ScrollTop float64 `json:"scrollTop" description:"上下滚动条位置"`
  104. Status interface{} `json:"status" description:"激活状态"`
  105. VisibleDataColumn []int64 `json:"visibledatacolumn" description:"所有列的位置信息,递增的列位置数据,初始化无需设置"`
  106. VisibleDataRow []int64 `json:"visibledatarow" description:"所有行的位置信息,递增的行位置数据,初始化无需设置"`
  107. ZoomRatio float64 `json:"zoomRatio" description:"sheet缩放比例"`
  108. }
  109. type LuckySheetDataConfig struct {
  110. BorderInfo []LuckySheetDataConfigBorderInfo `json:"borderInfo" description:"边框"`
  111. Colhidden map[string]int64 `json:"colhidden" description:"隐藏列,示例值:\"colhidden\":{\"30\":0,\"31\":0}"`
  112. Columnlen map[string]float64 `json:"columnlen" description:"每个单元格的列宽"`
  113. Merge map[string]LuckySheetDataConfigMerge `json:"merge" description:"合并单元格"`
  114. Rowlen map[string]float64 `json:"rowlen" description:"每个单元格的行高"`
  115. }
  116. type LuckySheetDataConfigMerge struct {
  117. Row int `json:"r" description:"行数"`
  118. Column int `json:"c" description:"列数"`
  119. Rs int `json:"rs" description:"合并的行数"`
  120. Cs int `json:"cs" description:"合并的列数"`
  121. }
  122. type LuckySheetDataConfigBorderInfo struct {
  123. 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"`
  124. Color string `json:"color" description:"边框颜色 color: 16进制颜色值"`
  125. Range []struct {
  126. Column []int64 `json:"column" description:"行"`
  127. Row []int64 `json:"row" description:"列"`
  128. } `json:"range" description:"选区范围 range: 行列信息数组"`
  129. RangeType string `json:"rangeType" description:"选区 rangeType: range | cell "`
  130. 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"`
  131. Value LuckySheetDataConfigBorderInfoCellValue `json:"value" description:"" description:"范围类型分单个单元格的数据"`
  132. }
  133. type LuckySheetDataConfigBorderInfoCellValue struct {
  134. B LuckySheetDataConfigBorderInfoCell `json:"b" description:"下边框"`
  135. L LuckySheetDataConfigBorderInfoCell `json:"l" description:"左边框"`
  136. R LuckySheetDataConfigBorderInfoCell `json:"r" description:"右边框"`
  137. T LuckySheetDataConfigBorderInfoCell `json:"t" description:"上边框"`
  138. ColIndex int64 `json:"col_index" description:"第几行"`
  139. RowIndex int64 `json:"row_index" description:"第几列"`
  140. }
  141. type LuckySheetDataConfigBorderInfoCell struct {
  142. Color string `json:"color" description:"边框颜色 color: 16进制颜色值"`
  143. 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"`
  144. 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"`
  145. }
  146. type LuckySheetCellData struct {
  147. Col int64 `json:"c" description:"列"`
  148. Row int64 `json:"r" description:"行"`
  149. Value LuckySheetDataValue `json:"v" description:"单元格内值的数据"`
  150. }
  151. type LuckySheetDataValue struct {
  152. CellType LuckySheetDataCellType `json:"ct" description:"单元格值格式:文本、时间等 "`
  153. Value interface{} `json:"v" description:"原始值"`
  154. Monitor string `json:"m" description:"显示值"`
  155. Background string `json:"bg" description:"背景色,实例值:#fff000"`
  156. 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 华文隶书 "`
  157. 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 华文隶书 "`
  158. FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
  159. Bold int `json:"bl" description:"粗体,0 常规 、 1加粗 "`
  160. Italic int `json:"it" description:"斜体,0 常规 、 1 斜体"`
  161. Fontsize int `description:"字体大小,14"`
  162. CancelLine int ` description:"删除线, 0 常规 、 1 删除线"`
  163. HorizontalType int `description:"水平对齐, 0 居中、1 左、2右"`
  164. VerticalType int ` description:"垂直对齐, 0 中间、1 上、2下"`
  165. Fs interface{} `json:"fs" description:"字体大小,14"`
  166. Cl interface{} `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
  167. Ht interface{} `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
  168. Vt interface{} `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
  169. TextBeak int `description:"文本换行, 0 截断、1溢出、2 自动换行"`
  170. Tb interface{} `json:"tb" description:"文本换行, 0 截断、1溢出、2 自动换行"`
  171. Ps LuckySheetDataCellComment `json:"ps" description:"批注"`
  172. Function string `json:"f" description:"公式"`
  173. MergeCell LuckySheetDataConfigMerge `json:"mc" description:"合并单元格信息"`
  174. }
  175. type LuckySheetDataCellType struct {
  176. Fa string `json:"fa" description:"格式名称,例如:“General”为自动格式"`
  177. T string `json:"t" description:"格式类型,例如:“n”为数字类型"`
  178. S []struct {
  179. 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 华文隶书 "`
  180. 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 华文隶书 "`
  181. FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
  182. Fontsize int `description:"字体大小,14"`
  183. CancelLine int ` description:"删除线, 0 常规 、 1 删除线"`
  184. HorizontalType int `description:"水平对齐, 0 居中、1 左、2右"`
  185. VerticalType int `description:"垂直对齐, 0 中间、1 上、2下"`
  186. Fs interface{} `json:"fs" description:"字体大小,14"`
  187. Cl interface{} `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
  188. Ht interface{} `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
  189. Vt interface{} `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
  190. Un interface{} `json:"un" description:""`
  191. Bold interface{} `json:"bl" description:"粗体,0 常规 、 1加粗 "`
  192. Italic interface{} `json:"it" description:"斜体,0 常规 、 1 斜体"`
  193. Value interface{} `json:"v" description:"原始值"`
  194. } `json:"s"`
  195. }
  196. type LuckySheetDataCellComment struct {
  197. Left int `json:"left" description:"批注框距离左边工作表边缘位置"`
  198. Top int `json:"top" description:"批注框距离上边工作表边缘位置"`
  199. Width int `json:"width" description:"批注框宽度"`
  200. Height int `json:"height" description:"批注框高度"`
  201. Value string `json:"value" description:"批注内容"`
  202. IsShow bool `json:"isshow" description:"是否显示批注"`
  203. }
  204. func GetLuckySheetData(jsonStr string) (item *LuckySheetData, err error) {
  205. err = json.Unmarshal([]byte(jsonStr), &item)
  206. for k, v := range item.CellData {
  207. value := v.Value
  208. value.Fontsize = getIntValueByInterface(value.Fs)
  209. value.CancelLine = getIntValueByInterface(value.Cl)
  210. value.HorizontalType = getIntValueByInterface(value.Ht)
  211. value.VerticalType = getIntValueByInterface(value.Vt)
  212. value.FontFamily = getIntValueByInterface(value.FF)
  213. value.TextBeak = getIntValueByInterface(value.Tb)
  214. if len(value.CellType.S) > 0 {
  215. for kk, vv := range value.CellType.S {
  216. vv.Fontsize = getIntValueByInterface(vv.Fs)
  217. vv.CancelLine = getIntValueByInterface(vv.Cl)
  218. vv.HorizontalType = getIntValueByInterface(vv.Ht)
  219. vv.VerticalType = getIntValueByInterface(vv.Vt)
  220. vv.FontFamily = getIntValueByInterface(vv.FF)
  221. value.CellType.S[kk] = vv
  222. }
  223. }
  224. item.CellData[k].Value = value
  225. }
  226. if len(item.Config.BorderInfo) > 0 {
  227. for k, v := range item.Config.BorderInfo {
  228. v.Value.T.Style = getIntValueByInterface(v.Value.T.Style)
  229. v.Value.B.Style = getIntValueByInterface(v.Value.B.Style)
  230. v.Value.L.Style = getIntValueByInterface(v.Value.L.Style)
  231. v.Value.R.Style = getIntValueByInterface(v.Value.R.Style)
  232. item.Config.BorderInfo[k] = v
  233. }
  234. }
  235. return
  236. }
  237. func getIntValueByInterface(valInterface interface{}) (val int) {
  238. if valInterface == nil {
  239. return
  240. }
  241. switch reflect.TypeOf(valInterface).Kind() {
  242. case reflect.String:
  243. tmpValue := reflect.ValueOf(valInterface).String()
  244. tmpValInt, err := strconv.Atoi(tmpValue)
  245. if err != nil {
  246. val = 0
  247. } else {
  248. val = tmpValInt
  249. }
  250. case reflect.Int, reflect.Int32, reflect.Int64:
  251. tmpValue := reflect.ValueOf(valInterface).Int()
  252. val = int(tmpValue)
  253. }
  254. return
  255. }
  256. type TableData struct {
  257. TableDataList [][]LuckySheetDataValue
  258. RowWidthList []float64
  259. RowHeightList []float64
  260. RemoveTopRow int `description:"移除表格上方的行数"`
  261. RemoveBottomRow int `description:"移除表格下方的行数"`
  262. RemoveLeftColumn int `description:"移除表格左侧的列数"`
  263. RemoveRightColumn int `description:"移除表格右侧的列数"`
  264. MergeList []TableDataMerge `description:"合并数据列"`
  265. }
  266. type TableDataMerge struct {
  267. StartRowIndex int `json:"start_row_index" description:"开始的行下标"`
  268. StartColumnIndex int `json:"start_column" description:"开始的列下标"`
  269. MergeRowNum int `json:"merge_row_num" description:"合并的行数"`
  270. MergeColumnNum int `json:"merge_column_num" description:"合并的列数"`
  271. }
  272. type TableRemoveNum struct {
  273. RemoveTopRow int `description:"移除表格上方的行数"`
  274. RemoveBottomRow int `description:"移除表格下方的行数"`
  275. RemoveLeftColumn int `description:"移除表格左侧的列数"`
  276. RemoveRightColumn int `description:"移除表格右侧的列数"`
  277. }
  278. func handleCellVal(tmpTableColData LuckySheetDataValue) (valueStr string) {
  279. valueStr = tmpTableColData.Monitor
  280. if valueStr == `` {
  281. if valueStr == `` && tmpTableColData.CellType.S != nil {
  282. for _, cellS := range tmpTableColData.CellType.S {
  283. valueStr += fmt.Sprint(cellS.Value)
  284. }
  285. }
  286. }
  287. return
  288. }
  289. type CellPosition struct {
  290. RowIndex int
  291. ColumnIndex int
  292. }