xlsx.go 11 KB

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