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

Getting Data From a specific Worksheet #124

Closed
Ethan-Chew opened this issue Jul 16, 2020 · 6 comments
Closed

Getting Data From a specific Worksheet #124

Ethan-Chew opened this issue Jul 16, 2020 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@Ethan-Chew
Copy link

I am so sorry to ask so many questions...

May I know how I can get the data from a specific worksheet, rather than letting the code run through all worksheets? Thanks

@MaxDesiatov
Copy link
Collaborator

MaxDesiatov commented Jul 16, 2020

Hi @Ethan-Chew, no worries, it's totally fine 🙂 Did you check the example code in README.md? parseWorksheetPathsAndNames returns an array of worksheet names and paths. If you know the worksheet name you're interested in upfront, you could filter it out and parse only that one. Does that answer your question?

@MaxDesiatov MaxDesiatov added the question Further information is requested label Jul 16, 2020
@Ethan-Chew
Copy link
Author

Yep, it answers my question.
However, what do I use?

file.parseWorksheet()? What do I put in the parentheses though..

@MaxDesiatov
Copy link
Collaborator

parseWorksheet(at:) expects a worksheet path as its argument. If you know the worksheet name, you could filter the path out and then parse it like this:

let filepath = "./file.xlsx"
guard let file = XLSXFile(filepath: filepath) else {
    fatalError("XLSX file at \(filepath) is corrupted or does not exist")
}

let wsName = "Sheet 1"

for wbk in try file.parseWorkbooks() {
    guard let path = try file.parseWorksheetPathsAndNames(workbook: wbk)
        .filter({ $0.name == wsName }).map({ $0.path })
    else { continue }

    let worksheet = try file.parseWorksheet(at: path)
    for row in worksheet.data?.rows ?? [] {
        for c in row.cells {
            print(c)
        }
    }
}

Would this snippet work in your case?

@Ethan-Chew
Copy link
Author

In my case,

guard let path = try file.parseWorksheetPathsAndNames(workbook: wbk) Shows error "Initializer for conditional binding must have Optional type, not '[String]' "

and

let worksheet = try file.parseWorksheet(at: path) Shows error "Cannot convert value of type '[String]' to expected argument type 'String'"

as path is a array and not a string

@MaxDesiatov
Copy link
Collaborator

MaxDesiatov commented Jul 19, 2020

Apologies, here's a snippet that should work, I should have used first instead of filter and a keyword argument where for the corresponding closure:

let filepath = "./file.xlsx"
guard let file = XLSXFile(filepath: filepath) else {
    fatalError("XLSX file at \(filepath) is corrupted or does not exist")
}

let wsName = "Sheet 1"

for wbk in try file.parseWorkbooks() {
    guard let path = try file.parseWorksheetPathsAndNames(workbook: wbk)
        .first(where: { $0.name == wsName }).map({ $0.path })
    else { continue }

    let worksheet = try file.parseWorksheet(at: path)
    for row in worksheet.data?.rows ?? [] {
        for c in row.cells {
            print(c)
        }
    }
}

@Ethan-Chew
Copy link
Author

Thank you so much for your help!

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

No branches or pull requests

2 participants