xlsx.go 9.8 KB

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