Last active
February 17, 2020 11:20
-
-
Save sorja/aae2c5fcab6ab23692464458bd189804 to your computer and use it in GitHub Desktop.
Convert any kind of html table to matrix ( for CSV / JSON export ) without using external tools ( like jquery or cheerio )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inspiration/base from cheerio-tableparser | |
// | |
export const getData = ( | |
tableElement, | |
dupCols = true, | |
dupRows = true, | |
textMode = true, | |
) => { | |
if (!tableElement) { | |
return [] | |
} | |
// Initialize variables | |
const columns = [] | |
let currentX = 0 | |
let currentY = 0 | |
Array.from(tableElement.rows).forEach(row => { | |
currentY = 0 | |
// Handle both table haders and table cells | |
Array.from(row.cells).forEach(column => { | |
const { rowSpan, colSpan } = column | |
const content = textMode ? column.innerText.trim() : column.innerHTML | |
// Handle spanning cells | |
for (let x = 0; x < rowSpan; x++) { | |
for (let y = 0; y < colSpan; y++) { | |
if (columns[currentY + y] === undefined) { | |
columns[currentY + y] = [] | |
} | |
while (columns[currentY + y][currentX + x] !== undefined) { | |
currentY += 1 | |
if (columns[currentY + y] === undefined) { | |
columns[currentY + y] = [] | |
} | |
} | |
const condition = (x === 0 || dupRows) && (y === 0 || dupCols) | |
columns[currentY + y][currentX + x] = condition ? content : "" | |
} | |
} | |
currentY += 1; | |
}) | |
currentX += 1 | |
}) | |
// transpose matrix | |
return columns[0].map((_, i) => columns.map(row => row[i])) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment