From 5820ad38870657940df4c64a1fc8430a8f3ba34a Mon Sep 17 00:00:00 2001
From: fangsmile <892739385@qq.com>
Date: Mon, 10 Mar 2025 16:41:29 +0800
Subject: [PATCH 1/4] fix: selection mergeCell extend range #3529

---
 .../scenegraph/select/update-select-border.ts | 20 +++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/packages/vtable/src/scenegraph/select/update-select-border.ts b/packages/vtable/src/scenegraph/select/update-select-border.ts
index 5fdd89cd63..913aeaa3cc 100644
--- a/packages/vtable/src/scenegraph/select/update-select-border.ts
+++ b/packages/vtable/src/scenegraph/select/update-select-border.ts
@@ -1,4 +1,4 @@
-import { createRect, type IRect } from '@src/vrender';
+import { type IRect } from '@src/vrender';
 import type { Scenegraph } from '../scenegraph';
 import type { CellRange, CellSubLocation } from '../../ts-types';
 import { getCellMergeInfo } from '../utils/get-cell-merge';
@@ -434,7 +434,23 @@ export function updateCellSelectBorder(
       extendSelectRange();
     }
   };
-  ifExtendSelectRange && extendSelectRange();
+  if (ifExtendSelectRange) {
+    extendSelectRange();
+    if (selectRange.start.col > selectRange.end.col) {
+      selectRange.start.col = Math.max(startCol, endCol);
+      selectRange.end.col = Math.min(startCol, endCol);
+    } else {
+      selectRange.start.col = Math.min(startCol, endCol);
+      selectRange.end.col = Math.max(startCol, endCol);
+    }
+    if (selectRange.start.row > selectRange.end.row) {
+      selectRange.start.row = Math.max(startRow, endRow);
+      selectRange.end.row = Math.min(startRow, endRow);
+    } else {
+      selectRange.start.row = Math.min(startRow, endRow);
+      selectRange.end.row = Math.max(startRow, endRow);
+    }
+  }
   //#endregion
   scene.selectingRangeComponents.forEach(
     (selectComp: { rect: IRect; fillhandle?: IRect; role: CellSubLocation }, key: string) => {

From d9d6aad0ef0f0b16167367f5b827b4bf6e3a8627 Mon Sep 17 00:00:00 2001
From: fangsmile <892739385@qq.com>
Date: Mon, 10 Mar 2025 16:42:01 +0800
Subject: [PATCH 2/4] docs: update changlog of rush

---
 ...ectionfillmode-replace-error_2025-03-10-08-42.json | 11 +++++++++++
 1 file changed, 11 insertions(+)
 create mode 100644 common/changes/@visactor/vtable/3529-bug-selectionfillmode-replace-error_2025-03-10-08-42.json

diff --git a/common/changes/@visactor/vtable/3529-bug-selectionfillmode-replace-error_2025-03-10-08-42.json b/common/changes/@visactor/vtable/3529-bug-selectionfillmode-replace-error_2025-03-10-08-42.json
new file mode 100644
index 0000000000..aacea1253b
--- /dev/null
+++ b/common/changes/@visactor/vtable/3529-bug-selectionfillmode-replace-error_2025-03-10-08-42.json
@@ -0,0 +1,11 @@
+{
+  "changes": [
+    {
+      "comment": "fix: selection mergeCell extend range #3529\n\n",
+      "type": "none",
+      "packageName": "@visactor/vtable"
+    }
+  ],
+  "packageName": "@visactor/vtable",
+  "email": "892739385@qq.com"
+}
\ No newline at end of file

From bdf4fd1013ee722950f1f8ca1878f94813f90639 Mon Sep 17 00:00:00 2001
From: fangsmile <892739385@qq.com>
Date: Mon, 10 Mar 2025 17:57:12 +0800
Subject: [PATCH 3/4] fix: selection mergeCell extend range #3529

---
 .../vtable/src/state/select/update-position.ts     | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/packages/vtable/src/state/select/update-position.ts b/packages/vtable/src/state/select/update-position.ts
index f21025097e..376d480ea0 100644
--- a/packages/vtable/src/state/select/update-position.ts
+++ b/packages/vtable/src/state/select/update-position.ts
@@ -278,6 +278,13 @@ export function updateSelectPosition(
       cellPos.row = row;
       // scenegraph.setCellNormalStyle(col, row);
       const currentRange = state.select.ranges?.[state.select.ranges.length - 1];
+      if (
+        (currentRange.start.row >= table.rowCount - 1 && currentRange.end.row >= table.rowCount - 1) ||
+        (currentRange.start.col >= table.colCount - 1 && currentRange.end.col >= table.colCount - 1)
+      ) {
+        // 如果选中范围超出表格范围，则不更新select border
+        return;
+      }
       currentRange && scenegraph.updateCellSelectBorder(currentRange, extendSelectRange);
     }
   } else if (
@@ -409,6 +416,13 @@ export function updateSelectPosition(
           }
         }
       }
