1

Is there a way to read in a selection of non-consecutive columns of Excel data using XLSX.gettable? I’ve read the documentation here XLSX.jl Tutorial, but it’s not clear whether it’s possible to do this. For example,

df = DataFrame(XLSX.gettable(sheet,"A:B")...)

selects the data in columns “A” and “B” of a worksheet called sheet. But what if I want columns A and C, for example? I tried

df = DataFrame(XLSX.gettable(sheet,["A","C"])...)

and similar variations of this, but it throws the following error: MethodError: no method matching gettable(::XLSX.Worksheet, ::Array{String,1}).

Is there a way to make this work with gettable, or is there a similar function which can accomplish this?

2 Answers 2

0

I don't think this is possible with the current version of XLSX.jl:

If you look at the definition of gettable here you'll see that it calls

eachtablerow(sheet, cols;...)

which is defined here as accepting Union{ColumnRange, AbstractString} as input for the cols argument. The cols argument itself is converted to a ColumnRange object in the eachtablerow function, which is defined here as:

struct ColumnRange
    start::Int # column number
    stop::Int  # column number

    function ColumnRange(a::Int, b::Int)
        @assert a <= b "Invalid ColumnRange. Start column must be located before end column."
        return new(a, b)
    end
end

So it looks to me like only consecutive columns are working.

To get around this you should be able to just broadcast the gettable function over your column ranges and then concatenate the resulting DataFrames:

df = reduce(hcat, DataFrame.(XLSX.gettable.(sheet, ["A:B", "D:E"])))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. Unfortunately when I tried to run your code on my sample data, I got the error MethodError: no method matching length(::XLSX.Worksheet)...
Ah sorry I hadn't actually tried this - that probably just means that XLSX.Worksheet is not broadcastable, try a comprehension reduce(hcat, [DataFrame(XLSX.gettable(sheet, x)) for x in ["A:B", "D:E"]])
0

I found that to get @Nils Gudat's answer to work you need to add the ... operator to give

reduce(hcat, [DataFrame(XLSX.gettable(sheet, x)...) for x in ["A:B", "D:E"]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.