Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/xlsx-to-table/demo-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "XLSX to Table",
"description": "Import spreadsheet data and insert as tables in SuperDoc",
"category": "advanced"
}
12 changes: 12 additions & 0 deletions examples/xlsx-to-table/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>XLSX to Table Demo - SuperDoc</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions examples/xlsx-to-table/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "xlsx-to-table-demo",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "vite"
},
"dependencies": {
"superdoc": "^1.8.3",
"vue": "^3.5.13",
"xlsx": "^0.18.5"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.2.3",
"vite": "^4.4.6"
}
}
187 changes: 187 additions & 0 deletions examples/xlsx-to-table/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<template>
<div class="app">
<header>
<h1>XLSX to Table Demo</h1>
<div class="actions">
<button @click="docInput?.click()">Load Document</button>
<input
type="file"
ref="docInput"
accept=".docx"
class="hidden"
@change="handleDocChange"
>
<button @click="xlsxInput?.click()" :disabled="!editorReady">
Import XLSX
</button>
<input
type="file"
ref="xlsxInput"
accept=".xlsx,.xls,.csv"
class="hidden"
@change="handleXlsxChange"
>
</div>
</header>

<main>
<DocumentEditor
ref="documentEditor"
:initial-data="documentFile"
@editor-ready="handleEditorReady"
/>
</main>
</div>
</template>

<script setup>
import { ref } from 'vue';
import * as XLSX from 'xlsx';
import DocumentEditor from './components/DocumentEditor.vue';

const documentFile = ref(null);
const docInput = ref(null);
const xlsxInput = ref(null);
const documentEditor = ref(null);
const editorReady = ref(false);
let editorInstance = null;

const handleDocChange = (event) => {
const file = event.target.files?.[0];
if (file) {
documentFile.value = file;
editorReady.value = false;
editorInstance = null;
}
};

const handleEditorReady = (superdoc) => {
editorInstance = superdoc?.activeEditor;
editorReady.value = !!editorInstance;
};

const handleXlsxChange = async (event) => {
const file = event.target.files?.[0];
if (!file || !editorInstance) return;

try {
const arrayBuffer = await file.arrayBuffer();
const workbook = XLSX.read(arrayBuffer, { type: 'array' });

const firstSheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[firstSheetName];

const rawData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });

// Filter out empty rows and convert all values to strings
const data = rawData
.filter(row => row.some(cell => cell !== null && cell !== undefined && cell !== ''))
.map(row => row.map(cell => String(cell ?? '')));

if (data.length === 0) {
alert('No data found in spreadsheet');
return;
}

insertTable(data);
} catch (error) {
console.error('Failed to parse spreadsheet:', error);
alert('Failed to parse spreadsheet file');
}

event.target.value = '';
};

const insertTable = (data) => {
const cols = Math.max(...data.map(row => row.length));

// Normalize rows to have consistent column count
const normalizedData = data.map(row => {
const normalized = [...row];
while (normalized.length < cols) {
normalized.push('');
}
return normalized;
});

// Insert a template table with 1 row
editorInstance.commands.insertTable({ rows: 1, cols, withHeaderRow: false });

// Find the newly inserted table
const tables = editorInstance.getNodesOfType('table');
if (!tables?.length) return;

const tablePos = tables[tables.length - 1].pos;
Comment on lines +111 to +114

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Appending data can target the wrong table

The inserted table is located by taking the last table in the document. If the cursor is in the middle of a document that already has a table after it, tables[tables.length - 1] will point to an existing table instead of the one just inserted, so the XLSX rows will append to the wrong table. This occurs whenever users import into a document that already contains later tables.

Useful? React with 👍 / 👎.


// Append all data rows
editorInstance.commands.appendRowsWithContent({
tablePos,
valueRows: normalizedData,
copyRowStyle: false
});

// Delete the empty template row
editorInstance.commands.deleteRow();
Comment on lines +123 to +124

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Delete row call is a no-op outside table

The example inserts the table and then immediately calls deleteRow, but insertTable moves the selection after the new table and appendRowsWithContent does not move it. The table command deletes the row containing the current selection (see packages/super-editor/src/extensions/table/table.js), so this call will typically do nothing and leave the empty template row in the output. This shows up any time a user imports a sheet because the cursor is not inside the table when deleteRow runs.

Useful? React with 👍 / 👎.

};
</script>

<style>
* {
box-sizing: border-box;
}

.app {
height: 100vh;
display: flex;
flex-direction: column;
}

header {
padding: 1rem;
background: #f5f5f5;
display: flex;
align-items: center;
gap: 1rem;
border-bottom: 1px solid #ddd;
}

header h1 {
margin: 0;
font-size: 1.25rem;
}

.actions {
display: flex;
gap: 0.5rem;
margin-left: auto;
}

header button {
padding: 0.5rem 1rem;
background: #1355ff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 0.875rem;
}

header button:hover:not(:disabled) {
background: #0044ff;
}

header button:disabled {
background: #999;
cursor: not-allowed;
}

.hidden {
display: none;
}

main {
flex: 1;
padding: 1rem;
overflow: hidden;
}
</style>
101 changes: 101 additions & 0 deletions examples/xlsx-to-table/src/components/DocumentEditor.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<template>
<div class="document-editor">
<div :key="documentKey" class="editor-container">
<div id="superdoc-toolbar" class="toolbar"></div>
<div id="superdoc" class="editor"></div>
</div>
</div>
</template>

<script setup>
import { onMounted, onUnmounted, ref, watch } from 'vue';
import { SuperDoc } from 'superdoc';
import 'superdoc/style.css';

const props = defineProps({
initialData: {
type: [File, Blob],
default: null
},
readOnly: {
type: Boolean,
default: false
}
});

const emit = defineEmits(['editor-ready', 'editor-error']);

let superdocInstance = null;
const documentKey = ref(0);

const destroyEditor = () => {
if (superdocInstance) {
superdocInstance = null;
}
};

const initializeEditor = async () => {
try {
destroyEditor();
documentKey.value++;

superdocInstance = new SuperDoc({
selector: '#superdoc',
toolbar: '#superdoc-toolbar',
document: props.initialData,
documentMode: props.readOnly ? 'viewing' : 'editing',
pagination: true,
rulers: true,
onReady: ({ superdoc }) => {
emit('editor-ready', superdoc);
},
});
} catch (error) {
console.error('Failed to initialize editor:', error);
emit('editor-error', error);
}
};

watch(
() => [props.initialData, props.readOnly],
() => {
initializeEditor();
}
);

onMounted(() => {
initializeEditor();
});

onUnmounted(() => {
destroyEditor();
});

defineExpose({
getSuperdoc: () => superdocInstance
});
</script>

<style scoped>
.document-editor {
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
}

.toolbar {
flex: 0 0 auto;
border-bottom: 1px solid #eee;
min-height: 40px;
}

.editor {
display: flex;
justify-content: center;
flex: 1 1 auto;
overflow: auto;
margin-top: 10px;
min-height: 400px;
}
</style>
4 changes: 4 additions & 0 deletions examples/xlsx-to-table/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createApp } from 'vue';
import App from './App.vue';

createApp(App).mount('#app');
9 changes: 9 additions & 0 deletions examples/xlsx-to-table/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
plugins: [vue()],
optimizeDeps: {
include: ['superdoc', 'xlsx']
}
});
Loading