Skip to content

Commit

Permalink
fix(statusbar): fix status bar can not read date type cell
Browse files Browse the repository at this point in the history
  • Loading branch information
VicKun4937 committed Jan 25, 2025
1 parent 49cd575 commit adeb39e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,28 @@ export class ListSourceModel extends SourceModelBase {
}

if (node.isDate === true) {
const formatter = node.formatter || 'yyyy-m-d am/pm h:mm';
return {
v: transformDate(data),
s: {
n: {
pattern: 'yyyy-m-d am/pm h:mm',
pattern: formatter,
},
},
t: CellValueType.NUMBER,
};
} else {
if (node.formatter) {
return {
v: data,
s: {
n: {
pattern: node.formatter,
},
},
};
}

return {
t: typeof data === 'number' ? CellValueType.NUMBER : CellValueType.STRING,
v: data,
Expand Down Expand Up @@ -177,16 +189,27 @@ export class ObjectSourceModel extends SourceModelBase {
}
}
if (node.isDate === true) {
const formatter = node.formatter || 'yyyy-m-d am/pm h:mm';
return {
v: transformDate(data),
s: {
n: {
pattern: 'yyyy-m-d am/pm h:mm',
pattern: formatter,
},
},
t: CellValueType.NUMBER,
};
} else {
if (node.formatter) {
return {
v: data,
s: {
n: {
pattern: node.formatter,
},
},
};
}
return {
v: data,
t: typeof data === 'number' ? CellValueType.NUMBER : CellValueType.STRING,
Expand Down
5 changes: 5 additions & 0 deletions packages-experimental/sheets-source-binding/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export interface ICellBindingNodeParam {
*/
isDate?: boolean;
nodeId?: string; // optional in ICellBindingNodeParam

/**
* The formatter of the binding node, the formatter will be applied to the binding value.
*/
formatter?: string;
}

export interface ICellBindingNode extends ICellBindingNodeParam {
Expand Down
29 changes: 26 additions & 3 deletions packages/sheets-ui/src/controllers/status-bar.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { ICellData, ICommandInfo, IRange, ISelectionCell, Nullable, Workbook, Worksheet } from '@univerjs/core';
import type { ICellData, ICommandInfo, IRange, ISelectionCell, Nullable, Styles, Workbook, Worksheet } from '@univerjs/core';
import type { ArrayValueObject, ISheetData } from '@univerjs/engine-formula';
import type {
ISelectionWithStyle,
Expand All @@ -29,6 +29,7 @@ import {
Inject,
InterceptorManager,
IUniverInstanceService,
numfmt,
ObjectMatrix,
RANGE_TYPE,
splitIntoGrid,
Expand Down Expand Up @@ -56,7 +57,7 @@ class CalculateValueSet {
private _min: number = Number.POSITIVE_INFINITY;
private _max: number = Number.NEGATIVE_INFINITY;

add(value: Nullable<ICellData>) {
add(value: Nullable<ICellData>, styles: Styles, patternInfoRecord: Record<string, any>) {
const v = value?.v;
const t = value?.t;
if (v !== undefined && v !== null) {
Expand All @@ -65,7 +66,26 @@ class CalculateValueSet {
this._countNumber++;
this._min = Math.min(this._min, v);
this._max = Math.max(this._max, v);
} else if (t === CellValueType.NUMBER && value?.s) {
const style = styles.get(value.s);
if (style && t === CellValueType.NUMBER && style.n) {
const { pattern } = style.n;
if (!patternInfoRecord[pattern]) {
patternInfoRecord[pattern] = numfmt.getInfo(pattern);
}
const formatInfo = patternInfoRecord[pattern];
const isDate = formatInfo.isDate;

if (isDate) {
const dateValue = numfmt.parseDate(v as string).v as number;
this._sum += Number(dateValue);
this._countNumber++;
this._min = Math.min(this._min, Number(dateValue));
this._max = Math.max(this._max, Number(dateValue));
}
}
}

this._count++;
}
}
Expand Down Expand Up @@ -263,12 +283,15 @@ export class StatusBarController extends Disposable {
const noDuplicate = splitIntoGrid(realSelections);
// const matrix = sheet.getCellMatrix();
const calculateValueSet = new CalculateValueSet();
const styles = workbook.getStyles();
const patternInfoRecord: Record<string, any> = {};

for (const range of noDuplicate) {
const { startRow, startColumn, endColumn, endRow } = this.getRangeStartEndInfo(range, sheet);
for (let r = startRow; r <= endRow; r++) {
for (let c = startColumn; c <= endColumn; c++) {
const value = sheet.getCell(r, c);
calculateValueSet.add(value);
calculateValueSet.add(value, styles, patternInfoRecord);
}
}
}
Expand Down

0 comments on commit adeb39e

Please sign in to comment.