Skip to content

Commit

Permalink
fix(numfmt): add or substract decimal inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
Gggpound committed Aug 27, 2024
1 parent d8ca9f6 commit 741d24c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { IAccessor, ICommand } from '@univerjs/core';
import { CommandType, ICommandService, IUniverInstanceService, Range } from '@univerjs/core';
import { CellValueType, CommandType, ICommandService, IUniverInstanceService, Range } from '@univerjs/core';
import { getSheetCommandTarget, INumfmtService, SheetsSelectionsService } from '@univerjs/sheets';

import { getDecimalFromPattern, setPatternDecimal } from '../../utils/decimal';
Expand All @@ -30,7 +30,6 @@ export const AddDecimalCommand: ICommand = {
const selectionManagerService = accessor.get(SheetsSelectionsService);
const numfmtService = accessor.get(INumfmtService);
const univerInstanceService = accessor.get(IUniverInstanceService);

const selections = selectionManagerService.getCurrentSelections();
if (!selections || !selections.length) {
return false;
Expand All @@ -46,6 +45,17 @@ export const AddDecimalCommand: ICommand = {
Range.foreach(selection.range, (row, col) => {
const numfmtValue = numfmtService.getValue(unitId, subUnitId, row, col);
if (!numfmtValue) {
const cell = target.worksheet.getCellRaw(row, col);
if (!maxDecimals && cell && cell.t === CellValueType.NUMBER && cell.v) {
const regResult = /\.(\d*)$/.exec(String(cell.v));
if (regResult) {
const length = regResult[1].length;
if (!length) {
return;
}
maxDecimals = Math.max(maxDecimals, length);
}
}
return;
}
const decimals = getDecimalFromPattern(numfmtValue.pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { IAccessor, ICommand } from '@univerjs/core';
import { CommandType, ICommandService, IUniverInstanceService, Range } from '@univerjs/core';
import { CellValueType, CommandType, ICommandService, IUniverInstanceService, Range } from '@univerjs/core';
import { getSheetCommandTarget, INumfmtService, SheetsSelectionsService } from '@univerjs/sheets';

import { getDecimalFromPattern, setPatternDecimal } from '../../utils/decimal';
Expand Down Expand Up @@ -46,6 +46,17 @@ export const SubtractDecimalCommand: ICommand = {
Range.foreach(selection.range, (row, col) => {
const numfmtValue = numfmtService.getValue(unitId, subUnitId, row, col);
if (!numfmtValue) {
const cell = target.worksheet.getCellRaw(row, col);
if (!maxDecimals && cell && cell.t === CellValueType.NUMBER && cell.v) {
const regResult = /\.(\d*)$/.exec(String(cell.v));
if (regResult) {
const length = regResult[1].length;
if (!length) {
return;
}
maxDecimals = Math.max(maxDecimals, length);
}
}
return;
}
const decimals = getDecimalFromPattern(numfmtValue.pattern);
Expand Down

0 comments on commit 741d24c

Please sign in to comment.