Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(autofill): fix extend number rule of auto fill (#6837)

wpxp1234568fe112264edc
diff --git a/packages/sheets/src/services/auto-fill/__tests__/rules.spec.ts b/packages/sheets/src/services/auto-fill/__tests__/rules.spec.ts
index a416a583fa..edf1330681 100644
--- a/packages/sheets/src/services/auto-fill/__tests__/rules.spec.ts
+++ b/packages/sheets/src/services/auto-fill/__tests__/rules.spec.ts
@@ -104,6 +104,22 @@ describe('auto-fill rules', () => {
);
expect(fallbackCopy?.length).toBe(4);
expect(`${fallbackCopy?.[0]?.v}`).toBe('A-001');
+
+ const oneCellWithPadStart0 = extendNumberRule.applyFunctions?.[AUTO_FILL_APPLY_TYPE.SERIES]?.(
+ { data: [{ v: 'A2B3C009D' }], index: [0] },
+ 4,
+ Direction.DOWN,
+ {} as any
+ );
+ expect(oneCellWithPadStart0?.map((c) => `${c?.v}`)).toEqual(['A2B3C010D', 'A2B3C011D', 'A2B3C012D', 'A2B3C013D']);
+
+ const multipleCellsWithPadStart0 = extendNumberRule.applyFunctions?.[AUTO_FILL_APPLY_TYPE.SERIES]?.(
+ { data: [{ v: 'A2B3C003D' }, { v: 'A2B3C002D' }, { v: 'A2B3C001D' }, { v: 'A2B3C000D' }, { v: 'A2B3C001D' }, { v: 'A2B3C002D' }], index: [0, 1, 2, 3, 4, 5] },
+ 4,
+ Direction.DOWN,
+ {} as any
+ );
+ expect(multipleCellsWithPadStart0?.map((c) => `${c?.v}`)).toEqual(['A2B3C003D', 'A2B3C004D', 'A2B3C005D', 'A2B3C006D']);
});
it('chinese-number rule should handle normal numbers and week cycle', () => {
diff --git a/packages/sheets/src/services/auto-fill/__tests__/tool.spec.ts b/packages/sheets/src/services/auto-fill/__tests__/tool.spec.ts
index 01ab87e025..976d5927c7 100644
--- a/packages/sheets/src/services/auto-fill/__tests__/tool.spec.ts
+++ b/packages/sheets/src/services/auto-fill/__tests__/tool.spec.ts
@@ -83,7 +83,8 @@ describe('Test tool', () => {
it('matches and extends numeric suffix in text', () => {
expect(matchExtendNumber('item12')).toEqual({
isExtendNumber: true,
- matchTxt: 12,
+ rawMatchTxt: '12',
+ matchNumber: 12,
beforeTxt: 'item',
afterTxt: '',
});
@@ -94,6 +95,8 @@ describe('Test tool', () => {
expect(getLenS([0, 2, 4], 3)).toBe(2);
expect(isEqualDiff([1, 3, 5, 7])).toBeTruthy();
expect(isEqualDiff([1, 3, 6])).toBeFalsy();
+ expect(isEqualDiff([4, 3, 2, 1, 0, 1, 2])).toBeTruthy();
+ expect(isEqualDiff([4, 2, 0, 1, 2])).toBeFalsy();
expect(isEqualRatio([2, 4, 8])).toBeTruthy();
expect(isEqualRatio([2, 4, 9])).toBeFalsy();
expect(getXArr(4)).toEqual([1, 2, 3, 4]);
@@ -148,6 +151,60 @@ describe('Test tool', () => {
expect(formatOnly[0]).toEqual({ s: 's1' });
});
+ it('fills extended text-number with padStart 0', () => {
+ const extended = fillExtendNumber([{ v: 'item009', s: 's1', custom: { flag: true } }], 5, 1);
+ expect(extended.map((d) => d?.v)).toEqual(['item010', 'item011', 'item012', 'item013', 'item014']);
+ expect(extended[0]?.custom).toBeUndefined();
+
+ const formatOnly = fillOnlyFormat([{ v: 1, s: 's1', custom: { flag: true } }], 1);
+ expect(formatOnly[0]).toEqual({ s: 's1' });
+ });
+
+ it('fills extended text-number with padStart 0 and end with text', () => {
+ const extended = fillExtendNumber([{ v: 'item009abc', s: 's1', custom: { flag: true } }], 5, 1);
+ expect(extended.map((d) => d?.v)).toEqual(['item010abc', 'item011abc', 'item012abc', 'item013abc', 'item014abc']);
+ expect(extended[0]?.custom).toBeUndefined();
+
+ const formatOnly = fillOnlyFormat([{ v: 1, s: 's1', custom: { flag: true } }], 1);
+ expect(formatOnly[0]).toEqual({ s: 's1' });
+ });
+
+ it('fills extended text-number with padStart 0 and end with multiple number block', () => {
+ const extended = fillExtendNumber([{ v: 'A2B3C4', s: 's1', custom: { flag: true } }], 5, 1);
+ expect(extended.map((d) => d?.v)).toEqual(['A2B3C5', 'A2B3C6', 'A2B3C7', 'A2B3C8', 'A2B3C9']);
+ expect(extended[0]?.custom).toBeUndefined();
+
+ const formatOnly = fillOnlyFormat([{ v: 1, s: 's1', custom: { flag: true } }], 1);
+ expect(formatOnly[0]).toEqual({ s: 's1' });
+ });
+
+ it('fills extended text-number with padStart 0 and direction up', () => {
+ const extended = fillExtendNumber([{ v: 'item003', s: 's1', custom: { flag: true } }], 5, -1);
+ expect(extended.map((d) => d?.v)).toEqual(['item002', 'item001', 'item000', 'item001', 'item002']);
+ expect(extended[0]?.custom).toBeUndefined();
+
+ const formatOnly = fillOnlyFormat([{ v: 1, s: 's1', custom: { flag: true } }], 1);
+ expect(formatOnly[0]).toEqual({ s: 's1' });
+ });
+
+ it('fills extended text-number with padStart 0 and multiple values', () => {
+ const extended = fillExtendNumber([
+ { v: 'item004' },
+ { v: 'item003' },
+ { v: 'item002' },
+ { v: 'item001' },
+ { v: 'item000' },
+ { v: 'item001' },
+ { v: 'item002' },
+ { v: 'item003' },
+ ], 5, 1);
+ expect(extended.map((d) => d?.v)).toEqual(['item004', 'item005', 'item006', 'item007', 'item008']);
+ expect(extended[0]?.custom).toBeUndefined();
+
+ const formatOnly = fillOnlyFormat([{ v: 1, s: 's1', custom: { flag: true } }], 1);
+ expect(formatOnly[0]).toEqual({ s: 's1' });
+ });
+
it('fills chinese week and chinese number sequences', () => {
const week = fillChnWeek([{ v: '日', custom: { flag: true } }], 3, 1, 0);
expect(week.map((d) => d?.v)).toEqual(['一', '二', '三']);
diff --git a/packages/sheets/src/services/auto-fill/rules.ts b/packages/sheets/src/services/auto-fill/rules.ts
index 13ec066dcd..c28606f31e 100644
--- a/packages/sheets/src/services/auto-fill/rules.ts
+++ b/packages/sheets/src/services/auto-fill/rules.ts
@@ -108,10 +108,16 @@ const extendNumberRule: IAutoFillRule = {
match: (cellData) => matchExtendNumber(`${cellData?.v}` || '').isExtendNumber,
isContinue: (prev, cur) => {
if (prev.type === AUTO_FILL_DATA_TYPE.EXTEND_NUMBER) {
- const { beforeTxt, afterTxt } = matchExtendNumber(`${prev.cellData?.v}` || '');
- const { beforeTxt: curBeforeTxt, afterTxt: curAfterTxt } = matchExtendNumber(`${cur?.v}` || '');
- if (beforeTxt === curBeforeTxt && afterTxt === curAfterTxt) {
- return true;
+ const prevMatch = matchExtendNumber(`${prev.cellData?.v}` || '');
+ const curMatch = matchExtendNumber(`${cur?.v}` || '');
+
+ if (prevMatch.isExtendNumber && curMatch.isExtendNumber) {
+ const { beforeTxt: prevBeforeTxt, afterTxt: prevAfterTxt } = prevMatch;
+ const { beforeTxt: curBeforeTxt, afterTxt: curAfterTxt } = curMatch;
+
+ if (prevBeforeTxt === curBeforeTxt && prevAfterTxt === curAfterTxt) {
+ return true;
+ }
}
}
return false;
@@ -122,15 +128,22 @@ const extendNumberRule: IAutoFillRule = {
const isReverse = direction === Direction.UP || direction === Direction.LEFT;
let step;
+
if (data.length === 1) {
step = isReverse ? -1 : 1;
return reverseIfNeed(fillExtendNumber(data, len, step), isReverse);
}
+
const dataNumArr = [];
for (let i = 0; i < data.length; i++) {
const txt = `${data[i]?.v}`;
- txt && dataNumArr.push(Number(matchExtendNumber(txt).matchTxt));
+
+ const matchResult = matchExtendNumber(txt);
+
+ if (matchResult.isExtendNumber) {
+ dataNumArr.push(matchResult.matchNumber);
+ }
}
if (isReverse) {
@@ -139,9 +152,11 @@ const extendNumberRule: IAutoFillRule = {
}
if (isEqualDiff(dataNumArr)) {
- step = dataNumArr[1] - dataNumArr[0];
+ const dataLen = data.length;
+ step = dataNumArr[dataLen - 1] - dataNumArr[dataLen - 2];
return reverseIfNeed(fillExtendNumber(data, len, step), isReverse);
}
+
return fillCopy(data, len);
},
},
@@ -221,7 +236,8 @@ const chnNumberRule: IAutoFillRule = {
(dataNumArr[0] < 6 && dataNumArr[dataNumArr.length - 1] > 0)
) {
// Fill with sequence of Monday~Sunday
- const step = dataNumArr[1] - dataNumArr[0];
+ const dataLen = data.length;
+ const step = dataNumArr[dataLen - 1] - dataNumArr[dataLen - 2];
return reverseIfNeed(fillChnWeek(data, len, step), isReverse);
}
// Fill with sequence of Chinese lowercase numbers
@@ -283,7 +299,8 @@ const chnWeek2Rule: IAutoFillRule = {
}
if (isEqualDiff(dataNumArr)) {
- const step = dataNumArr[1] - dataNumArr[0];
+ const dataLen = data.length;
+ const step = dataNumArr[dataLen - 1] - dataNumArr[dataLen - 2];
return reverseIfNeed(fillChnWeek(data, len, step, 1), isReverse);
}
return fillCopy(data, len);
@@ -337,7 +354,8 @@ const chnWeek3Rule: IAutoFillRule = {
}
if (isEqualDiff(dataNumArr)) {
- const step = dataNumArr[1] - dataNumArr[0];
+ const dataLen = data.length;
+ const step = dataNumArr[dataLen - 1] - dataNumArr[dataLen - 2];
return reverseIfNeed(fillChnWeek(data, len, step, 2), isReverse);
}
return fillCopy(data, len);
@@ -394,7 +412,8 @@ const loopSeriesRule: IAutoFillRule = {
}
if (isEqualDiff(dataNumArr)) {
- const step = dataNumArr[1] - dataNumArr[0];
+ const dataLen = data.length;
+ const step = dataNumArr[dataLen - 1] - dataNumArr[dataLen - 2];
return reverseIfNeed(fillLoopSeries(data, len, step, series), isReverse);
}
return fillCopy(data, len);
diff --git a/packages/sheets/src/services/auto-fill/tools.ts b/packages/sheets/src/services/auto-fill/tools.ts
index 2bb8f88f47..8c8b9d5089 100644
--- a/packages/sheets/src/services/auto-fill/tools.ts
+++ b/packages/sheets/src/services/auto-fill/tools.ts
@@ -164,37 +164,37 @@ function isChnNumber(txt?: string) {
return isChnNumber;
}
-function matchExtendNumber(txt?: string) {
+function matchExtendNumber(txt?: string): {
+ isExtendNumber: false;
+} | {
+ isExtendNumber: true;
+ rawMatchTxt: string;
+ matchNumber: number;
+ beforeTxt: string;
+ afterTxt: string;
+} {
if (!txt) {
- return {
- isExtendNumber: false,
- };
- }
- const reg = /0|([1-9]+[0-9]*)/g;
- const isExtendNumber = reg.test(txt);
-
- if (isExtendNumber) {
- const match = txt.match(reg);
- if (match && match.length > 0) {
- const matchTxt = match[match.length - 1];
- const matchIndex = txt.lastIndexOf(matchTxt);
- const beforeTxt = txt.substr(0, matchIndex);
- const afterTxt = txt.substr(matchIndex + matchTxt.length);
-
- return {
- isExtendNumber: true,
- matchTxt: Number(matchTxt),
- beforeTxt,
- afterTxt,
- };
- }
- return {
- isExtendNumber: false,
- };
+ return { isExtendNumber: false };
}
+ const matches = [...txt.matchAll(/\d+/g)];
+ if (!matches.length) {
+ return { isExtendNumber: false };
+ };
+
+ const last = matches[matches.length - 1];
+ const rawMatchTxt = last[0];
+ const index = last.index;
+
+ const beforeTxt = txt.substring(0, index);
+ const afterTxt = txt.substring(index + rawMatchTxt.length);
+
return {
- isExtendNumber,
+ isExtendNumber: true,
+ rawMatchTxt,
+ matchNumber: Number(rawMatchTxt),
+ beforeTxt,
+ afterTxt,
};
}
@@ -277,17 +277,29 @@ function getLenS(indexArr: any[], rsd: number) {
* equal diff
*/
function isEqualDiff(arr: number[]) {
- let diff = true;
- const step = arr[1] - arr[0];
+ if (arr.length < 3) return true;
- for (let i = 1; i < arr.length; i++) {
- if (arr[i] - arr[i - 1] !== step) {
- diff = false;
- break;
+ let step = arr[1] - arr[0];
+ let changeStep = false;
+
+ for (let i = 2; i < arr.length; i++) {
+ const currentStep = arr[i] - arr[i - 1];
+
+ if (currentStep !== step) {
+ if (changeStep) {
+ return false;
+ }
+
+ if (currentStep !== -step) {
+ return false;
+ }
+
+ step = currentStep;
+ changeStep = true;
}
}
- return diff;
+ return true;
}
function getDataIndex(csLen: number, asLen: number, indexArr: number[]) {
@@ -467,28 +479,38 @@ function forecast(x: number, yArr: number[], xArr: number[], forward = true) {
function fillExtendNumber(data: Array<Nullable<ICellData>>, len: number, step: number) {
const applyData = [];
- const reg = /0|([1-9]+[0-9]*)/g;
+
+ const lastData = data[data.length - 1];
+ const matchResult = matchExtendNumber(`${lastData?.v}`);
+
+ if (!matchResult.isExtendNumber) {
+ return fillCopy(data, len);
+ }
+
+ const { matchNumber, rawMatchTxt, beforeTxt, afterTxt } = matchResult;
+ const width = rawMatchTxt.length;
for (let i = 1; i <= len; i++) {
const index = (i - 1) % data.length;
- const d = Tools.deepClone(data[index]);
+ const d = Tools.deepClone(data[index]);
removeCellCustom(d);
- const last = `${data[data.length - 1]?.v}`;
- if (!last) continue;
+ if (!d || !d.v) {
+ continue;
+ }
- const match = last?.match(reg);
- const lastTxt = match?.[match.length - 1];
- if (!lastTxt) continue;
+ const num = Math.abs(matchNumber + step * i);
- const num = Math.abs(Number(lastTxt) + step * i);
- const lastIndex = last.lastIndexOf(lastTxt);
- const valueTxt = last.substr(0, lastIndex) + num.toString() + last.substr(lastIndex + lastTxt.length);
+ let numStr = num.toString();
+ if (numStr.length < width) {
+ numStr = numStr.padStart(width, '0');
+ }
+
+ const valueTxt = `${beforeTxt}${numStr}${afterTxt}`;
if (d) {
d.v = valueTxt;
-
applyData.push(d);
}
}