mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:07:45 +00:00
Spreadsheet: Add support for importing from and exporting to CSV files
Closes #4136.
This commit is contained in:
parent
8066a623d9
commit
48d8534967
3 changed files with 125 additions and 43 deletions
|
@ -486,6 +486,52 @@ JsonObject Sheet::to_json() const
|
|||
return object;
|
||||
}
|
||||
|
||||
Vector<Vector<String>> Sheet::to_xsv() const
|
||||
{
|
||||
Vector<Vector<String>> data;
|
||||
|
||||
// First row = headers.
|
||||
data.append(m_columns);
|
||||
|
||||
for (size_t i = 0; i < m_rows; ++i) {
|
||||
Vector<String> row;
|
||||
row.resize(m_columns.size());
|
||||
for (size_t j = 0; j < m_columns.size(); ++j) {
|
||||
auto cell = at({ m_columns[j], i });
|
||||
if (cell)
|
||||
row[j] = cell->typed_display();
|
||||
}
|
||||
|
||||
data.append(move(row));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
RefPtr<Sheet> Sheet::from_xsv(const Reader::XSV& xsv, Workbook& workbook)
|
||||
{
|
||||
auto cols = xsv.headers();
|
||||
auto rows = xsv.size();
|
||||
|
||||
auto sheet = adopt(*new Sheet(workbook));
|
||||
sheet->m_columns = cols;
|
||||
for (size_t i = 0; i < rows; ++i)
|
||||
sheet->add_row();
|
||||
|
||||
for (auto row : xsv) {
|
||||
for (size_t i = 0; i < cols.size(); ++i) {
|
||||
auto str = row[i];
|
||||
if (str.is_empty())
|
||||
continue;
|
||||
Position position { cols[i], row.index() };
|
||||
auto cell = make<Cell>(str, position, *sheet);
|
||||
sheet->m_cells.set(position, move(cell));
|
||||
}
|
||||
}
|
||||
|
||||
return sheet;
|
||||
}
|
||||
|
||||
JsonObject Sheet::gather_documentation() const
|
||||
{
|
||||
JsonObject object;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue