import * as React from 'react' import XLSX from 'xlsx' interface UploadOptimalResponsesProps { submitOptimalResponses: Function; } interface UploadOptimalResponsesState { responses: Array } export class UploadOptimalResponses extends React.Component { constructor(props) { super(props) this.state = { responses: [], } this.handleChangeFile = this.handleChangeFile.bind(this) this.submitResponses = this.submitResponses.bind(this) } handleChangeFile(file: any) { const fileReader = new FileReader(); fileReader.onload = (e) => { const data = new Uint8Array(e.target.result); const workbook = XLSX.read(data, { type: 'array', }); // get the first sheet of the excel workbook const sheet = workbook.Sheets[workbook.SheetNames[0]] const sheet_array = XLSX.utils.sheet_to_json(sheet, {header:1}) const responses = sheet_array.slice(1).map((row: Array) => { return { "text": row[0], "concepts": row.slice(1)} }); this.setState({ responses: responses, }) }; fileReader.readAsArrayBuffer(file); } submitResponses() { this.props.submitOptimalResponses(this.state.responses) } render() { return (
Upload optimal responses

Upload an xlsx file with the prompt in the first row, followed by optimal responses with their associated concepts.

See example

this.handleChangeFile(e.target.files[0])} type="file" />

) } }