{
const fileInput1 = html`<input type="file" />`;
const fileInput2 = html`<input type="file" />`;
const table1 = html` <table>
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody></tbody>
</table>`;
const table2 = table1.cloneNode(true);
const handleFileLoad = (table) => async (event) => {
const file = event.target.files[0];
const reader = new FileReader();
reader.readAsText(file);
const fileLoaded = new Promise((resolve, reject) => {
reader.onload = () => {
const contents = reader.result;
const lines = contents.trim().split("\n");
const headers = lines[0].split(",");
const numColumns = headers.length;
const numRows = lines.length - 1;
const numRowsRow = html`<tr>
<td>Number of rows:</td>
<td>${numRows}</td>
</tr>`;
const numColsRow = html`<tr>
<td>Number of columns:</td>
<td>${numColumns}</td>
</tr>`;
const headerRow = html`<tr>
<td>Column names:</td>
<td>${headers.map((h, i) => `${i}: ${h}`).join(", ")}</td>
</tr>`;
const tableBody = html`<tbody>
${numRowsRow}${numColsRow}${headerRow}
</tbody>`;
table.replaceChild(tableBody, table.lastChild);
resolve({ numRows, numColumns, headers });
};
reader.onerror = () => {
reject(reader.error);
};
});
try {
const { numRows, numColumns, headers } = await fileLoaded;
console.log(
`File loaded: ${file.name}, Rows: ${numRows}, Columns: ${numColumns}, Headers: ${headers}`
);
} catch (err) {
console.error(err);
}
};
fileInput1.addEventListener("change", handleFileLoad(table1));
fileInput2.addEventListener("change", handleFileLoad(table2));
return html`${fileInput1} ${table1} ${fileInput2} ${table2}`;
}