Contents

entonio/tabular

The basic spreadsheet format - rows, columns, headers - is well understood by regular computer users. There are many spreadsheet programs, all of which offer advanced editing of the data, and there are even other programs that can export their own data as a table. These circumsta

Examples

Header in row, single data column:

Data:
+-------+-------+
| Name  | Alice |
| Age   |   27  |
+-------+-------+

let person = Person(
    name: try table.at(row:"name").text(),
    age: try table.at(row:"age").int()
)

Create a map of people grouped by last name:

Data:
+-------------+------------+-----+
| First name  | Last name  | Age |
+-------------+------------+-----+
| Alice       | Goldsmith  |  27 |
| Bob         | Cooper     |  31 |
| Concha      | Delorean   |  60 |
| Zoe         | Cooper     |  36 |
+-------------+------------+-----+

let personsByLastName = try table.enumerateRows().reduce(into: [String:[Person]]()) { map, row in
    let lastName = try table.at(col:"last name", row).text()
    let person = try Person(
        firstName: table.at(col:"first name", row).text(),
        lastName: lastName,
        age: table.at(col:"age", row).int()
    )

    var list = map[lastName] ?? []
    list.append(person)
    map[lastName] = list
}

Loading related columns into arrays / 2-dimensional arrays:

Data:
+-------------+-----------------+-----------------+-----------------+-----------------+------------+-----------+
| Guest       | Main Course 1.1 | Main Course 1.2 | Main Course 2.1 | Main Course 2.2 | Dessert 1  | Dessert 2 |
+-------------+-----------------+-----------------+-----------------+-----------------+------------+-----------+
| Alice       | Veal            | Mutton          | Tuna            | Bass            | Porridge   | Mousse    |
| Bob         | Beef            | Pork            | Salmon          | Trout           | Cheesecake | Custard   |
+-------------+-----------------+-----------------+-----------------+-----------------+------------+-----------+

struct Choices {
    let guest: String
    let mains: [[String]]
    let dessert: [String]
}

let choices = try table.enumerateRows().map { row in Choices(
    guest: try table.at(col:"guest", row).text(),
    mains: try table.array2(col:"main course", row).map { $0.map { try $0.text() }},
    dessert: try table.array(col:"main course", row).map { try $0.text() }
)}

License

Except where/if otherwise specified, all the files in this package are copyright of the package contributors mentioned in the NOTICE file and licensed under the Apache 2.0 License, which is permissive for business use.

Package Metadata

Repository: entonio/tabular

Default branch: main

README: README.md