Skip to main content

WIP - Helper to convert HTML table to CSV - handling colspan, rowspan, headers, etc

import table_to_csv_headless from "https://deno.land/x/table_to_csv/table_to_csv_headless.js";
table_to_csv_headless(`<table>
  <thead><tr><th colspan="2">The table header</th></tr></thead>
  <tbody><tr><td>The table body</td><td>with two columns</td></tr></tbody>
</table>`); /* returns:
The table header,The table header
The table body,with two columns
*/
table_to_csv_headless(
  `<table>
  <thead><tr><th colspan="2">The table header</th></tr></thead>
  <tbody><tr><td>The table body</td><td>with two columns</td></tr></tbody>
</table>`,
  {
    includeheaders: false,
  }
); /* returns:
The table body,with two columns
*/
let resp = await fetch(
  "https://en.wikipedia.org/wiki/List_of_states_and_territories_of_the_United_States"
);
let text = await resp.text();
table_to_csv_headless(text, {
  tableSelector: "table:nth-of-type(2)",
});