Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(sheet): support source bind ignore header (#4524)

VicKunf38f29e3460f
diff --git a/packages-experimental/sheets-source-binding/src/controllers/binding-manager.ts b/packages-experimental/sheets-source-binding/src/controllers/binding-manager.ts
index b3508df2e0..ec536cbe9d 100644
--- a/packages-experimental/sheets-source-binding/src/controllers/binding-manager.ts
+++ b/packages-experimental/sheets-source-binding/src/controllers/binding-manager.ts
@@ -15,13 +15,13 @@
*/
import type { IMutationInfo } from '@univerjs/core';
-import type { ICellBindingJSON, ICellBindingNode, ICellBindingNodeParam } from '../types';
+import type { ICellBindingJSON, ICellBindingNode, ICellBindingNodeParam, IListDataBindingNode } from '../types';
import { Disposable, generateRandomId, Inject, IUniverInstanceService, Range } from '@univerjs/core';
import { ClearSelectionAllCommand, ClearSelectionContentCommand, getSheetCommandTarget, SheetInterceptorService, SheetsSelectionsService } from '@univerjs/sheets';
import { Subject } from 'rxjs';
import { SheetBindingModel } from '../model/binding-model';
-import { BindingSourceChangeTypeEnum } from '../types';
+import { BindingSourceChangeTypeEnum, DataBindingNodeTypeEnum } from '../types';
interface IBindingNodeInfo {
unitId: string;
@@ -35,7 +35,13 @@ interface IBindingNodeInfo {
export class SheetsBindingManager extends Disposable {
modelMap: Map<string, Map<string, SheetBindingModel>> = new Map();
- private _cellBindInfoUpdate$ = new Subject<IBindingNodeInfo & { changeType: BindingSourceChangeTypeEnum; oldSourceId?: string }>();
+ private _cellBindInfoUpdate$ = new Subject<IBindingNodeInfo & {
+ changeType: BindingSourceChangeTypeEnum;
+ oldSourceId?: string;
+ containHeader?: boolean;
+ oldNodeContainHeader?: boolean;
+ }>();
+
cellBindInfoUpdate$ = this._cellBindInfoUpdate$.asObservable();
constructor(
@@ -125,6 +131,7 @@ export class SheetsBindingManager extends Disposable {
throw new Error('row and column is required');
}
const oldNode = model.getBindingNode(row, column);
+ const containHeader = node.type === DataBindingNodeTypeEnum.List ? Boolean((node as IListDataBindingNode).containHeader) : false;
model.setBindingNode(row, column, { ...node, row, column } as ICellBindingNode);
this._cellBindInfoUpdate$.next({
unitId,
@@ -133,8 +140,10 @@ export class SheetsBindingManager extends Disposable {
nodeId: node.nodeId,
row,
column,
+ containHeader,
changeType: oldNode ? BindingSourceChangeTypeEnum.Update : BindingSourceChangeTypeEnum.Add,
oldSourceId: oldNode?.sourceId,
+ oldNodeContainHeader: oldNode?.type === DataBindingNodeTypeEnum.List ? Boolean((oldNode as IListDataBindingNode).containHeader) : false,
});
}
@@ -165,6 +174,14 @@ export class SheetsBindingManager extends Disposable {
return undefined;
}
+ getBindingNodeById(unitId: string, subunitId: string, nodeId: string): ICellBindingNode | undefined {
+ const model = this.getModel(unitId, subunitId);
+ if (model) {
+ return model.getBindingNodeById(nodeId);
+ }
+ return undefined;
+ }
+
createModel(unitId: string, subunitId: string, json?: ICellBindingNode[]): SheetBindingModel {
const model = new SheetBindingModel(json);
this.addModel(unitId, subunitId, model);
diff --git a/packages-experimental/sheets-source-binding/src/model/source-model.ts b/packages-experimental/sheets-source-binding/src/model/source-model.ts
index f3883c4c04..9a26ecbe26 100644
--- a/packages-experimental/sheets-source-binding/src/model/source-model.ts
+++ b/packages-experimental/sheets-source-binding/src/model/source-model.ts
@@ -107,19 +107,20 @@ export class ListSourceModel extends SourceModelBase {
}
getData(node: IListDataBindingNode, row: number): ICellData | null {
- const { path, row: baseRow } = node;
+ const { path, row: baseRow, containHeader } = node;
const colIndex = this._fieldIndexMap.get(path)!;
const rowIndex = row - baseRow;
- if (rowIndex === 0) {
+ if (containHeader && rowIndex === 0) {
return {
v: this._data.fields[colIndex],
};
}
let data;
+ const offset = containHeader ? 1 : 0;
if (this._isListObject) {
- data = (this._data.records as Record<string | number, any>)[rowIndex - 1][path];
+ data = (this._data.records as Record<string | number, any>)[rowIndex - offset][path];
} else {
- data = this._data.records[rowIndex - 1][colIndex];
+ data = this._data.records[rowIndex - offset][colIndex];
}
if (node.isDate === true) {
diff --git a/packages-experimental/sheets-source-binding/src/services/source-binding-service.ts b/packages-experimental/sheets-source-binding/src/services/source-binding-service.ts
index 5d1c546108..4d941f446f 100644
--- a/packages-experimental/sheets-source-binding/src/services/source-binding-service.ts
+++ b/packages-experimental/sheets-source-binding/src/services/source-binding-service.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ICellBindingNodeParam, IListSourceInfo, ISourceBindingInfo } from '../types';
+import type { ICellBindingNodeParam, IListDataBindingNode, IListSourceInfo, ISourceBindingInfo } from '../types';
import { Disposable, Inject, InterceptorEffectEnum, RTree } from '@univerjs/core';
import { INTERCEPTOR_POINT, SheetInterceptorService } from '@univerjs/sheets';
@@ -107,6 +107,7 @@ export class SheetsSourceBindService extends Disposable {
// eslint-disable-next-line max-lines-per-function
private _registerSourceChange() {
// Update the RTree collection when the source data updated
+ // eslint-disable-next-line complexity
this.disposeWithMe(this._sheetsSourceManager.sourceDataUpdate$.subscribe((sourceInfo) => {
const { sourceId, sourceType, unitId: sourceUnitId, changeType } = sourceInfo;
@@ -117,8 +118,10 @@ export class SheetsSourceBindService extends Disposable {
const recordCount = sourceInfo.recordCount;
for (const { unitId, subunitId, nodeId, row, column } of nodeInfo) {
const rTreeCollection = this._getRTeeCollection(sourceUnitId);
- if (rTreeCollection) {
- const range = { startRow: row, startColumn: column, endRow: row + recordCount, endColumn: column };
+ const node = this._sheetsBindingManager.getBindingNodeById(unitId, subunitId, nodeId);
+ if (rTreeCollection && node?.type === DataBindingNodeTypeEnum.List) {
+ const offset = (node as IListDataBindingNode).containHeader ? 0 : 1;
+ const range = { startRow: row, startColumn: column, endRow: row + recordCount - offset, endColumn: column };
rTreeCollection.remove({ unitId, sheetId: subunitId, id: nodeId, range });
}
}
@@ -129,9 +132,11 @@ export class SheetsSourceBindService extends Disposable {
const nodeInfo = this._sheetsBindingManager.getBindingModelBySourceId(sourceId);
for (const { unitId, subunitId, nodeId, row, column } of nodeInfo) {
const rTreeCollection = this._getRTeeCollection(sourceUnitId);
- if (rTreeCollection) {
- const oldRange = { startRow: row, startColumn: column, endRow: row + oldRecordCount, endColumn: column };
- const range = { startRow: row, startColumn: column, endRow: row + sourceInfo.recordCount, endColumn: column };
+ const node = this._sheetsBindingManager.getBindingNodeById(unitId, subunitId, nodeId);
+ if (rTreeCollection && node?.type === DataBindingNodeTypeEnum.List) {
+ const offset = (node as IListDataBindingNode).containHeader ? 0 : 1;
+ const oldRange = { startRow: row, startColumn: column, endRow: row + oldRecordCount - offset, endColumn: column };
+ const range = { startRow: row, startColumn: column, endRow: row + sourceInfo.recordCount - offset, endColumn: column };
rTreeCollection.remove({ unitId, sheetId: subunitId, id: nodeId, range: oldRange });
rTreeCollection.insert({ unitId, sheetId: subunitId, id: nodeId, range });
}
@@ -148,8 +153,12 @@ export class SheetsSourceBindService extends Disposable {
for (const { unitId, subunitId, nodeId, row, column } of nodeInfo) {
const rTreeCollection = this._ensureRTreeCollection(unitId);
- const range = { startRow: row, startColumn: column, endRow: row + recordCount, endColumn: column };
- rTreeCollection.insert({ unitId, sheetId: subunitId, id: nodeId, range });
+ const node = this._sheetsBindingManager.getBindingNodeById(unitId, subunitId, nodeId);
+ if (rTreeCollection && node?.type === DataBindingNodeTypeEnum.List) {
+ const offset = (node as IListDataBindingNode).containHeader ? 0 : 1;
+ const range = { startRow: row, startColumn: column, endRow: row + recordCount - offset, endColumn: column };
+ rTreeCollection.insert({ unitId, sheetId: subunitId, id: nodeId, range });
+ }
}
}
}
@@ -157,7 +166,7 @@ export class SheetsSourceBindService extends Disposable {
// Update the RTree collection when the binding node updated
this.disposeWithMe(this._sheetsBindingManager.cellBindInfoUpdate$.subscribe((nodeInfo) => {
- const { unitId, subunitId, sourceId, nodeId, row, column, changeType } = nodeInfo;
+ const { unitId, subunitId, sourceId, nodeId, row, column, changeType, containHeader } = nodeInfo;
const rTreeCollection = this._ensureRTreeCollection(unitId);
const source = this._sheetsSourceManager.getSource(unitId, sourceId);
@@ -165,19 +174,21 @@ export class SheetsSourceBindService extends Disposable {
const sourceInfo = source.getSourceInfo() as IListSourceInfo;
if (sourceInfo.sourceType === DataBindingNodeTypeEnum.List) {
const recordCount = sourceInfo.recordCount;
- const range = { startRow: row, startColumn: column, endRow: row + recordCount, endColumn: column };
+ const offset = containHeader ? 0 : 1;
+ const range = { startRow: row, startColumn: column, endRow: row + recordCount - offset, endColumn: column };
if (changeType === BindingSourceChangeTypeEnum.Add) {
rTreeCollection.insert({ unitId, sheetId: subunitId, id: nodeId, range });
} else if (changeType === BindingSourceChangeTypeEnum.Remove) {
rTreeCollection.remove({ unitId, sheetId: subunitId, id: nodeId, range });
} else if (changeType === BindingSourceChangeTypeEnum.Update) {
const oldSourceId = nodeInfo.oldSourceId!;
+ const offset = nodeInfo.oldNodeContainHeader ? 0 : 1;
// remove the old range
const oldSource = this._sheetsSourceManager.getSource(unitId, oldSourceId);
if (oldSource && oldSource.hasData()) {
const oldSourceInfo = oldSource.getSourceInfo() as IListSourceInfo;
const oldRecordCount = oldSourceInfo.recordCount;
- const oldRange = { startRow: row, startColumn: column, endRow: row + oldRecordCount, endColumn: column };
+ const oldRange = { startRow: row, startColumn: column, endRow: row + oldRecordCount - offset, endColumn: column };
rTreeCollection.remove({ unitId, sheetId: subunitId, id: nodeId, range: oldRange });
}
rTreeCollection.insert({ unitId, sheetId: subunitId, id: nodeId, range });
@@ -241,7 +252,7 @@ export class SheetsSourceBindService extends Disposable {
effect: InterceptorEffectEnum.Value | InterceptorEffectEnum.Style,
priority: 102,
handler: (cell, context, next) => {
- const { row, col, unitId, subUnitId } = context;
+ const { row, col, unitId, subUnitId, workbook } = context;
let value = null;
if (this._bindingModel === BindModeEnum.Path) {
value = this._getPathModeCellValue(unitId, subUnitId, row, col);
@@ -249,7 +260,9 @@ export class SheetsSourceBindService extends Disposable {
value = this._getValueModeCellValue(unitId, subUnitId, row, col);
}
if (value !== null) {
- return next({ ...cell, ...value });
+ const defaultStyle = (typeof cell?.s === 'string' ? workbook.getStyles().get(cell?.s) : cell?.s) || {};
+ const newStyle = { ...defaultStyle };
+ return next({ ...cell, ...value, s: newStyle });
}
return next(cell);
diff --git a/packages-experimental/sheets-source-binding/src/types.ts b/packages-experimental/sheets-source-binding/src/types.ts
index c5ccb6415d..a1510f7d64 100644
--- a/packages-experimental/sheets-source-binding/src/types.ts
+++ b/packages-experimental/sheets-source-binding/src/types.ts
@@ -119,6 +119,10 @@ export interface IListDataBindingNode extends ICellBindingNode {
direction?: ListDataBindingNodeDirectionEnum;
range?: { startRow: number; startColumn: number; endRow: number; endColumn: number };
displayName?: string;
+ /**
+ * Whether contain header in the list data binding node.
+ */
+ containHeader?: boolean;
}
export interface IDataBindingService {