Skip to content

Commit

Permalink
fix(sheet): force string sets style (#2448)
Browse files Browse the repository at this point in the history
* fix(sheet): force string sets style

* fix(link): do not change cell type when link added

* fix(sheet): undo priority sheet interceptor
  • Loading branch information
Dushusir committed Jun 17, 2024
1 parent 389e65b commit 17130d1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import type { IMutationInfo } from '@univerjs/core';
import { CellValueType, Disposable, IUniverInstanceService, LifecycleStages, ObjectMatrix, OnLifecycle, Range, Tools } from '@univerjs/core';
import type { IMutationInfo, Workbook } from '@univerjs/core';
import { CellValueType, Disposable, IUniverInstanceService, LifecycleStages, ObjectMatrix, OnLifecycle, Range, Tools, UniverInstanceType } from '@univerjs/core';
import { Inject, Injector } from '@wendellhu/redi';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import { ClearSelectionAllCommand, ClearSelectionContentCommand, ClearSelectionFormatCommand, getSheetCommandTarget, SelectionManagerService, SetRangeValuesCommand, SetRangeValuesMutation, SetRangeValuesUndoMutationFactory, SheetInterceptorService } from '@univerjs/sheets';
Expand Down Expand Up @@ -51,15 +51,17 @@ export class SheetHyperLinkSetRangeController extends Disposable {
if (command.id === AddHyperLinkCommand.id) {
const params = command.params as IAddHyperLinkCommandParams;
const { unitId, subUnitId, link } = params;
const currentCell = this._getCurrentCell(unitId, subUnitId, link.row, link.column);
const redoParams: ISetRangeValuesMutationParams = {
unitId,
subUnitId,
cellValue: {
[link.row]: {
[link.column]: {
v: link.display,
t: CellValueType.STRING,
// t: CellValueType.STRING, // Setting a link to a number is still a number
p: null,
t: currentCell?.t ?? undefined, // Keep force string type
},
},
},
Expand Down Expand Up @@ -258,4 +260,8 @@ export class SheetHyperLinkSetRangeController extends Disposable {
},
}));
}

private _getCurrentCell(unitId: string, subUnitId: string, row: number, col: number) {
return this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)?.getSheetBySheetId(subUnitId)?.getCell(row, col);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ describe('test "SetRangeValuesMutation" ', () => {

expect(checkCellValueType(1, CellValueType.BOOLEAN)).toBe(CellValueType.BOOLEAN); // it is valid boolean value
expect(checkCellValueType(0, CellValueType.BOOLEAN)).toBe(CellValueType.BOOLEAN); // it is valid boolean value

expect(checkCellValueType('2', CellValueType.FORCE_STRING)).toBe(CellValueType.FORCE_STRING); // do not change cell value type when it is force string
});

it('should be able to cast values that can be casted to boolean', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ export const SetRangeValuesMutation: IMutation<ISetRangeValuesMutationParams, bo
export function checkCellValueType(v: Nullable<CellValue>, oldType: Nullable<CellValueType>): Nullable<CellValueType> {
if (v === null) return null;

if (oldType === CellValueType.FORCE_STRING) return oldType;

if (typeof v === 'string') {
if (isSafeNumeric(v)) {
if ((+v === 0 || +v === 1) && oldType === CellValueType.BOOLEAN) {
Expand Down

0 comments on commit 17130d1

Please sign in to comment.