Clicking a single cell inside a continuous data block and choosing Insert -> Table (Format as Table) now expands the selection to the surrounding CurrentRegion, matching Excel. The helper scans outward from the anchor while edge cells have values, bounded by the used range, and requires at least header plus one data row. Co-authored-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
This commit is contained in:
co-authored by
Aniruddha Adak
parent
616a7acf5a
commit
ed2bb86536
@@ -26,6 +26,7 @@ import { isSheetRemoved, journalSize, recordStructuralOp } from './edit-journal'
|
||||
import { resolveGoToRef, type GoToNameEntry } from './goto'
|
||||
import { getLang, t } from './i18n/locale'
|
||||
import { appendSymbol } from './SymbolDialog'
|
||||
import { inferContinuousRegion } from './table-actions'
|
||||
import {
|
||||
a1RangeRef,
|
||||
a1RowRangeRef,
|
||||
@@ -641,10 +642,20 @@ export function handleFormatAsTable(ctx: DataToolsContext, style: string): void
|
||||
}
|
||||
const sheetId = worksheet.getSheetId()
|
||||
if (isSheetRemoved(state.editJournal, sheetId)) return
|
||||
const startRow = range.getRow()
|
||||
const startColumn = range.getColumn()
|
||||
const endRow = startRow + range.getHeight() - 1
|
||||
const endColumn = startColumn + range.getWidth() - 1
|
||||
let startRow = range.getRow()
|
||||
let startColumn = range.getColumn()
|
||||
let endRow = startRow + range.getHeight() - 1
|
||||
let endColumn = startColumn + range.getWidth() - 1
|
||||
// fixes #298: single-cell selection → infer continuous region (Excel CurrentRegion)
|
||||
if (range.getHeight() === 1 && range.getWidth() === 1) {
|
||||
const inferred = inferContinuousRegion(worksheet, startRow, startColumn)
|
||||
if (inferred) {
|
||||
startRow = inferred.startRow
|
||||
startColumn = inferred.startColumn
|
||||
endRow = inferred.endRow
|
||||
endColumn = inferred.endColumn
|
||||
}
|
||||
}
|
||||
try {
|
||||
applyAiTableAdd(runtime, state, {
|
||||
op: 'add_table',
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
import type { UniverWorksheet } from './univer-state'
|
||||
|
||||
export interface TableRegion {
|
||||
readonly startRow: number
|
||||
readonly startColumn: number
|
||||
readonly endRow: number
|
||||
readonly endColumn: number
|
||||
}
|
||||
|
||||
function cellHasValue(worksheet: UniverWorksheet, row: number, column: number): boolean {
|
||||
try {
|
||||
const cell = worksheet.getRange(row, column, 1, 1)
|
||||
// getValue returns the raw value (null for empty); getDisplayValue covers formatted empties
|
||||
const raw = (cell as unknown as { getValue?: () => unknown }).getValue?.()
|
||||
if (raw !== null && raw !== undefined && raw !== '') {
|
||||
if (typeof raw === 'object') {
|
||||
const v = (raw as { v?: unknown }).v
|
||||
if (v !== undefined) return v !== null && v !== ''
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
const display = (
|
||||
cell as unknown as { getDisplayValue?: () => string | null }
|
||||
).getDisplayValue?.()
|
||||
return display !== null && display !== undefined && display !== ''
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Infer the continuous data region (Excel CurrentRegion) around the anchor
|
||||
* cell. Expands outward while any cell on the adjacent edge has a value,
|
||||
* bounded by the sheet's used extent. Returns null when the anchor itself
|
||||
* is empty or the region would be a single cell.
|
||||
*/
|
||||
export function inferContinuousRegion(
|
||||
worksheet: UniverWorksheet,
|
||||
anchorRow: number,
|
||||
anchorColumn: number,
|
||||
): TableRegion | null {
|
||||
if (!cellHasValue(worksheet, anchorRow, anchorColumn)) return null
|
||||
let startRow = anchorRow
|
||||
let endRow = anchorRow
|
||||
let startColumn = anchorColumn
|
||||
let endColumn = anchorColumn
|
||||
const lastRow = Math.max(anchorRow, worksheet.getLastRow())
|
||||
const lastColumn = Math.max(anchorColumn, worksheet.getLastColumn())
|
||||
let grew = true
|
||||
let guard = 0
|
||||
while (grew && guard < 10_000) {
|
||||
guard += 1
|
||||
grew = false
|
||||
if (startRow > 0) {
|
||||
for (let c = startColumn; c <= endColumn; c += 1) {
|
||||
if (cellHasValue(worksheet, startRow - 1, c)) {
|
||||
startRow -= 1
|
||||
grew = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if (endRow < lastRow) {
|
||||
for (let c = startColumn; c <= endColumn; c += 1) {
|
||||
if (cellHasValue(worksheet, endRow + 1, c)) {
|
||||
endRow += 1
|
||||
grew = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if (startColumn > 0) {
|
||||
for (let r = startRow; r <= endRow; r += 1) {
|
||||
if (cellHasValue(worksheet, r, startColumn - 1)) {
|
||||
startColumn -= 1
|
||||
grew = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if (endColumn < lastColumn) {
|
||||
for (let r = startRow; r <= endRow; r += 1) {
|
||||
if (cellHasValue(worksheet, r, endColumn + 1)) {
|
||||
endColumn += 1
|
||||
grew = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Need at least header plus one data row
|
||||
if (endRow <= startRow) return null
|
||||
return { startRow, startColumn, endRow, endColumn }
|
||||
}
|
||||
Reference in New Issue
Block a user