Flutter: How can I copy paste column values directly into my flutter UI from an excel spreadsheet?

304

Understanding how copy/paste works in Spreadsheets

Assume there is data something like this in Spreadsheet, or MS Excel

A1  B1  C1
A2  B2  C2

When you copy this data, it will be copied as a string in following format

A1\tB1\tC1\nA2\tB2\tC2

To put this data in your table first split them into rows and then into Cells

Example -

void main() {
  String data = "A1\tB1\tC1\nA2\tB2\tC2";
  List<String> rows = data.split('\n');
  List cells = <List<String>>[];
  for (String row in rows) {
    cells.addAll(row.split('\t'));
  }
  print(cells);
  //prints: [[A1, B1, C1], [A2, B2, C2]]
}

NOTE: If you copy only one column, each item will be separated with a '\n'(New Line).

How to use solution with Flutter

Note that spreadsheets doesn't allow you to use new lines in a single cell. So when user copied a text into a cell or use \n then you can handle the data with a function and convert all data into a list as mentioned above.

Share:
304
tsy5176
Author by

tsy5176

Updated on December 24, 2022

Comments

  • tsy5176
    tsy5176 over 1 year

    Is there a specific widget for this? I want to be able to open a spreadsheet on my phone (a google sheet) or an excel file, select a column containing 10-50 latitude points, and another column containing longitude points. I want to be able to paste this directly into my app and create a latlng list from it.

    I've tried using Sticky Headers (below) but it will only let me paste everything into one cell at a time:

    StickyHeadersTable(
              columnsLength: titleColumn.length,
              rowsLength: titleRow.length,
              columnsTitleBuilder: (i) => Text(titleColumn[i]),
              rowsTitleBuilder: (i) => Text(titleRow[i]),
              contentCellBuilder: (i, j) =>
                  Container(height: 50, width: 50, child: TextField()),
              legendCell: Text('Sticky Legend'),
            ),
    

    Is there a way to modify this to accept entire columns from another spreadsheet via copy/paste?

  • Filip Marko
    Filip Marko about 2 years
    You should parse the string with the csv package or something similar, since Excel cells can contain newlines.