App.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import React from 'react'
  2. import XLSX from 'xlsx'
  3. import XLSXS from 'xlsx-style'
  4. import Workbook from 'workbook'
  5. import PlayerList from './player/player-list'
  6. import MatchList from './match/match-list'
  7. import Stats from './stats/stats'
  8. import saveAs from 'file-saver'
  9. //import Workbook from './workbook/workbook.js'
  10. class App extends React.Component {
  11. constructor () {
  12. super()
  13. this.state = {
  14. stats: {
  15. matchDates: []
  16. },
  17. players: [],
  18. matches: [],
  19. worksheets: {
  20. 'PlayerList': null,
  21. 'Calendar': null,
  22. },
  23. filters: {
  24. date: "Alle"
  25. }
  26. }
  27. this.handlePlayerList = this.handlePlayerList.bind(this)
  28. this.handleCalendar = this.handleCalendar.bind(this)
  29. this.generatePlayerList = this.generatePlayerList.bind(this)
  30. this.generateCalendar = this.generateCalendar.bind(this)
  31. this.filterData = this.filterData.bind(this)
  32. this.generateExcel = this.generateExcel.bind(this)
  33. }
  34. componentDidMount() {
  35. this.generateStats()
  36. }
  37. handlePlayerList (event) {
  38. const file = this.playerList.files[0]
  39. this.readWorkbook(file, this.generatePlayerList)
  40. }
  41. handleCalendar (event) {
  42. const file = this.calendar.files[0]
  43. this.readWorkbook(file, this.generateCalendar)
  44. }
  45. readWorkbook (file, callback) {
  46. const reader = new FileReader()
  47. reader.onload = (e) => {
  48. const data = e.target.result
  49. const workbook = XLSX.read(data, {type: 'binary'})
  50. console.log(workbook)
  51. if (workbook.SheetNames.length !== 1) {
  52. throw Error(`Expected only one worksheet in the file, but found ${workbook.SheetNames.length}.`)
  53. }
  54. const worksheet = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[workbook.SheetNames[0]], { header: 1 })
  55. callback(worksheet)
  56. }
  57. reader.readAsBinaryString(file)
  58. }
  59. generatePlayerList (worksheet) {
  60. console.log('About to read the player list.')
  61. const worksheets = { ...this.state.worksheets }
  62. worksheets['PlayerList'] = worksheet
  63. this.setState({ worksheets })
  64. if (worksheet[4].length !== 32 & worksheet[3][0] !== 'Konkurrenz' & worksheet[3][31] !== 'bezahlt') {
  65. throw Error('Wrong file structure.')
  66. }
  67. const players = worksheet.slice(4, worksheet.length)
  68. this.setState({ players })
  69. this.generateStats()
  70. console.log(this.state)
  71. }
  72. generateCalendar (worksheet) {
  73. console.log('About to read the calendar.')
  74. const worksheets = { ...this.state.worksheets }
  75. worksheets['Calendar'] = worksheet
  76. this.setState({ worksheets })
  77. if (worksheet[2].length !== 8) {
  78. throw Error('Wrong file structure.')
  79. }
  80. const matches = worksheet.slice(2, worksheet.length)
  81. this.setState({ matches })
  82. this.generateStats()
  83. console.log(this.state)
  84. }
  85. generateStats () {
  86. const stats = {
  87. players: [],
  88. playerCategories: [],
  89. matchCategories: [],
  90. matches: [],
  91. matchDates: [],
  92. matchPlaces: []
  93. }
  94. stats.players = this.state.players
  95. this.state.players.forEach((player, key) => {
  96. if (!stats.playerCategories.includes(player[0])) {
  97. stats.playerCategories.push(player[0])
  98. }
  99. })
  100. stats.matches = this.state.matches
  101. this.state.matches.forEach((match, key) => {
  102. if (!stats.matchDates.includes(match[1])) {
  103. stats.matchDates.push(match[1])
  104. }
  105. if (!stats.matchCategories.includes(match[3])) {
  106. stats.matchCategories.push(match[3])
  107. }
  108. if (!stats.matchPlaces.includes(match[0])) {
  109. stats.matchPlaces.push(match[0])
  110. }
  111. })
  112. this.setState({ stats })
  113. }
  114. filterData() {
  115. const filters = {
  116. date: this.date.value
  117. }
  118. this.setState({ filters })
  119. }
  120. generateExcel(event) {
  121. event.preventDefault()
  122. const wopts = {bookType: 'xlsx', bookSST: false, type: 'binary'}
  123. const matchlist = new XLSX.Workbook()
  124. const worksheets = {}
  125. this.state.stats.matchPlaces.forEach((place) => {
  126. worksheets[place] = matchlist.add(place)
  127. })
  128. const placeArray = Array.concat(this.state.stats.places, ["Alle"])
  129. console.log(placeArray, matchlist)
  130. const wbout = XLSX.write(matchlist)
  131. function s2ab(s) {
  132. const buf = new ArrayBuffer(s.length)
  133. const view = new Uint8Array(buf)
  134. for (let i=0; i != s.length; ++i) {
  135. view[i] = s.charCodeAt(i) && 0xFF
  136. }
  137. return buf
  138. }
  139. saveAs(new Blob([s2ab(wbout)], {type: 'application/octet-stream'}), 'Spielliste.xlsx')
  140. }
  141. render () {
  142. return (
  143. <div className='App'>
  144. <form>
  145. <label htmlFor='playerList'>Swisstennis playerList.xls</label>
  146. <input type='file' id='PlayerList' ref={(input) => { this.playerList = input }} accept='.xls' placeholder='playerList File' onChange={this.handlePlayerList} />
  147. <label htmlFor='Calendar'>Swisstennis Calendar.xls</label>
  148. <input type='file' id='Calendar' ref={(input) => { this.calendar = input }} accept='.xls' placeholder='Calendar File' onChange={this.handleCalendar} />
  149. <label htmlFor='Date'>Datum</label>
  150. <select ref={(input) => { this.date = input }} onChange={this.filterData}>
  151. <option>Alle</option>
  152. {this.state.stats.matchDates.map((date) => (
  153. <option>{date}</option>
  154. ))}
  155. </select>
  156. <button onClick={this.generateExcel}>Excel-Files generieren.</button>
  157. </form>
  158. <Stats stats={this.state.stats} />
  159. <PlayerList players={this.state.players} />
  160. <MatchList matches={this.state.matches} filters={this.state.filters}/>
  161. </div>
  162. )
  163. }
  164. }
  165. export default App