xlsx.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package schedule
  2. import (
  3. // "fmt"
  4. "strings"
  5. "strconv"
  6. "github.com/tealeg/xlsx"
  7. )
  8. func ImportXLSX(filename string) []*PlanXLSX {
  9. var (
  10. listOfPlans []*PlanXLSX
  11. )
  12. xlFile, err := xlsx.OpenFile(filename)
  13. if err != nil {
  14. return nil
  15. }
  16. for _, sheet := range xlFile.Sheets {
  17. var plan = new(PlanXLSX)
  18. x, _ := sheet.Cell(5, 1)
  19. plan.ProgramCode = x.String()
  20. namecode, _ := sheet.Cell(5,6)
  21. plan.NameCode = strings.Split(namecode.String(), " ")[0]
  22. pof, _ := sheet.Cell(7,6)
  23. plan.PeriodOfStudy = pof.String()
  24. spl, _ := sheet.Cell(9,1)
  25. splited := strings.Split(spl.String(), " ")
  26. plan.GroupNumber = strings.Split(splited[len(splited)-1], "-")[0]
  27. sheet.ForEachRow(func(row *Row) error {
  28. splited := strings.Split(row.Cells[1].String(), ".")
  29. if i >= 20 && len(row.Cells) >= 20 && len(row.Cells)>=20 && !(row.Cells[2].String() == "" || row.Cells[2].String() == "ИТОГО")
  30. && !(strings.HasPrefix(row.Cells[1].String(), "Консультации") || strings.HasPrefix(row.Cells[1].String(), "Государственная"))
  31. && len(splited) == 2{
  32. typerow := TypeRow(IsSubTR)
  33. switch {
  34. case strings.Contains(row.Cells[2].String(), "цикл"):
  35. typerow = IsCycleTR
  36. case splited[1] == "00":
  37. typerow = IsStartTR
  38. case splited[0] == "ПМ":
  39. typerow = IsPmTR
  40. }
  41. max, _ := row.Cells[4].Int()
  42. selfstudy, _ := row.Cells[5].Int()
  43. allstudy, _ := row.Cells[6].Int()
  44. lectures, _ := row.Cells[7].Int()
  45. labs, _ := row.Cells[8].Int()
  46. projects, _ := row.Cells[9].Int()
  47. c1s1, _ := row.Cells[12].Int()
  48. c1s2, _ := row.Cells[13].Int()
  49. c2s1, _ := row.Cells[14].Int()
  50. c2s2, _ := row.Cells[15].Int()
  51. c3s1, _ := row.Cells[16].Int()
  52. c3s2, _ := row.Cells[17].Int()
  53. c4s1, _ := row.Cells[18].Int()
  54. c4s2, _ := row.Cells[19].Int()
  55. certform := ""
  56. splited = strings.Split(row.Cells[3].String(), ",")
  57. for _, v := range splited {
  58. if v != "-" {
  59. certform = v
  60. break
  61. }
  62. }
  63. plan.Lines = append(plan.Lines, LineXLSX{
  64. ID: row.Cells[1].String(),
  65. Name: row.Cells[2].String(),
  66. CertForm: certform,
  67. StudyLoad: StudyLoadXLSX{
  68. Max: max,
  69. SelfStudy: selfstudy,
  70. AllStudy: allstudy,
  71. Lectures: lectures,
  72. Labs: labs,
  73. Projects: projects,
  74. },
  75. Course: [4][2]int{
  76. [2]int{
  77. c1s1,
  78. c1s2,
  79. },
  80. [2]int{
  81. c2s1,
  82. c2s2,
  83. },
  84. [2]int{
  85. c3s1,
  86. c3s2,
  87. },
  88. [2]int{
  89. c4s1,
  90. c4s2,
  91. },
  92. },
  93. TypeRow: typerow,
  94. })
  95. }
  96. // if len(row.Cells) < 20 {
  97. // continue
  98. // }
  99. // if row.Cells[2].String() == "" || row.Cells[2].String() == "ИТОГО" {
  100. // continue
  101. // }
  102. // if strings.HasPrefix(row.Cells[1].String(), "Консультации") || strings.HasPrefix(row.Cells[1].String(), "Государственная") {
  103. // continue
  104. // }
  105. // splited := strings.Split(row.Cells[1].String(), ".")
  106. // if len(splited) != 2 {
  107. // continue
  108. // }
  109. }
  110. listOfPlans = append(listOfPlans, plan)
  111. // for i, row := range sheet.Rows {
  112. // if i < 20 {
  113. // continue
  114. // }
  115. // if len(row.Cells) < 20 {
  116. // continue
  117. // }
  118. // if row.Cells[2].String() == "" || row.Cells[2].String() == "ИТОГО" {
  119. // continue
  120. // }
  121. // if strings.HasPrefix(row.Cells[1].String(), "Консультации") || strings.HasPrefix(row.Cells[1].String(), "Государственная") {
  122. // continue
  123. // }
  124. // splited := strings.Split(row.Cells[1].String(), ".")
  125. // if len(splited) != 2 {
  126. // continue
  127. // }
  128. // typerow := TypeRow(IsSubTR)
  129. // switch {
  130. // case strings.Contains(row.Cells[2].String(), "цикл"):
  131. // typerow = IsCycleTR
  132. // case splited[1] == "00":
  133. // typerow = IsStartTR
  134. // case splited[0] == "ПМ":
  135. // typerow = IsPmTR
  136. // }
  137. // max, _ := row.Cells[4].Int()
  138. // selfstudy, _ := row.Cells[5].Int()
  139. // allstudy, _ := row.Cells[6].Int()
  140. // lectures, _ := row.Cells[7].Int()
  141. // labs, _ := row.Cells[8].Int()
  142. // projects, _ := row.Cells[9].Int()
  143. // c1s1, _ := row.Cells[12].Int()
  144. // c1s2, _ := row.Cells[13].Int()
  145. // c2s1, _ := row.Cells[14].Int()
  146. // c2s2, _ := row.Cells[15].Int()
  147. // c3s1, _ := row.Cells[16].Int()
  148. // c3s2, _ := row.Cells[17].Int()
  149. // c4s1, _ := row.Cells[18].Int()
  150. // c4s2, _ := row.Cells[19].Int()
  151. // certform := ""
  152. // splited = strings.Split(row.Cells[3].String(), ",")
  153. // for _, v := range splited {
  154. // if v != "-" {
  155. // certform = v
  156. // break
  157. // }
  158. // }
  159. // plan.Lines = append(plan.Lines, LineXLSX{
  160. // ID: row.Cells[1].String(),
  161. // Name: row.Cells[2].String(),
  162. // CertForm: certform,
  163. // StudyLoad: StudyLoadXLSX{
  164. // Max: max,
  165. // SelfStudy: selfstudy,
  166. // AllStudy: allstudy,
  167. // Lectures: lectures,
  168. // Labs: labs,
  169. // Projects: projects,
  170. // },
  171. // Course: [4][2]int{
  172. // [2]int{
  173. // c1s1,
  174. // c1s2,
  175. // },
  176. // [2]int{
  177. // c2s1,
  178. // c2s2,
  179. // },
  180. // [2]int{
  181. // c3s1,
  182. // c3s2,
  183. // },
  184. // [2]int{
  185. // c4s1,
  186. // c4s2,
  187. // },
  188. // },
  189. // TypeRow: typerow,
  190. // })
  191. // }
  192. listOfPlans = append(listOfPlans, plan)
  193. }
  194. return listOfPlans
  195. return nil
  196. }
  197. func CreateXLSX(filename string) (*xlsx.File, string) {
  198. file := xlsx.NewFile()
  199. _, err := file.AddSheet("Init")
  200. if err != nil {
  201. return nil, ""
  202. }
  203. err = file.Save(filename)
  204. if err != nil {
  205. return nil, ""
  206. }
  207. return file, filename
  208. }
  209. func (gen *Generator) WriteXLSX(file *xlsx.File, filename string, schedule []*Schedule, iter int) error {
  210. const (
  211. colWidth = 30
  212. rowHeight = 30
  213. MAXCOL = 3
  214. )
  215. rowsNext := uint(len(schedule)) / MAXCOL
  216. if rowsNext == 0 || uint(len(schedule)) % MAXCOL != 0 {
  217. rowsNext += 1
  218. }
  219. var (
  220. colNum = uint(NUM_TABLES + 2)
  221. row = make([]*xlsx.Row, colNum * rowsNext) // * (rowsNext + 1)
  222. cell *xlsx.Cell
  223. dayN = gen.Day
  224. day = ""
  225. )
  226. if dayN == SUNDAY {
  227. dayN = SATURDAY
  228. } else {
  229. dayN -= 1
  230. }
  231. switch dayN {
  232. case SUNDAY: day = "Sunday"
  233. case MONDAY: day = "Monday"
  234. case TUESDAY: day = "Tuesday"
  235. case WEDNESDAY: day = "Wednesday"
  236. case THURSDAY: day = "Thursday"
  237. case FRIDAY: day = "Friday"
  238. case SATURDAY: day = "Saturday"
  239. }
  240. sheet, err := file.AddSheet(day + "-" + strconv.Itoa(iter))
  241. if err != nil {
  242. return err
  243. }
  244. sheet.SetColWidth(2, int(MAXCOL)*3+1, COL_W)
  245. for r := uint(0); r < rowsNext; r++ {
  246. for i := uint(0); i < colNum; i++ {
  247. row[(r*colNum)+i] = sheet.AddRow() // (r*rowsNext)+
  248. row[(r*colNum)+i].SetHeight(ROW_H)
  249. cell = row[(r*colNum)+i].AddCell()
  250. if i == 0 {
  251. cell.Value = "Пара"
  252. continue
  253. }
  254. cell.Value = strconv.Itoa(int(i-1))
  255. }
  256. }
  257. index := uint(0)
  258. exit: for r := uint(0); r < rowsNext; r++ {
  259. for i := uint(0); i < MAXCOL; i++ {
  260. if uint(len(schedule)) <= index {
  261. break exit
  262. }
  263. savedCell := row[(r*colNum)+0].AddCell()
  264. savedCell.Value = "Группа " + schedule[index].Group
  265. cell = row[(r*colNum)+0].AddCell()
  266. cell = row[(r*colNum)+0].AddCell()
  267. savedCell.Merge(2, 0)
  268. cell = row[(r*colNum)+1].AddCell()
  269. cell.Value = "Предмет"
  270. cell = row[(r*colNum)+1].AddCell()
  271. cell.Value = "Преподаватель"
  272. cell = row[(r*colNum)+1].AddCell()
  273. cell.Value = "Кабинет"
  274. for j, trow := range schedule[index].Table {
  275. cell = row[(r*colNum)+uint(j)+2].AddCell()
  276. if trow.Subject[A] == trow.Subject[B] {
  277. cell.Value = trow.Subject[A]
  278. } else {
  279. if trow.Subject[A] != "" {
  280. cell.Value = trow.Subject[A] + " (A)"
  281. }
  282. if trow.Subject[B] != "" {
  283. cell.Value += "\n" + trow.Subject[B] + " (B)"
  284. }
  285. }
  286. cell = row[(r*colNum)+uint(j)+2].AddCell()
  287. if trow.Teacher[A] == trow.Teacher[B] {
  288. cell.Value = trow.Teacher[A]
  289. } else {
  290. if trow.Teacher[A] != "" {
  291. cell.Value = trow.Teacher[A]
  292. }
  293. if trow.Teacher[B] != "" {
  294. cell.Value += "\n" + trow.Teacher[B]
  295. }
  296. }
  297. sheet.SetColWidth(colWidthForCabinets(int(j)))
  298. cell = row[(r*colNum)+uint(j)+2].AddCell()
  299. if trow.Cabinet[A] == trow.Cabinet[B] {
  300. cell.Value = trow.Cabinet[A]
  301. } else {
  302. if trow.Cabinet[A] != "" {
  303. cell.Value = trow.Cabinet[A]
  304. }
  305. if trow.Cabinet[B] != "" {
  306. cell.Value += "\n" + trow.Cabinet[B]
  307. }
  308. }
  309. }
  310. index++
  311. }
  312. }
  313. err = file.Save(filename)
  314. if err != nil {
  315. return err
  316. }
  317. return nil
  318. }