Dax Fohl Dax Fohl - 15 days ago 5
Javascript Question

How can I make a web-based "table" select like a spreadsheet? (rectangular area vs row-wrap selection)

I want to make a table that displays on a webpage, but one requirement is to make it easy to copy and paste into a spreadsheet. Normal HTML tables selection behavior is obviously different from how a spreadsheet like Excel selects -- when you select multiple rows it wraps around instead of selecting a rectangular area. Is there any way to make HTML table behave like a spreadsheet in this regard, or is the only way to resort to a Flash table or something similar?

Answer

No easy solution comes into my mind. You could...

  1. Make rectangular "fake selection" over the table using mousedown/up events.
  2. With javascript, parse values of selected cells into CSV structure.
  3. Send the CSV data to server via ajax and have the server send it back to you as CSV file with proper HTTP headers.
  4. Now you can import the downloaded CSV file into Excel.

Ugly? Oh yeah.