Skip to content

Commit

Permalink
fix(formula): formula cell shows format, cell calculation sets percen…
Browse files Browse the repository at this point in the history
…tage
  • Loading branch information
Dushusir committed Feb 6, 2024
1 parent 8576bf6 commit f68e4b3
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/engine-formula/src/basics/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

export const DEFFAULT_DATE_FORMAT = 'yyyy/mm/dd';
export const DEFFAULT_DATE_FORMAT = 'yyyy-mm-dd;@';

/**
* Excel stores dates as sequential serial numbers so they can be used in calculations. By default, January 1, 1900 is serial number 1, and January 1, 2008 is serial number 39448 because it is 39,447 days after January 1, 1900.
Expand Down
3 changes: 3 additions & 0 deletions packages/engine-formula/src/engine/ast-node/suffix-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export class SuffixNode extends BaseAstNode {
value as BaseValueObject,
new NumberValueObject(100)
) as FunctionVariantType;

// set number format
result.setPattern('0.00%');
} else if (this._operatorString === suffixToken.POUND) {
result = this._handlerPound(value);
} else {
Expand Down
4 changes: 4 additions & 0 deletions packages/engine-formula/src/models/formula-data.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ export class FormulaDataModel extends Disposable {
return this._numfmtItemMap;
}

getNumfmtValue(unitId: string, sheetId: string, row: number, column: number) {
return this._numfmtItemMap[unitId]?.[sheetId]?.[row]?.[column];
}

setNumfmtItemMap(value: INumfmtItemMap) {
this._numfmtItemMap = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class FormulaClipboardController extends Disposable {
private _pasteFormulaHook(): ISheetClipboardHook {
const specialPasteFormulaHook: ISheetClipboardHook = {
id: SPECIAL_PASTE_FORMULA,
priority: 10,
specialPasteInfo: {
label: 'specialPaste.formula',
},
Expand Down Expand Up @@ -96,6 +97,7 @@ export class FormulaClipboardController extends Disposable {
private _pasteWithFormulaHook(): ISheetClipboardHook {
const specialPasteFormulaHook: ISheetClipboardHook = {
id: DEFAULT_PASTE_FORMULA,
priority: 10,
onPasteCells: (pasteFrom, pasteTo, data, payload) => {
const copyInfo = {
copyType: payload.copyType || COPY_TYPE.COPY,
Expand Down
2 changes: 2 additions & 0 deletions packages/sheets-numfmt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"peerDependencies": {
"@univerjs/core": "workspace:*",
"@univerjs/design": "workspace:*",
"@univerjs/engine-formula": "workspace:*",
"@univerjs/engine-numfmt": "workspace:*",
"@univerjs/engine-render": "workspace:*",
"@univerjs/sheets": "workspace:*",
Expand All @@ -70,6 +71,7 @@
"@types/react": "^18.2.53",
"@univerjs/core": "workspace:*",
"@univerjs/design": "workspace:*",
"@univerjs/engine-formula": "workspace:*",
"@univerjs/engine-numfmt": "workspace:*",
"@univerjs/engine-render": "workspace:*",
"@univerjs/shared": "workspace:*",
Expand Down
9 changes: 7 additions & 2 deletions packages/sheets-numfmt/src/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { getMenuHiddenObservable, MenuGroup, MenuItemType, MenuPosition } from '
import type { IAccessor } from '@wendellhu/redi';
import { merge, Observable } from 'rxjs';

import { FormulaDataModel } from '@univerjs/engine-formula';
import { MENU_OPTIONS } from '../base/const/MENU-OPTIONS';
import { AddDecimalCommand } from '../commands/commands/add-decimal.command';
import { SetCurrencyCommand } from '../commands/commands/set-currency.command';
Expand Down Expand Up @@ -96,6 +97,7 @@ export const FactoryOtherMenuItem = (componentManager: ComponentManager) => {

return (_accessor: IAccessor) => {
const numfmtService = _accessor.get(INumfmtService);
const formulaDataModel = _accessor.get(FormulaDataModel);
const univerInstanceService = _accessor.get(IUniverInstanceService);
const commandService = _accessor.get(ICommandService);
const localeService = _accessor.get(LocaleService);
Expand All @@ -121,11 +123,14 @@ export const FactoryOtherMenuItem = (componentManager: ComponentManager) => {
const range = selections[0].range;
const row = range.startRow;
const col = range.startColumn;

const numfmtValue = numfmtService.getValue(workbook.getUnitId(), worksheet.getSheetId(), row, col);
const numfmtValueByFormula = formulaDataModel.getNumfmtValue(workbook.getUnitId(), worksheet.getSheetId(), row, col);

const pattern = numfmtValue?.pattern || numfmtValueByFormula;
let value: string = localeService.t('sheet.numfmt.general');

if (numfmtValue) {
const pattern = numfmtValue.pattern;
if (pattern) {
const item = MENU_OPTIONS.filter((item) => typeof item === 'object' && item.pattern).find(
(item) => isPatternEqualWithoutDecimal(pattern, (item as { pattern: string }).pattern)
);
Expand Down
2 changes: 2 additions & 0 deletions packages/sheets-numfmt/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default ({ mode }) => createViteConfig({
'@univerjs/core',
'@univerjs/design',
'@univerjs/engine-formula',
'@univerjs/engine-numfmt',
'@univerjs/engine-render',
'@univerjs/sheets',
'@univerjs/sheets-ui',
Expand All @@ -24,6 +25,7 @@ export default ({ mode }) => createViteConfig({
'@univerjs/core': 'UniverCore',
'@univerjs/design': 'UniverDesign',
'@univerjs/engine-formula': 'UniverEngineFormula',
'@univerjs/engine-numfmt': 'UniverEngineNumfmt',
'@univerjs/engine-render': 'UniverEngineRender',
'@univerjs/sheets': 'UniverSheets',
'@univerjs/sheets-ui': 'UniverSheetsUi',
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f68e4b3

Please sign in to comment.