### Typst Converter Logic Source: https://github.com/hongjr03/excel-to-typst/blob/main/paste/index.html Handles the initialization of the Typst compiler, parsing of HTML table structures, and generation of Typst table syntax. ```javascript document.getElementById("typst").addEventListener("load", function () { $typst.setCompilerInitOptions({ getModule: () => "https://cdn.jsdelivr.net/npm/@myriaddreamin/typst-ts-web-compiler/pkg/typst_ts_web_compiler_bg.wasm", }); $typst.setRendererInitOptions({ getModule: () => "https://cdn.jsdelivr.net/npm/@myriaddreamin/typst-ts-renderer/pkg/typst_ts_renderer_bg.wasm", }); }); function getGlobalAlign() { const checkboxes = document.querySelectorAll( 'input[name="global-align"]:checked' ); const values = Array.from(checkboxes).map((cb) => cb.value); const horizontal = values.filter((v) => ["left", "center", "right"].includes(v) )[0]; const vertical = values.filter((v) => ["top", "horizon", "bottom"].includes(v) )[0]; return [horizontal, vertical].filter(Boolean).join("+"); } function convertToTypst() { const pasteArea = document.getElementById("paste-area"); const typstCode = document.getElementById("typst-code"); const globalAlign = getGlobalAlign(); const parseCellAlign = document.getElementById("parse-cell-align").checked; const tempDiv = document.createElement("div"); tempDiv.innerHTML = pasteArea.value; const style = tempDiv.querySelector("style"); const table = tempDiv.querySelector("table"); if (!table) { alert( "No valid table content found, please ensure the correct table is pasted." ); return; } const rows = table.rows; const firstRow = rows[0]; const columnCount = Array.from(firstRow.cells).reduce((total, cell) => { return total + (cell.colSpan || 1); }, 0); let typstOutput = `#table(columns: ${columnCount},`; if (globalAlign) { typstOutput += ` align: ${globalAlign},`; } typstOutput += "\n"; for (let i = 0; i < rows.length; i++) { const cells = rows[i].cells; typstOutput += " "; for (let j = 0; j < columnCount; j++) { if (j < cells.length) { const cell = cells[j]; const rowspan = cell.rowSpan > 1 ? `rowspan: ${cell.rowSpan}, ` : ""; const colspan = cell.colSpan > 1 ? `colspan: ${cell.colSpan}, ` : ""; let alignParts = []; if (parseCellAlign) { if (cell.className) { const classes = cell.className.split(" "); for (const className of classes) { if (style) { const regex = new RegExp(`\\.${className}\\s*{([^}]+)}`); const match = style.innerHTML.match(regex); if (match) { const styleParts = match[1].split(";"); for (const stylePart of styleParts) { const [key, value] = stylePart.split(":"); if (key === "text-align") { alignParts.push(value.trim()); } if (key === "vertical-align") { alignParts.push(value.trim()); } } } } } } else { const valign = cell.getAttribute("valign"); if (valign) { alignParts.push(valign === "middle" ? "horizon" : valign); } const p = cell.querySelector("p"); if (p && p.getAttribute("align")) { alignParts.push(p.getAttribute("align")); } } } const align = alignParts.length > 0 ? `align: ${alignParts.join("+")}, ` : ""; if (rowspan || colspan || align) { typstOutput += `table.cell(${rowspan}${colspan}${align})[${cell.textContent.trim()}], `; } else { typstOutput += `[${cell.textContent.trim()}], `; } } } typstOutput += "\n"; } typstOutput += ")"; typstCode.value = typstOutput; $typst .svg({ mainContent: typstOutput }) .then((svg) => { console.log(`rendered! SvgElement { len: ${svg.length} }`); document.getElementById("preview-content").innerHTML = svg; }) .catch((error) => { console.error("Preview generation failed:", error); document.getElementById("preview-content").innerHTML = '

Preview generation failed. Please check the Typst code for errors.

