Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to directly embed excel ? #43

Open
katrinrodkina opened this issue Oct 18, 2022 · 3 comments
Open

Is it possible to directly embed excel ? #43

katrinrodkina opened this issue Oct 18, 2022 · 3 comments

Comments

@katrinrodkina
Copy link

When I'm trying to pass excel file's url into ExcelRenderer I'm getting the follow error: Unhandled Rejection (TypeError): Failed to execute 'readAsBinaryString' on 'FileReader': parameter 1 is not of type 'Blob'

Is it possible to embed it directly somehow?

@katrinrodkina
Copy link
Author

I've found a way to get blob and display it.

Can I use canvas-datagrid library to style the rows and columns?

@ashishd751
Copy link
Owner

ashishd751 commented Oct 20, 2022 via email

@katrinrodkina
Copy link
Author

katrinrodkina commented Oct 24, 2022

I got it, I used blob.. Weird thing is that when I use remote link to the file it works but is not working when I try to load local file:

` const ExcelRenderer = (file, callback) => {
return new Promise((resolve, reject) => {
const reader = new FileReader()
const isBinaryString = !!reader.readAsBinaryString
reader.onload = e => {
// parse data
const binaryString = e.target.result
try {
const extractedData = XLSX.read(binaryString, {
type: isBinaryString ? 'binary' : 'array',
})

                const sheetName = extractedData.SheetNames[0]
                const sheet = extractedData.Sheets[sheetName]

                const rows = XLSX.utils.sheet_to_json(sheet, { header: 1 })

                const columns = makeColumns(sheet['!ref'])
                const data = { rows, columns }

                resolve(data)
                return callback(null, data)
            } catch (e) {
                console.log(e)
            }
        }
        if (file && isBinaryString) reader.readAsBinaryString(file) 
        else reader.readAsArrayBuffer(file)
    })
}

const makeColumns = range => {
    const columns = []
    const columnsNum = XLSX.utils.decode_range(range).e.c + 1

    for (let i = 0; i < columnsNum; ++i) {
        columns[i] = { name: XLSX.utils.encode_col(i), key: i }
    }

    return columns
}

useEffect(() => {
    fetch(doc_original_link).then(res =>
        res.blob().then(blob => {
            ExcelRenderer(blob, (err, { rows, columns }) => {
                if (err) {
                    console.log(err)
                } else {
                    setRows(rows)
                    setColumns(columns)
                }
            })
        }),
    )
}, [])`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants