Update: support direct table paste from Excel
This commit is contained in:
+116
-390
@@ -3,439 +3,165 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Excel → Markdown 转换器</title>
|
<title>Excel 转 Markdown 表格</title>
|
||||||
<style>
|
<script src="https://cdn.tailwindcss.com"></script>
|
||||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
||||||
|
<script>
|
||||||
:root {
|
tailwind.config = {
|
||||||
--bg: #0f0f0f;
|
theme: {
|
||||||
--surface: #1a1a1a;
|
extend: {
|
||||||
--surface2: #252525;
|
colors: {
|
||||||
--border: #333;
|
primary: '#165DFF',
|
||||||
--text: #e0e0e0;
|
secondary: '#64748b',
|
||||||
--text-dim: #888;
|
},
|
||||||
--accent: #3b82f6;
|
|
||||||
--accent-hover: #60a5fa;
|
|
||||||
--success: #22c55e;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
body {
|
|
||||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
|
||||||
background: var(--bg);
|
|
||||||
color: var(--text);
|
|
||||||
height: 100vh;
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
header {
|
|
||||||
padding: 14px 24px;
|
|
||||||
border-bottom: 1px solid var(--border);
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 12px;
|
|
||||||
background: var(--surface);
|
|
||||||
}
|
}
|
||||||
|
</script>
|
||||||
header h1 {
|
<style type="text/tailwindcss">
|
||||||
font-size: 16px;
|
@layer utilities {
|
||||||
font-weight: 600;
|
.table-fixed-layout {
|
||||||
color: var(--text);
|
table-layout: fixed;
|
||||||
}
|
}
|
||||||
|
.paste-area table {
|
||||||
header p {
|
|
||||||
font-size: 13px;
|
|
||||||
color: var(--text-dim);
|
|
||||||
}
|
|
||||||
|
|
||||||
.toolbar {
|
|
||||||
margin-left: auto;
|
|
||||||
display: flex;
|
|
||||||
gap: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn {
|
|
||||||
padding: 6px 14px;
|
|
||||||
border-radius: 6px;
|
|
||||||
border: none;
|
|
||||||
font-size: 13px;
|
|
||||||
font-weight: 500;
|
|
||||||
cursor: pointer;
|
|
||||||
transition: all 0.15s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn-primary {
|
|
||||||
background: var(--accent);
|
|
||||||
color: #fff;
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn-primary:hover { background: var(--accent-hover); }
|
|
||||||
|
|
||||||
.btn-ghost {
|
|
||||||
background: transparent;
|
|
||||||
color: var(--text-dim);
|
|
||||||
border: 1px solid var(--border);
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn-ghost:hover { background: var(--surface2); color: var(--text); }
|
|
||||||
|
|
||||||
main {
|
|
||||||
flex: 1;
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: 1fr 1fr;
|
|
||||||
gap: 0;
|
|
||||||
overflow: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
.panel {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
overflow: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
.panel-left {
|
|
||||||
border-right: 1px solid var(--border);
|
|
||||||
}
|
|
||||||
|
|
||||||
.panel-header {
|
|
||||||
padding: 10px 16px;
|
|
||||||
background: var(--surface);
|
|
||||||
border-bottom: 1px solid var(--border);
|
|
||||||
font-size: 12px;
|
|
||||||
font-weight: 600;
|
|
||||||
text-transform: uppercase;
|
|
||||||
letter-spacing: 0.05em;
|
|
||||||
color: var(--text-dim);
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.panel-header .dot {
|
|
||||||
width: 6px; height: 6px;
|
|
||||||
border-radius: 50%;
|
|
||||||
background: var(--accent);
|
|
||||||
}
|
|
||||||
|
|
||||||
textarea {
|
|
||||||
flex: 1;
|
|
||||||
background: var(--surface);
|
|
||||||
color: var(--text);
|
|
||||||
border: none;
|
|
||||||
outline: none;
|
|
||||||
resize: none;
|
|
||||||
padding: 16px;
|
|
||||||
font-family: 'SF Mono', 'Cascadia Code', 'Fira Code', monospace;
|
|
||||||
font-size: 13px;
|
|
||||||
line-height: 1.6;
|
|
||||||
}
|
|
||||||
|
|
||||||
textarea::placeholder { color: var(--text-dim); }
|
|
||||||
|
|
||||||
.panel-right {
|
|
||||||
background: var(--surface);
|
|
||||||
}
|
|
||||||
|
|
||||||
.output-wrap {
|
|
||||||
flex: 1;
|
|
||||||
overflow-y: auto;
|
|
||||||
padding: 16px;
|
|
||||||
}
|
|
||||||
|
|
||||||
#output {
|
|
||||||
font-family: 'SF Mono', 'Cascadia Code', 'Fira Code', monospace;
|
|
||||||
font-size: 13px;
|
|
||||||
line-height: 1.7;
|
|
||||||
white-space: pre-wrap;
|
|
||||||
word-break: break-word;
|
|
||||||
}
|
|
||||||
|
|
||||||
.preview-toggle {
|
|
||||||
display: flex;
|
|
||||||
gap: 4px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.preview-toggle button {
|
|
||||||
padding: 4px 10px;
|
|
||||||
border-radius: 4px;
|
|
||||||
border: 1px solid var(--border);
|
|
||||||
background: transparent;
|
|
||||||
color: var(--text-dim);
|
|
||||||
font-size: 12px;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
.preview-toggle button.active {
|
|
||||||
background: var(--accent);
|
|
||||||
color: #fff;
|
|
||||||
border-color: var(--accent);
|
|
||||||
}
|
|
||||||
|
|
||||||
.md-preview {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.md-preview.active { display: block; }
|
|
||||||
|
|
||||||
table.md-table {
|
|
||||||
border-collapse: collapse;
|
border-collapse: collapse;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
font-size: 13px;
|
|
||||||
}
|
}
|
||||||
|
.paste-area td, .paste-area th {
|
||||||
table.md-table th,
|
border: 1px solid #cbd5e1;
|
||||||
table.md-table td {
|
padding: 6px 10px;
|
||||||
border: 1px solid var(--border);
|
min-width: 80px;
|
||||||
padding: 6px 12px;
|
|
||||||
text-align: left;
|
|
||||||
}
|
}
|
||||||
|
.paste-area th {
|
||||||
table.md-table th {
|
background-color: #f1f5f9;
|
||||||
background: var(--surface2);
|
|
||||||
font-weight: 600;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
table.md-table tr:nth-child(even) td {
|
|
||||||
background: var(--bg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.status {
|
|
||||||
font-size: 11px;
|
|
||||||
color: var(--text-dim);
|
|
||||||
margin-left: auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
.status.success { color: var(--success); }
|
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body class="bg-slate-50 min-h-screen">
|
||||||
|
<div class="container mx-auto px-4 py-8 max-w-7xl">
|
||||||
<header>
|
<header class="text-center mb-8">
|
||||||
<div>
|
<h1 class="text-3xl font-bold text-slate-800 mb-2">
|
||||||
<h1>Excel → Markdown 转换器</h1>
|
<i class="fa-solid fa-table text-primary mr-2"></i>Excel → Markdown 表格转换
|
||||||
<p>粘贴 Excel 表格内容,自动生成 Markdown 表格</p>
|
</h1>
|
||||||
</div>
|
<p class="text-secondary">直接粘贴 Excel / 网页表格,左侧实时预览,右侧自动生成 Markdown</p>
|
||||||
<div class="toolbar">
|
|
||||||
<div class="preview-toggle">
|
|
||||||
<button id="btn-markdown" class="active">Markdown</button>
|
|
||||||
<button id="btn-preview">预览</button>
|
|
||||||
</div>
|
|
||||||
<button class="btn btn-ghost" id="btn-copy">复制 Markdown</button>
|
|
||||||
<button class="btn btn-primary" id="btn-convert">转换</button>
|
|
||||||
</div>
|
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main>
|
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
||||||
<div class="panel panel-left">
|
<!-- 左侧:可粘贴表格区域 -->
|
||||||
<div class="panel-header">
|
<div class="bg-white rounded-lg shadow p-5">
|
||||||
<span class="dot"></span>
|
<div class="flex justify-between items-center mb-3">
|
||||||
Excel / CSV 内容
|
<h2 class="text-xl font-semibold">表格预览</h2>
|
||||||
<span class="status" id="status-left"></span>
|
<button id="clearLeft" class="px-3 py-1.5 bg-slate-200 rounded hover:bg-slate-300 transition">
|
||||||
|
<i class="fa-solid fa-trash mr-1"></i>清空
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div id="pasteArea" class="paste-area border border-slate-300 rounded p-3 h-[calc(100vh-240px)] overflow-auto bg-white">
|
||||||
|
<div class="text-slate-400 text-center py-10">在此粘贴 Excel 或网页表格内容</div>
|
||||||
</div>
|
</div>
|
||||||
<textarea
|
|
||||||
id="input"
|
|
||||||
placeholder="在这里粘贴 Excel 表格内容(支持从 Excel、Numbers、Google Sheets 直接粘贴)
|
|
||||||
|
|
||||||
💡 粘贴后会自动转换,也可以点击「转换」按钮手动触发"
|
|
||||||
></textarea>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="panel panel-right">
|
<!-- 右侧:Markdown 输出 -->
|
||||||
<div class="panel-header">
|
<div class="bg-white rounded-lg shadow p-5">
|
||||||
<span class="dot" style="background: var(--success)"></span>
|
<div class="flex justify-between items-center mb-3">
|
||||||
Markdown 输出
|
<h2 class="text-xl font-semibold">Markdown 输出</h2>
|
||||||
<span class="status" id="status-right"></span>
|
<button id="copyMd" class="px-3 py-1.5 bg-primary text-white rounded hover:opacity-90 transition">
|
||||||
|
<i class="fa-solid fa-copy mr-1"></i>复制
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="output-wrap">
|
<pre id="mdOutput" class="border border-slate-300 rounded p-3 h-[calc(100vh-240px)] overflow-auto bg-slate-50 whitespace-pre-wrap"></pre>
|
||||||
<div id="output-raw" class="md-preview"></div>
|
</div>
|
||||||
<div id="output" class="md-preview active"></div>
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-6 p-4 bg-white rounded shadow text-secondary">
|
||||||
|
<h3 class="font-semibold mb-2"><i class="fa-solid fa-circle-info text-primary mr-1"></i>使用说明</h3>
|
||||||
|
<ul class="list-disc pl-5 space-y-1">
|
||||||
|
<li>直接复制 Excel 或网页里的表格,粘贴到左侧区域</li>
|
||||||
|
<li>左侧会自动渲染成可视化表格,格式不会丢失</li>
|
||||||
|
<li>右侧实时生成标准 Markdown 表格</li>
|
||||||
|
<li>一键复制,可直接用于 Notion、GitHub、语雀、Obsidian</li>
|
||||||
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
const $ = id => document.getElementById(id);
|
const pasteArea = document.getElementById('pasteArea');
|
||||||
const input = $('input');
|
const mdOutput = document.getElementById('mdOutput');
|
||||||
const outputRaw = $('output-raw');
|
const clearLeft = document.getElementById('clearLeft');
|
||||||
const output = $('output');
|
const copyMd = document.getElementById('copyMd');
|
||||||
const statusLeft = $('status-left');
|
|
||||||
const statusRight = $('status-right');
|
|
||||||
|
|
||||||
let convertMode = 'markdown'; // 'markdown' or 'preview'
|
// 粘贴监听(核心)
|
||||||
|
pasteArea.addEventListener('paste', (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
const html = e.clipboardData.getData('text/html');
|
||||||
|
const text = e.clipboardData.getData('text');
|
||||||
|
|
||||||
function excelToMarkdown(text) {
|
if (html && html.includes('<table')) {
|
||||||
const lines = text.trim().split('\n');
|
pasteArea.innerHTML = html;
|
||||||
if (lines.length === 0) return '';
|
|
||||||
|
|
||||||
const rows = lines.map(line => {
|
|
||||||
// Split by tab, handling quoted fields
|
|
||||||
const cells = [];
|
|
||||||
let current = '';
|
|
||||||
let inQuotes = false;
|
|
||||||
|
|
||||||
for (let i = 0; i < line.length; i++) {
|
|
||||||
const ch = line[i];
|
|
||||||
if (ch === '"') {
|
|
||||||
inQuotes = !inQuotes;
|
|
||||||
} else if (ch === '\t' && !inQuotes) {
|
|
||||||
cells.push(current.trim());
|
|
||||||
current = '';
|
|
||||||
} else {
|
} else {
|
||||||
current += ch;
|
pasteArea.innerHTML = textToTable(text);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
cells.push(current.trim());
|
setTimeout(renderMarkdown, 10);
|
||||||
return cells;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (rows.length === 0) return '';
|
// 纯文本转表格(Excel 粘贴纯文本也能识别)
|
||||||
|
function textToTable(text) {
|
||||||
// Detect delimiter: try tab first, fall back to comma or semicolon
|
const rows = text.trim().split(/\r?\n/).filter(i => i);
|
||||||
const firstRow = rows[0];
|
let html = '<table>';
|
||||||
const allTabs = rows.every(r => r.length === firstRow.length && firstRow.every(c => c.includes('\t') || c.trim() === ''));
|
|
||||||
|
|
||||||
// Normalize: handle if the split by tab didn't work well
|
|
||||||
// Fallback: split by comma if no tabs found
|
|
||||||
const normalizedRows = firstRow.length <= 1 && lines[0].includes(',')
|
|
||||||
? lines.map(line => line.split(',').map(c => c.trim().replace(/^"|"$/g, '')))
|
|
||||||
: rows;
|
|
||||||
|
|
||||||
const colCount = normalizedRows[0].length;
|
|
||||||
const justified = normalizedRows.map(row => {
|
|
||||||
while (row.length < colCount) row.push('');
|
|
||||||
return row.slice(0, colCount);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Build markdown
|
|
||||||
const header = justified[0];
|
|
||||||
const separator = header.map(() => '---');
|
|
||||||
|
|
||||||
const mdRows = [header, separator, ...justified.slice(1)];
|
|
||||||
|
|
||||||
// Find max width per column
|
|
||||||
const colWidths = [];
|
|
||||||
for (let c = 0; c < colCount; c++) {
|
|
||||||
let max = 0;
|
|
||||||
for (let r = 0; r < mdRows.length; r++) {
|
|
||||||
const cell = mdRows[r][c] || '';
|
|
||||||
const len = cell.replace(/\|/g, '\\|').length;
|
|
||||||
if (len > max) max = len;
|
|
||||||
}
|
|
||||||
colWidths.push(max);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build the table string
|
|
||||||
const separatorRow = '| ' + colWidths.map(w => '-'.repeat(w)).join(' | ') + ' |';
|
|
||||||
|
|
||||||
const lines_out = mdRows.map((row, ri) => {
|
|
||||||
const cells = row.map((cell, ci) => {
|
|
||||||
const cleaned = cell.replace(/\|/g, '\\|');
|
|
||||||
const padded = ri === 1 ? separatorRow : (cleaned + ' '.repeat(colWidths[ci] - cleaned.replace(/\|/g, '\\|').length));
|
|
||||||
return padded;
|
|
||||||
});
|
|
||||||
return '| ' + cells.join(' | ') + ' |';
|
|
||||||
});
|
|
||||||
|
|
||||||
return lines_out.join('\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderMarkdownTable(markdown) {
|
|
||||||
const lines = markdown.split('\n').filter(l => l.trim());
|
|
||||||
if (lines.length < 2) return markdown;
|
|
||||||
|
|
||||||
// Simple markdown table renderer
|
|
||||||
const rows = [];
|
|
||||||
let header = [];
|
|
||||||
let inBody = false;
|
|
||||||
|
|
||||||
for (const line of lines) {
|
|
||||||
if (line.startsWith('|') && line.endsWith('|')) {
|
|
||||||
const cells = line.split('|').slice(1, -1).map(c => c.trim());
|
|
||||||
if (cells.every(c => c.match(/^-+$/))) {
|
|
||||||
inBody = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!inBody) {
|
|
||||||
header = cells;
|
|
||||||
} else {
|
|
||||||
rows.push(cells);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (header.length === 0) return markdown;
|
|
||||||
|
|
||||||
let html = '<table class="md-table"><thead><tr>';
|
|
||||||
header.forEach(h => html += `<th>${escapeHtml(h)}</th>`);
|
|
||||||
html += '</tr></thead><tbody>';
|
|
||||||
rows.forEach(row => {
|
rows.forEach(row => {
|
||||||
html += '<tr>';
|
html += '<tr>';
|
||||||
row.forEach(cell => html += `<td>${escapeHtml(cell)}</td>`);
|
row.split('\t').forEach(cell => {
|
||||||
|
html += '<td>' + cell + '</td>';
|
||||||
|
});
|
||||||
html += '</tr>';
|
html += '</tr>';
|
||||||
});
|
});
|
||||||
html += '</tbody></table>';
|
html += '</table>';
|
||||||
return html;
|
return html;
|
||||||
}
|
}
|
||||||
|
|
||||||
function escapeHtml(str) {
|
// 把左侧表格转成 Markdown
|
||||||
return str.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
|
function renderMarkdown() {
|
||||||
}
|
const table = pasteArea.querySelector('table');
|
||||||
|
if (!table) {
|
||||||
function convert() {
|
mdOutput.textContent = '请粘贴表格内容';
|
||||||
const text = input.value;
|
|
||||||
if (!text.trim()) {
|
|
||||||
output.textContent = '';
|
|
||||||
outputRaw.textContent = '';
|
|
||||||
statusRight.textContent = '';
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const markdown = excelToMarkdown(text);
|
let md = '';
|
||||||
outputRaw.textContent = markdown;
|
const rows = table.querySelectorAll('tr');
|
||||||
output.innerHTML = renderMarkdownTable(markdown);
|
|
||||||
statusRight.textContent = '已转换';
|
rows.forEach((tr, idx) => {
|
||||||
statusRight.className = 'status success';
|
const cells = tr.querySelectorAll('td, th');
|
||||||
setTimeout(() => { statusRight.textContent = ''; statusRight.className = 'status'; }, 2000);
|
const rowContent = Array.from(cells).map(c => c.innerText.trim());
|
||||||
|
|
||||||
|
md += '| ' + rowContent.join(' | ') + ' |\n';
|
||||||
|
|
||||||
|
// 表头分隔线
|
||||||
|
if (idx === 0) {
|
||||||
|
md += '| ' + rowContent.map(() => '---').join(' | ') + ' |\n';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
mdOutput.textContent = md;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auto-convert on paste
|
// 清空
|
||||||
input.addEventListener('input', () => {
|
clearLeft.addEventListener('click', () => {
|
||||||
statusLeft.textContent = input.value.split('\n').length + ' 行';
|
pasteArea.innerHTML = '<div class="text-slate-400 text-center py-10">在此粘贴 Excel 或网页表格内容</div>';
|
||||||
convert();
|
mdOutput.textContent = '';
|
||||||
});
|
});
|
||||||
|
|
||||||
// Manual convert
|
// 复制
|
||||||
$('btn-convert').addEventListener('click', convert);
|
copyMd.addEventListener('click', async () => {
|
||||||
|
const content = mdOutput.textContent;
|
||||||
// Copy
|
if (!content.trim()) return alert('无内容可复制');
|
||||||
$('btn-copy').addEventListener('click', () => {
|
await navigator.clipboard.writeText(content);
|
||||||
const text = outputRaw.textContent || output.textContent;
|
copyMd.innerHTML = '<i class="fa-solid fa-check mr-1"></i>已复制';
|
||||||
if (!text) return;
|
setTimeout(() => {
|
||||||
navigator.clipboard.writeText(text).then(() => {
|
copyMd.innerHTML = '<i class="fa-solid fa-copy mr-1"></i>复制';
|
||||||
const btn = $('btn-copy');
|
}, 1500);
|
||||||
const orig = btn.textContent;
|
|
||||||
btn.textContent = '已复制!';
|
|
||||||
setTimeout(() => btn.textContent = orig, 1500);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Toggle preview
|
|
||||||
$('btn-markdown').addEventListener('click', () => {
|
|
||||||
convertMode = 'markdown';
|
|
||||||
$('btn-markdown').classList.add('active');
|
|
||||||
$('btn-preview').classList.remove('active');
|
|
||||||
outputRaw.classList.add('active');
|
|
||||||
output.classList.remove('active');
|
|
||||||
});
|
|
||||||
|
|
||||||
$('btn-preview').addEventListener('click', () => {
|
|
||||||
convertMode = 'preview';
|
|
||||||
$('btn-preview').classList.add('active');
|
|
||||||
$('btn-markdown').classList.remove('active');
|
|
||||||
output.classList.add('active');
|
|
||||||
outputRaw.classList.remove('active');
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user