diff --git a/packages/core/src/modules/cell.ts b/packages/core/src/modules/cell.ts index 6e0480d3..35f335bf 100644 --- a/packages/core/src/modules/cell.ts +++ b/packages/core/src/modules/cell.ts @@ -1586,7 +1586,7 @@ export function getFontStyleByCell( // style += "font-family: " + f + ";"; // } - if (key === "fs" && valueNum !== 10) { + if (key === "fs" && !_.isNil(value)) { style.fontSize = `${valueNum}pt`; } diff --git a/packages/core/src/modules/selection.ts b/packages/core/src/modules/selection.ts index e409d8cd..aff20a57 100644 --- a/packages/core/src/modules/selection.ts +++ b/packages/core/src/modules/selection.ts @@ -1616,7 +1616,8 @@ export function rangeValueToHtml( const c = colIndexArr[j]; // eslint-disable-next-line no-template-curly-in-string - let column = ''; + let column = + ''; const cell = d[r]?.[c]; if (cell != null) { @@ -1903,7 +1904,10 @@ export function rangeValueToHtml( } } - column = replaceHtml(column, { style, span }); + const cellData = encodeURIComponent( + JSON.stringify({ ...cell, _srcRow: r, _srcCol: c }) + ); + column = replaceHtml(column, { style, span, cellData }); if (_.isNil(c_value)) { c_value = getCellValue(r, c, d); @@ -1994,7 +1998,7 @@ export function rangeValueToHtml( } } - column = replaceHtml(column, { style, span: "" }); + column = replaceHtml(column, { style, span: "", cellData: "" }); column += ""; } diff --git a/packages/core/src/paste-table-helpers.ts b/packages/core/src/paste-table-helpers.ts index 65b48916..ad7c5ea5 100644 --- a/packages/core/src/paste-table-helpers.ts +++ b/packages/core/src/paste-table-helpers.ts @@ -6,6 +6,7 @@ import { getQKBorder, saveHyperlink } from "./modules"; import { Cell } from "./types"; import { getSheetIndex } from "./utils"; import { setRowHeight, setColumnWidth } from "./api"; +import { adjustFormulaForPaste } from "./events/paste"; export const DEFAULT_FONT_SIZE = 12; @@ -150,6 +151,8 @@ interface BuiltCellResult { rowspan: number; colspan: number; hyperlink?: { href: string; display: string } | null; + srcRow?: number; + srcCol?: number; } const HEX_REGEX = /^0x?[a-fA-F0-9]+$/; @@ -177,6 +180,30 @@ const buildCellFromTd = ( classStyles: Record, ctx: Context ): BuiltCellResult => { + const fortuneCellAttr = td.getAttribute("data-fortune-cell"); + if (fortuneCellAttr) { + try { + const { _srcRow, _srcCol, ...parsed } = JSON.parse( + decodeURIComponent(fortuneCellAttr) + ); + const cell = parsed as Cell; + delete cell.mc; + delete cell.hl; + const rowspan = parseInt(td.getAttribute("rowspan") || "1", 10); + const colspan = parseInt(td.getAttribute("colspan") || "1", 10); + return { + cell, + rowspan: Number.isNaN(rowspan) ? 1 : rowspan, + colspan: Number.isNaN(colspan) ? 1 : colspan, + hyperlink: detectHyperlink(td), + srcRow: _srcRow, + srcCol: _srcCol, + }; + } catch { + // fall through to CSS parsing if JSON is malformed + } + } + let cell: Cell = {}; const rawText = (td.innerText || td.innerHTML || "").trim(); const isLineBreak = rawText.includes("
"); @@ -391,16 +418,27 @@ export function handlePastedTable( } if (localColIndex === totalColumns) return; // row overflow - const { cell, rowspan, colspan, hyperlink } = buildCellFromTd( - tdElement, - classStyleMap, - ctx - ); + const { cell, rowspan, colspan, hyperlink, srcRow, srcCol } = + buildCellFromTd(tdElement, classStyleMap, ctx); const anchorCol = ctx.luckysheet_select_save![0].column[0]; const absoluteRow = anchorRow + localRowIndex; const absoluteCol = anchorCol + localColIndex; + if (cell.f && srcRow != null && srcCol != null) { + try { + cell.f = adjustFormulaForPaste( + cell.f, + srcCol, + srcRow, + absoluteCol, + absoluteRow + ); + } catch { + // invalid ref — leave formula as-is + } + } + // Place cell into matrix pastedMatrix[localRowIndex][localColIndex] = cell;