+      if (
+        (currentRange.start.row >= table.rowCount - 1 && currentRange.end.row >= table.rowCount - 1) ||
+        (currentRange.start.col >= table.colCount - 1 && currentRange.end.col >= table.colCount - 1)
+      ) {
+        // 如果选中范围超出表格范围，则不更新select border
+        return;
+      }
       scenegraph.updateCellSelectBorder(currentRange, extendSelectRange);
     }
   }

From 5bc5950df6712bf8014311091dcb8d5a443e3eaf Mon Sep 17 00:00:00 2001
From: fangsmile <892739385@qq.com>
Date: Mon, 10 Mar 2025 19:17:30 +0800
Subject: [PATCH 4/4] fix: selection mergeCell extend range #3529

---
 .../src/state/select/update-position.ts       | 22 ++++++++++---------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/packages/vtable/src/state/select/update-position.ts b/packages/vtable/src/state/select/update-position.ts
index 376d480ea0..27fcf04656 100644
--- a/packages/vtable/src/state/select/update-position.ts
+++ b/packages/vtable/src/state/select/update-position.ts
@@ -279,13 +279,14 @@ export function updateSelectPosition(
       // scenegraph.setCellNormalStyle(col, row);
       const currentRange = state.select.ranges?.[state.select.ranges.length - 1];
       if (
-        (currentRange.start.row >= table.rowCount - 1 && currentRange.end.row >= table.rowCount - 1) ||
-        (currentRange.start.col >= table.colCount - 1 && currentRange.end.col >= table.colCount - 1)
+        currentRange &&
+        currentRange.start.row <= table.rowCount - 1 &&
+        currentRange.end.row <= table.rowCount - 1 &&
+        currentRange.start.col <= table.colCount - 1 &&
+        currentRange.end.col <= table.colCount - 1
       ) {
-        // 如果选中范围超出表格范围，则不更新select border
-        return;
+        scenegraph.updateCellSelectBorder(currentRange, extendSelectRange);
       }
-      currentRange && scenegraph.updateCellSelectBorder(currentRange, extendSelectRange);
     }
   } else if (
     (interactionState === InteractionState.grabing || table.eventManager.isDraging) &&
@@ -417,13 +418,14 @@ export function updateSelectPosition(
         }
       }
       if (
-        (currentRange.start.row >= table.rowCount - 1 && currentRange.end.row >= table.rowCount - 1) ||
-        (currentRange.start.col >= table.colCount - 1 && currentRange.end.col >= table.colCount - 1)
+        currentRange &&
+        currentRange.start.row <= table.rowCount - 1 &&
+        currentRange.end.row <= table.rowCount - 1 &&
+        currentRange.start.col <= table.colCount - 1 &&
+        currentRange.end.col <= table.colCount - 1
       ) {
-        // 如果选中范围超出表格范围，则不更新select border
-        return;
+        scenegraph.updateCellSelectBorder(currentRange, extendSelectRange);
       }
-      scenegraph.updateCellSelectBorder(currentRange, extendSelectRange);
     }
   }
   scenegraph.updateNextFrame();
