mirror of
https://github.com/pipipi-pikachu/PPTist.git
synced 2025-04-15 02:20:00 +08:00
fix: 粘贴excel数据超过表格本身范围时报错
This commit is contained in:
parent
c8e7360312
commit
2391f56f20
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,7 +1,6 @@
|
||||
.DS_Store
|
||||
node_modules
|
||||
/dist
|
||||
*.lock
|
||||
|
||||
# local env files
|
||||
.env.local
|
||||
|
@ -53,29 +53,20 @@ export const pasteCustomClipboardString = (text: string) => {
|
||||
return clipboardData
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查粘贴元素是否为外部exlc表格类型
|
||||
* @param text
|
||||
* @returns
|
||||
*/
|
||||
export const exlcTesting = (text: string): string[][] | boolean => {
|
||||
// 判定一下是不是exl格式
|
||||
const lineList: string[] = text.split('\r\n')
|
||||
// 按照\n拆分表格 最后会多出一个空字串
|
||||
if (lineList[lineList.length - 1] === '') {
|
||||
lineList.splice(length - 1, 1)
|
||||
// 尝试解析剪贴板内容是否为Excel表格(或类似的)数据格式
|
||||
export const pasteExcelClipboardString = (text: string): string[][] | boolean => {
|
||||
const lines: string[] = text.split('\r\n')
|
||||
|
||||
if (lines[lines.length - 1] === '') lines.pop()
|
||||
|
||||
let colCount = -1
|
||||
const data: string[][] = []
|
||||
for (const index in lines) {
|
||||
data[index] = lines[index].split('\t')
|
||||
|
||||
if (data[index].length === 1) return false
|
||||
if (colCount === -1) colCount = data[index].length
|
||||
else if (colCount !== data[index].length) return false
|
||||
}
|
||||
let tNum = -1
|
||||
const exlc: string[][] = []
|
||||
for (const index in lineList) {
|
||||
exlc[index] = lineList[index].split('\t')
|
||||
if (exlc[index].length === 1) return false
|
||||
if (tNum === -1) {
|
||||
tNum = exlc[index].length
|
||||
}
|
||||
else if (tNum !== exlc[index].length) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return exlc
|
||||
return data
|
||||
}
|
@ -12,7 +12,7 @@
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent, onUnmounted, ref, watch } from 'vue'
|
||||
import { pasteCustomClipboardString, exlcTesting } from '@/utils/clipboard'
|
||||
import { pasteCustomClipboardString, pasteExcelClipboardString } from '@/utils/clipboard'
|
||||
|
||||
export default defineComponent({
|
||||
name: 'custom-textarea',
|
||||
@ -58,16 +58,16 @@ export default defineComponent({
|
||||
|
||||
if (clipboardDataFirstItem && clipboardDataFirstItem.kind === 'string' && clipboardDataFirstItem.type === 'text/plain') {
|
||||
clipboardDataFirstItem.getAsString(text => {
|
||||
// exlc数据格式解析
|
||||
const exlc = exlcTesting(text)
|
||||
if (exlc) {
|
||||
emit('updateExlc', exlc)
|
||||
if (textareaRef.value) textareaRef.value.innerHTML = exlc[0][0]
|
||||
const clipboardData = pasteCustomClipboardString(text)
|
||||
if (typeof clipboardData === 'object') return
|
||||
|
||||
const excelData = pasteExcelClipboardString(text)
|
||||
if (excelData) {
|
||||
emit('insertExcelData', excelData)
|
||||
if (textareaRef.value) textareaRef.value.innerHTML = excelData[0][0]
|
||||
return
|
||||
}
|
||||
|
||||
const clipboardData = pasteCustomClipboardString(text)
|
||||
if (typeof clipboardData === 'object') return
|
||||
emit('updateValue', text)
|
||||
document.execCommand('insertText', false, text)
|
||||
})
|
||||
|
@ -61,7 +61,7 @@
|
||||
contenteditable="plaintext-only"
|
||||
:value="cell.text"
|
||||
@updateValue="value => handleInput(value, rowIndex, colIndex)"
|
||||
@updateExlc="exlc => handleExlcInput(exlc, rowIndex, colIndex)"
|
||||
@insertExcelData="value => insertExcelData(value, rowIndex, colIndex)"
|
||||
/>
|
||||
<div v-else class="cell-text" v-html="formatText(cell.text)" />
|
||||
</td>
|
||||
@ -510,12 +510,18 @@ export default defineComponent({
|
||||
emit('change', tableCells.value)
|
||||
}, 300, { trailing: true })
|
||||
|
||||
// 粘贴exlc数据时的更新方法
|
||||
const handleExlcInput = (exlc:string[][], rowIndex:number, colIndex:number) => {
|
||||
for (let i = 0; i < exlc.length; i++) {
|
||||
for (let j = 0; j < exlc[i].length; j++) {
|
||||
// 插入来自Excel的数据
|
||||
const insertExcelData = (data: string[][], rowIndex: number, colIndex: number) => {
|
||||
let maxRow = data.length
|
||||
let maxCol = data[0].length
|
||||
|
||||
if (rowIndex + maxRow > tableCells.value.length) maxRow = tableCells.value.length - rowIndex
|
||||
if (colIndex + maxCol > tableCells.value[0].length) maxCol = tableCells.value[0].length - colIndex
|
||||
|
||||
for (let i = 0; i < maxRow; i++) {
|
||||
for (let j = 0; j < maxCol; j++) {
|
||||
if (tableCells.value[rowIndex + i][colIndex + j]) {
|
||||
tableCells.value[rowIndex + i][colIndex + j].text = exlc[i][j]
|
||||
tableCells.value[rowIndex + i][colIndex + j].text = data[i][j]
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -642,7 +648,7 @@ export default defineComponent({
|
||||
handleMousedownColHandler,
|
||||
contextmenus,
|
||||
handleInput,
|
||||
handleExlcInput,
|
||||
insertExcelData,
|
||||
subThemeColor,
|
||||
formatText,
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user