'; }); } document .getElementById("paste-area") .addEventListener("paste", function (e) { e.preventDefault(); const text = e.clipboardData.getData("text/html") || e.clipboardData.getData("text/plain"); this.value = text; }); const globalAlignCheckboxes = document.querySelectorAll( 'input[name="global-align"]' ); globalAlignCheckboxes.forEach((checkbox) => { checkbox.addEventListener("change", function () { const isHorizontal = ["left", "center", "right"].includes(this.value); const isVertical = ["top", "horizon", "bottom"].includes(this.value); globalAlignCheckboxes.forEach((cb) => { if (cb !== this) { if ( (isHorizontal && ["left", "center", "right"].includes(cb.value)) || (isVertical && ["top", "horizon", "bottom"].includes(cb.value)) ) { cb.checked = false; } } }); }); }); ``` -------------------------------- ### Initialize Typst Code Input Listener Source: https://github.com/hongjr03/excel-to-typst/blob/main/index.html Attaches an event listener to the Typst code textarea to trigger rendering on input. ```javascript document .getElementById("typst-code") .addEventListener("input", function () { renderTypst(this.value); }); ``` -------------------------------- ### Handle Excel File Upload and Sheet Selection Source: https://github.com/hongjr03/excel-to-typst/blob/main/index.html Reads an uploaded Excel file and populates a dropdown menu with available sheet names. ```javascript function handleUpload() { const fileInput = document.getElementById("file-input"); const file = fileInput.files[0]; if (!file || !file.name.endsWith(".xlsx")) { alert("Please select an Excel file"); return; } const reader = new FileReader(); reader.onload = function (e) { const data = new Uint8Array(e.target.result); const workbook = XLSX.read(data, { type: "array" }); const sheetNames = workbook.SheetNames; const sheetSelect = document.getElementById("sheet-select"); sheetSelect.innerHTML = ""; sheetNames.forEach((sheetName) => { const option = document.createElement("option"); option.value = sheetName; option.text = sheetName; sheetSelect.appendChild(option); }); }; reader.readAsArrayBuffer(file); } ``` -------------------------------- ### Convert Excel Worksheet to Typst Code Source: https://github.com/hongjr03/excel-to-typst/blob/main/index.html Processes the selected worksheet and generates the corresponding Typst table markup. ```javascript function handleConvert() { const fileInput = document.getElementById("file-input"); const file = fileInput.files[0]; if (!file || !file.name.endsWith(".xlsx")) { alert("Please select an Excel file"); return; } const reader = new FileReader(); reader.onload = function (e) { const data = new Uint8Array(e.target.result); const workbook = XLSX.read(data, { type: "array" }); const sheetName = document.getElementById("sheet-select").value; const worksheet = workbook.Sheets[sheetName]; const typstCode = convertToTypst(worksheet); document.getElementById("typst-code").value = typstCode; }; reader.readAsArrayBuffer(file); } ``` ```javascript function convertToTypst(worksheet) { const range = XLSX.utils.decode_range(worksheet["!ref"]); const columns = range.e.c - range.s.c + 1; const merges = worksheet["!merges"] || []; let typstCode = `#table(columns: ${columns}, `; for (let row = range.s.r; row <= range.e.r; row++) { typstCode += " "; for (let col = range.s.c; col <= range.e.c; col++) { const cellAddress = XLSX.utils.encode_cell({ r: row, c: col }); const mergeInfo = findMergeInfo(merges, row, col); if (mergeInfo) { if (mergeInfo.s.r === row && mergeInfo.s.c === col) { const cell = worksheet[cellAddress]; const value = cell ? cell.v : ""; const rowspan = mergeInfo.e.r - mergeInfo.s.r + 1; const colspan = mergeInfo.e.c - mergeInfo.s.c + 1; typstCode += "table.cell("; if (rowspan > 1) { typstCode += `rowspan: ${rowspan}, `; } if (colspan > 1) { typstCode += `colspan: ${colspan}, `; } typstCode = typstCode.replace(/,\s*$/, ""); typstCode += `)[${value}], `; } } else { const cell = worksheet[cellAddress]; const value = cell ? cell.v : ""; typstCode += `[${value}], `; } } typstCode += "\n"; } typstCode += ")"; return typstCode; } ``` ```javascript function findMergeInfo(merges, row, col) { return merges.find( (merge) => row >= merge.s.r && row <= merge.e.r && col >= merge.s.c && col <= merge.e.c ); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.