Skip to content

Commit

Permalink
Merge branch 'dev' into fix/cursor-at-start
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs authored Nov 26, 2024
2 parents f4f9af6 + d1d4bbe commit dd9bfbb
Show file tree
Hide file tree
Showing 54 changed files with 376 additions and 274 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/sheets/worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ export class Worksheet {
return this._spanModel;
}

getStyleDataByHash(hash: string): Nullable<IStyleData> {
const data = this._styles.get(hash);
return { ...data };
}

setStyleData(style: IStyleData): Nullable<string> {
return this._styles.setValue(style);
}

/**
* Get the style of the column.
* @param {number} column The column index
Expand Down
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 @@ -257,7 +257,7 @@ export function countWorkingDays(startDateSerialNumber: number, endDateSerialNum
workingDays++;
}

return end > start ? workingDays : -workingDays;
return end >= start ? workingDays : -workingDays;
}

export function getDateSerialNumberByWorkingDays(startDateSerialNumber: number, workingDays: number, weekend: number | string = 1, holidays?: number[]): (number | ErrorValueObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

import { describe, expect, it } from 'vitest';

import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
import { BooleanValueObject, NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_DATE } from '../../function-names';
import { NetworkdaysIntl } from '../index';
import { BooleanValueObject, NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
import { ErrorType } from '../../../../basics/error-type';

describe('Test networkdays.intl function', () => {
const testFunction = new NetworkdaysIntl(FUNCTION_NAMES_DATE.NETWORKDAYS);
Expand All @@ -31,12 +32,12 @@ describe('Test networkdays.intl function', () => {
const startDate = StringValueObject.create('2012-10-1');
const endDate = StringValueObject.create('2013-3-1');
const result = testFunction.calculate(startDate, endDate);
expect(result.getValue()).toStrictEqual(110);
expect(getObjectValue(result)).toStrictEqual(110);

const weekend2 = NumberValueObject.create(6);
const holidays2 = StringValueObject.create('2012-11-22');
const result2 = testFunction.calculate(startDate, endDate, weekend2, holidays2);
expect(result2.getValue()).toStrictEqual(108);
expect(getObjectValue(result2)).toStrictEqual(108);

const weekend3 = StringValueObject.create('0011001');
const holidays3 = ArrayValueObject.create({
Expand All @@ -53,114 +54,114 @@ describe('Test networkdays.intl function', () => {
column: 0,
});
const result3 = testFunction.calculate(startDate, endDate, weekend3, holidays3);
expect(result3.getValue()).toStrictEqual(85);
expect(getObjectValue(result3)).toStrictEqual(85);
});

it('Value is number or date string', () => {
const startDate = NumberValueObject.create(45122);
const endDate = StringValueObject.create('2013-3-1');
const result = testFunction.calculate(startDate, endDate);
expect(result.getValue()).toStrictEqual(-2706);
expect(getObjectValue(result)).toStrictEqual(-2706);
});

it('Value is not date string', () => {
const startDate = StringValueObject.create('test');
const endDate = StringValueObject.create('2013-3-1');
const result = testFunction.calculate(startDate, endDate);
expect(result.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);

const startDate2 = StringValueObject.create('2012-10-1');
const endDate2 = StringValueObject.create('test');
const result2 = testFunction.calculate(startDate2, endDate2);
expect(result2.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);

const startDate3 = StringValueObject.create('2012-10-1');
const endDate3 = StringValueObject.create('2013-3-1');
const weekend3 = StringValueObject.create('test');
const result3 = testFunction.calculate(startDate3, endDate3, weekend3);
expect(result3.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result3)).toStrictEqual(ErrorType.VALUE);

const startDate4 = StringValueObject.create('2012-10-1');
const endDate4 = StringValueObject.create('2013-3-1');
const weekend4 = StringValueObject.create('1110001');
const holidays4 = StringValueObject.create('test');
const result4 = testFunction.calculate(startDate4, endDate4, weekend4, holidays4);
expect(result4.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result4)).toStrictEqual(ErrorType.VALUE);
});

it('Value is blank cell', () => {
const startDate = NullValueObject.create();
const endDate = StringValueObject.create('2013-3-1');
const result = testFunction.calculate(startDate, endDate);
expect(result.getValue()).toStrictEqual(29525);
expect(getObjectValue(result)).toStrictEqual(29525);

const startDate2 = StringValueObject.create('2012-10-1');
const endDate2 = NullValueObject.create();
const result2 = testFunction.calculate(startDate2, endDate2);
expect(result2.getValue()).toStrictEqual(-29416);
expect(getObjectValue(result2)).toStrictEqual(-29416);

const startDate3 = StringValueObject.create('2012-10-1');
const endDate3 = StringValueObject.create('2013-3-1');
const weekend3 = NullValueObject.create();
const result3 = testFunction.calculate(startDate3, endDate3, weekend3);
expect(result3.getValue()).toStrictEqual(ErrorType.NUM);
expect(getObjectValue(result3)).toStrictEqual(ErrorType.NUM);

const startDate4 = StringValueObject.create('2012-10-1');
const endDate4 = StringValueObject.create('2013-3-1');
const weekend4 = NumberValueObject.create(5);
const holidays4 = NullValueObject.create();
const result4 = testFunction.calculate(startDate4, endDate4, weekend4, holidays4);
expect(result4.getValue()).toStrictEqual(108);
expect(getObjectValue(result4)).toStrictEqual(108);
});

it('Value is boolean', () => {
const startDate = BooleanValueObject.create(true);
const endDate = StringValueObject.create('2013-3-1');
const result = testFunction.calculate(startDate, endDate);
expect(result.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);

const startDate2 = StringValueObject.create('2012-10-1');
const endDate2 = BooleanValueObject.create(false);
const result2 = testFunction.calculate(startDate2, endDate2);
expect(result2.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);

const startDate3 = StringValueObject.create('2012-10-1');
const endDate3 = StringValueObject.create('2013-3-1');
const weekend3 = BooleanValueObject.create(true);
const result3 = testFunction.calculate(startDate3, endDate3, weekend3);
expect(result3.getValue()).toStrictEqual(110);
expect(getObjectValue(result3)).toStrictEqual(110);

const startDate4 = StringValueObject.create('2012-10-1');
const endDate4 = StringValueObject.create('2013-3-1');
const weekend4 = NumberValueObject.create(1);
const holidays4 = BooleanValueObject.create(true);
const result4 = testFunction.calculate(startDate4, endDate4, weekend4, holidays4);
expect(result4.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result4)).toStrictEqual(ErrorType.VALUE);
});

it('Value is error', () => {
const startDate = ErrorValueObject.create(ErrorType.NAME);
const endDate = StringValueObject.create('2013-3-1');
const result = testFunction.calculate(startDate, endDate);
expect(result.getValue()).toStrictEqual(ErrorType.NAME);
expect(getObjectValue(result)).toStrictEqual(ErrorType.NAME);

const startDate2 = StringValueObject.create('2012-10-1');
const endDate2 = ErrorValueObject.create(ErrorType.NAME);
const result2 = testFunction.calculate(startDate2, endDate2);
expect(result2.getValue()).toStrictEqual(ErrorType.NAME);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.NAME);

const startDate3 = StringValueObject.create('2012-10-1');
const endDate3 = StringValueObject.create('2013-3-1');
const weekend3 = ErrorValueObject.create(ErrorType.NAME);
const result3 = testFunction.calculate(startDate3, endDate3, weekend3);
expect(result3.getValue()).toStrictEqual(ErrorType.NAME);
expect(getObjectValue(result3)).toStrictEqual(ErrorType.NAME);

const startDate4 = StringValueObject.create('2012-10-1');
const endDate4 = StringValueObject.create('2013-3-1');
const weekend4 = NumberValueObject.create(1);
const holidays4 = ErrorValueObject.create(ErrorType.NAME);
const result4 = testFunction.calculate(startDate4, endDate4, weekend4, holidays4);
expect(result4.getValue()).toStrictEqual(ErrorType.NAME);
expect(getObjectValue(result4)).toStrictEqual(ErrorType.NAME);
});

it('Weekend value is array', () => {
Expand Down Expand Up @@ -194,12 +195,29 @@ describe('Test networkdays.intl function', () => {
column: 0,
});
const result = testFunction.calculate(startDate, endDate, weekend, holidays);
expect(transformToValue(result.getArrayValue())).toStrictEqual([
expect(getObjectValue(result)).toStrictEqual([
[107, ErrorType.NUM, ErrorType.NUM],
[ErrorType.VALUE, ErrorType.NUM, ErrorType.NUM],
[106, ErrorType.NUM, ErrorType.NUM],
[ErrorType.NUM, ErrorType.NUM, ErrorType.NUM],
]);
});

it('More test', () => {
const startDate = StringValueObject.create('2024-11-25');
const endDate = StringValueObject.create('2024-11-25');
const result = testFunction.calculate(startDate, endDate);
expect(getObjectValue(result)).toBe(1);

const startDate2 = StringValueObject.create('2024-11-24');
const endDate2 = StringValueObject.create('2024-11-25');
const result2 = testFunction.calculate(startDate2, endDate2);
expect(getObjectValue(result2)).toBe(1);

const startDate3 = StringValueObject.create('2024-11-25');
const endDate3 = StringValueObject.create('2024-11-26');
const result3 = testFunction.calculate(startDate3, endDate3);
expect(getObjectValue(result3)).toBe(2);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { APPLY_TYPE, getAutoFillRepeatRange, IAutoFillService, virtualizeDiscret
export class DataValidationAutoFillController extends Disposable {
constructor(
@IAutoFillService private readonly _autoFillService: IAutoFillService,
@Inject(SheetDataValidationModel) private readonly _dataValidationModel: SheetDataValidationModel,
@Inject(SheetDataValidationModel) private readonly _sheetDataValidationModel: SheetDataValidationModel,
@Inject(Injector) private readonly _injector: Injector
) {
super();
Expand All @@ -37,7 +37,7 @@ export class DataValidationAutoFillController extends Disposable {

const generalApplyFunc = (location: IAutoFillLocation, applyType: APPLY_TYPE) => {
const { source: sourceRange, target: targetRange, unitId, subUnitId } = location;
const ruleMatrixCopy = this._dataValidationModel.getRuleObjectMatrix(unitId, subUnitId).clone();
const ruleMatrixCopy = this._sheetDataValidationModel.getRuleObjectMatrix(unitId, subUnitId).clone();

const virtualRange = virtualizeDiscreteRanges([sourceRange, targetRange]);
const [vSourceRange, vTargetRange] = virtualRange.ranges;
Expand Down Expand Up @@ -75,27 +75,26 @@ export class DataValidationAutoFillController extends Disposable {
sourceRange
);
const { row: sourceRow, col: sourceCol } = mapFunc(sourcePositionRange.startRow, sourcePositionRange.startColumn);
const ruleId = this._dataValidationModel.getRuleIdByLocation(unitId, subUnitId, sourceRow, sourceCol);
if (ruleId) {
const targetPositionRange = Rectangle.getPositionRange(
{
startRow: row,
startColumn: col,
endColumn: col,
endRow: row,
},
targetRange
);
const { row: targetRow, col: targetCol } = mapFunc(targetPositionRange.startRow, targetPositionRange.startColumn);
// if ruleId exists, set more dv rules, if not, clear dv rules.
const ruleId = this._sheetDataValidationModel.getRuleIdByLocation(unitId, subUnitId, sourceRow, sourceCol) || '';
const targetPositionRange = Rectangle.getPositionRange(
{
startRow: row,
startColumn: col,
endColumn: col,
endRow: row,
},
targetRange
);
const { row: targetRow, col: targetCol } = mapFunc(targetPositionRange.startRow, targetPositionRange.startColumn);

additionMatrix.setValue(targetRow, targetCol, ruleId);
additionRules.add(ruleId);
}
additionMatrix.setValue(targetRow, targetCol, ruleId);
additionRules.add(ruleId);
});
});
const additions = Array.from(additionRules).map((id) => ({ id, ranges: queryObjectMatrix(additionMatrix, (value) => value === id) }));
ruleMatrixCopy.addRangeRules(additions);
const diffs = ruleMatrixCopy.diff(this._dataValidationModel.getRules(unitId, subUnitId));
const diffs = ruleMatrixCopy.diff(this._sheetDataValidationModel.getRules(unitId, subUnitId));
const { redoMutations, undoMutations } = getDataValidationDiffMutations(unitId, subUnitId, diffs, this._injector, 'patched', applyType === APPLY_TYPE.ONLY_FORMAT);
return {
undos: undoMutations,
Expand All @@ -111,7 +110,7 @@ export class DataValidationAutoFillController extends Disposable {
const { source: sourceRange, unitId, subUnitId } = location;
for (const row of sourceRange.rows) {
for (const col of sourceRange.cols) {
const dv = this._dataValidationModel.getRuleByLocation(unitId, subUnitId, row, col);
const dv = this._sheetDataValidationModel.getRuleByLocation(unitId, subUnitId, row, col);
if (dv && disabledDataVallation.indexOf(dv.type) > -1) {
this._autoFillService.setDisableApplyType(APPLY_TYPE.SERIES, true);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,34 @@ export function getDataValidationDiffMutations(
},
} as IUpdateDataValidationMutationParams,
});
} else {
redoMutations.push({
id: UpdateDataValidationMutation.id,
params: {
unitId,
subUnitId,
ruleId: diff.ruleId,
payload: {
type: UpdateRuleType.RANGE,
payload: diff.newRanges,
},
source,
} as IUpdateDataValidationMutationParams,
});

undoMutations.unshift({
id: UpdateDataValidationMutation.id,
params: {
unitId,
subUnitId,
ruleId: diff.ruleId,
payload: {
type: UpdateRuleType.RANGE,
payload: diff.oldRanges,
},
source,
} as IUpdateDataValidationMutationParams,
});
}
} else {
redoMutations.push({
Expand Down
10 changes: 9 additions & 1 deletion packages/sheets-data-validation/src/utils/formula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,13 @@ export function isLegalFormulaResult(res: string) {
* Judge if the data-validation's formula need to be offseted by ranges
*/
export function isCustomFormulaType(type: DataValidationType) {
return type !== DataValidationType.LIST && type !== DataValidationType.LIST_MULTIPLE && type !== DataValidationType.CHECKBOX && type !== DataValidationType.ANY;
// types not in this list is formula type
const invalidTypes = [
DataValidationType.LIST,
DataValidationType.LIST_MULTIPLE,
DataValidationType.CHECKBOX,
DataValidationType.ANY,
];

return !invalidTypes.includes(type);
}
Loading

0 comments on commit dd9bfbb

Please sign in to comment.