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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(sheet): support rule registry in auto fill (#545)
YuHongc55868ca3d04
diff --git a/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts b/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.tsindex c3efee6bd0..378346f102 100644--- a/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts+++ b/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts@@ -25,6 +25,7 @@ import {APPLY_TYPE,APPLY_TYPE_IN_USE,DATA_TYPE,+ ICopyDataInType,ICopyDataPiece,IRuleConfirmedData,} from '../services/auto-fill/type';@@ -207,14 +208,14 @@ export class AutoFillController extends Disposable {copyDataInType.forEach((copySquad) => {const s = getLenS(copySquad.index, rsd);const len = copySquad.index.length * num + s;- const arrData = this._applyFunctions(copySquad.data, len, direction, applyType, customApplyFunctions);+ const arrData = this._applyFunctions(copySquad, len, direction, applyType, customApplyFunctions);const arrIndex = getDataIndex(csLen, asLen, copySquad.index);applyDataInTypes[type].push({ data: arrData, index: arrIndex });});});// calc index- for (let x = 1; x <= asLen; x++) {+ for (let x = 0; x < asLen; x++) {rules.forEach((r) => {const { type } = r;const applyDataInType = applyDataInTypes[type];@@ -230,19 +231,20 @@ export class AutoFillController extends Disposable {}private _applyFunctions(- data: Array<Nullable<ICellData>>,+ copySquad: ICopyDataInType,len: number,direction: Direction,applyType: APPLY_TYPE_IN_USE,customApplyFunctions?: APPLY_FUNCTIONS) {+ const { data } = copySquad;const isReverse = direction === Direction.UP || direction === Direction.LEFT;// according to applyType, apply functions. if customApplyFunctions is provided, use it instead of default apply functionsif (applyType === APPLY_TYPE.COPY) {const custom = customApplyFunctions?.[APPLY_TYPE.COPY];if (custom) {- return custom(data, len, direction);+ return custom(copySquad, len, direction);}isReverse && data.reverse();return fillCopy(data, len);@@ -250,17 +252,19 @@ export class AutoFillController extends Disposable {if (applyType === APPLY_TYPE.SERIES) {const custom = customApplyFunctions?.[APPLY_TYPE.SERIES];if (custom) {- return custom(data, len, direction);+ return custom(copySquad, len, direction);}isReverse && data.reverse();- // special rules must provide custom SERIES apply functions, or will be applied as copy- const copyFunc = customApplyFunctions?.[APPLY_TYPE.COPY] || fillCopy;- return copyFunc(data, len, direction);+ // special rules, if not provide custom SERIES apply functions, will be applied as copy+ if (customApplyFunctions?.[APPLY_TYPE.COPY]) {+ return customApplyFunctions[APPLY_TYPE.COPY](copySquad, len, direction);+ }+ return fillCopy(data, len);}if (applyType === APPLY_TYPE.ONLY_FORMAT) {const custom = customApplyFunctions?.[APPLY_TYPE.ONLY_FORMAT];if (custom) {- return custom(data, len, direction);+ return custom(copySquad, len, direction);}return fillCopyStyles(data, len);}@@ -315,19 +319,19 @@ export class AutoFillController extends Disposable {const last = typeInfo![typeInfo!.length - 1];last.data.push(data);- last.index.push(b - b1 + 1);+ last.index.push(b - b1);} else {const typeInfo = copyDataPiece[type];if (typeInfo) {typeInfo.push({data: [data],- index: [b - b1 + 1],+ index: [b - b1],});} else {copyDataPiece[type] = [{data: [data],- index: [b - b1 + 1],+ index: [b - b1],},];}@@ -345,6 +349,7 @@ export class AutoFillController extends Disposable {this._autoFillService.getRules().forEach((r) => {copyDataPiece[r.type] = [];});+return copyDataPiece;}diff --git a/packages/ui-plugin-sheets/src/services/auto-fill/auto-fill.service.ts b/packages/ui-plugin-sheets/src/services/auto-fill/auto-fill.service.tsindex 2d2f746239..5f5b1eca67 100644--- a/packages/ui-plugin-sheets/src/services/auto-fill/auto-fill.service.ts+++ b/packages/ui-plugin-sheets/src/services/auto-fill/auto-fill.service.ts@@ -93,11 +93,21 @@ export class AutoFillService extends Disposable implements IAutoFillService {chnWeek3Rule,loopSeriesRule,otherRule,- ];- // this._applyType = APPLY_TYPE.SERIES;+ ].sort((a, b) => b.priority - a.priority);this._isFillingStyle = true;}+ registryRule(rule: IAutoFillRule) {+ // if rule.type is used, console error+ if (this._rules.find((r) => r.type === rule.type)) {+ throw new Error(`Registry rule failed, type '${rule.type}' already exist!`);+ return;+ }+ // insert rules according to the rule.priority, the higher priority will be inserted at the beginning of the array+ const index = this._rules.findIndex((r) => r.priority < rule.priority);+ this._rules.splice(index === -1 ? this._rules.length : index, 0, rule);+ }+getRules() {return this._rules;}diff --git a/packages/ui-plugin-sheets/src/services/auto-fill/rules.ts b/packages/ui-plugin-sheets/src/services/auto-fill/rules.tsindex a23912d1b3..84dd336428 100644--- a/packages/ui-plugin-sheets/src/services/auto-fill/rules.ts+++ b/packages/ui-plugin-sheets/src/services/auto-fill/rules.ts@@ -21,6 +21,7 @@ import { APPLY_TYPE, DATA_TYPE, IAutoFillRule } from './type';export const numberRule: IAutoFillRule = {type: DATA_TYPE.NUMBER,+ priority: 1000,match: (cellData) => typeof cellData?.v === 'number' || !isNaN(Number(cellData?.v)),// TODO@yuhongz: not the good way to match number, will be changed after cell type is supportedisContinue: (prev, cur) => {@@ -30,7 +31,8 @@ export const numberRule: IAutoFillRule = {return false;},applyFunctions: {- [APPLY_TYPE.SERIES]: (data, len, direction) => {+ [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {+ const { data } = dataWithIndex;if (direction === Direction.LEFT || direction === Direction.UP) {data.reverse();}@@ -41,6 +43,7 @@ export const numberRule: IAutoFillRule = {export const otherRule: IAutoFillRule = {type: DATA_TYPE.OTHER,+ priority: 0,match: () => true,isContinue: (prev, cur) => {if (prev.type === DATA_TYPE.OTHER) {@@ -52,6 +55,7 @@ export const otherRule: IAutoFillRule = {export const extendNumberRule: IAutoFillRule = {type: DATA_TYPE.EXTEND_NUMBER,+ priority: 900,match: (cellData) => matchExtendNumber(cellData?.m || '').isExtendNumber,isContinue: (prev, cur) => {if (prev.type === DATA_TYPE.EXTEND_NUMBER) {@@ -64,7 +68,9 @@ export const extendNumberRule: IAutoFillRule = {return false;},applyFunctions: {- [APPLY_TYPE.SERIES]: (data, len, direction) => {+ [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {+ const { data } = dataWithIndex;+let step;if (data.length === 1) {step = direction === Direction.DOWN || direction === Direction.RIGHT ? 1 : -1;@@ -93,6 +99,7 @@ export const extendNumberRule: IAutoFillRule = {export const chnNumberRule: IAutoFillRule = {type: DATA_TYPE.CHN_NUMBER,+ priority: 830,match: (cellData) => {if (isChnNumber(cellData?.m || '')) {return true;@@ -106,7 +113,9 @@ export const chnNumberRule: IAutoFillRule = {return false;},applyFunctions: {- [APPLY_TYPE.SERIES]: (data, len, direction) => {+ [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {+ const { data } = dataWithIndex;+const isReverse = direction === Direction.LEFT || direction === Direction.UP;if (data.length === 1) {const formattedValue = data[0]?.m;@@ -177,6 +186,7 @@ export const chnNumberRule: IAutoFillRule = {export const chnWeek2Rule: IAutoFillRule = {type: DATA_TYPE.CHN_WEEK2,+ priority: 820,match: (cellData) => {if (isChnWeek2(cellData?.m || '')) {return true;@@ -185,7 +195,9 @@ export const chnWeek2Rule: IAutoFillRule = {},isContinue: (prev, cur) => prev.type === DATA_TYPE.CHN_WEEK2,applyFunctions: {- [APPLY_TYPE.SERIES]: (data, len, direction) => {+ [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {+ const { data } = dataWithIndex;+const isReverse = direction === Direction.LEFT || direction === Direction.UP;if (data.length === 1) {let step;@@ -231,10 +243,13 @@ export const chnWeek2Rule: IAutoFillRule = {export const chnWeek3Rule: IAutoFillRule = {type: DATA_TYPE.CHN_WEEK3,+ priority: 810,match: (cellData) => isChnWeek3(cellData?.m || ''),isContinue: (prev, cur) => prev.type === DATA_TYPE.CHN_WEEK3,applyFunctions: {- [APPLY_TYPE.SERIES]: (data, len, direction) => {+ [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {+ const { data } = dataWithIndex;+const isReverse = direction === Direction.LEFT || direction === Direction.UP;if (data.length === 1) {let step;@@ -282,6 +297,7 @@ export const chnWeek3Rule: IAutoFillRule = {export const loopSeriesRule: IAutoFillRule = {type: DATA_TYPE.LOOP_SERIES,+ priority: 800,match: (cellData) => isLoopSeries(cellData?.m || ''),isContinue: (prev, cur) => {if (prev.type === DATA_TYPE.LOOP_SERIES) {@@ -290,7 +306,8 @@ export const loopSeriesRule: IAutoFillRule = {return false;},applyFunctions: {- [APPLY_TYPE.SERIES]: (data, len, direction) => {+ [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {+ const { data } = dataWithIndex;const isReverse = direction === Direction.LEFT || direction === Direction.UP;const { series } = getLoopSeriesInfo(data[0]?.m || '');if (data.length === 1) {@@ -340,6 +357,7 @@ export const loopSeriesRule: IAutoFillRule = {*/export const formulaRule: IAutoFillRule = {type: DATA_TYPE.FORMULA,+ priority: 1000,match: (cellData) => isFormulaString(cellData?.f),isContinue: (prev, cur) => {if (prev.type === DATA_TYPE.FORMULA) {@@ -349,6 +367,9 @@ export const formulaRule: IAutoFillRule = {},applyFunctions: {// FIXME@Dushusir: COPY not work- [APPLY_TYPE.COPY]: (data, len, direction) => fillCopyFormula(data, len, direction),+ [APPLY_TYPE.COPY]: (dataWithIndex, len, direction) => {+ const { data, index } = dataWithIndex; // TODO@Dushusir: now you have index infos when applying formula+ return fillCopyFormula(data, len, direction);+ },},};diff --git a/packages/ui-plugin-sheets/src/services/auto-fill/tools.ts b/packages/ui-plugin-sheets/src/services/auto-fill/tools.tsindex f6fab19425..c61b77f6fa 100644--- a/packages/ui-plugin-sheets/src/services/auto-fill/tools.ts+++ b/packages/ui-plugin-sheets/src/services/auto-fill/tools.ts@@ -1,7 +1,5 @@import { Direction, ICellData, Nullable, Tools } from '@univerjs/core';-import { DATA_TYPE, ICopyDataPiece } from './type';-export const chnNumChar = { 零: 0, 一: 1, 二: 2, 三: 3, 四: 4, 五: 5, 六: 6, 七: 7, 八: 8, 九: 9 };export const chnNumChar2 = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];export const chnUnitSection = ['', '万', '亿', '万亿', '亿亿'];@@ -22,18 +20,6 @@ export const chnNameValue = {亿: { value: 100000000, secUnit: true },};-export function getEmptyCopyDataPiece(): ICopyDataPiece {- return {- [DATA_TYPE.NUMBER]: [],- [DATA_TYPE.DATE]: [],- [DATA_TYPE.EXTEND_NUMBER]: [],- [DATA_TYPE.CHN_NUMBER]: [],- [DATA_TYPE.CHN_WEEK2]: [],- [DATA_TYPE.CHN_WEEK3]: [],- [DATA_TYPE.FORMULA]: [],- [DATA_TYPE.OTHER]: [],- };-}export function chineseToNumber(chnStr?: Nullable<string>) {if (!chnStr) {return 0;@@ -253,7 +239,7 @@ export function getLenS(indexArr: any[], rsd: number) {let s = 0;for (let j = 0; j < indexArr.length; j++) {- if (indexArr[j] <= rsd) {+ if (indexArr[j] < rsd) {s++;} else {break;@@ -290,14 +276,14 @@ export function getDataIndex(csLen: number, asLen: number, indexArr: number[]) {let sum = 0;if (num > 0) {- for (let i = 1; i <= num; i++) {+ for (let i = 0; i < num; i++) {for (let j = 0; j < indexArr.length; j++) {- obj[indexArr[j] + (i - 1) * csLen] = sum;+ obj[indexArr[j] + i * csLen] = sum;sum++;}}for (let a = 0; a < indexArr.length; a++) {- if (indexArr[a] <= rsd) {+ if (indexArr[a] < rsd) {obj[indexArr[a] + csLen * num] = sum;sum++;} else {@@ -306,7 +292,7 @@ export function getDataIndex(csLen: number, asLen: number, indexArr: number[]) {}} else {for (let a = 0; a < indexArr.length; a++) {- if (indexArr[a] <= rsd) {+ if (indexArr[a] < rsd) {obj[indexArr[a]] = sum;sum++;} else {diff --git a/packages/ui-plugin-sheets/src/services/auto-fill/type.ts b/packages/ui-plugin-sheets/src/services/auto-fill/type.tsindex cddf752e45..0fad27edb4 100644--- a/packages/ui-plugin-sheets/src/services/auto-fill/type.ts+++ b/packages/ui-plugin-sheets/src/services/auto-fill/type.ts@@ -13,7 +13,7 @@ export enum DATA_TYPE {}export type ICopyDataPiece = {- [key in DATA_TYPE]?: ICopyDataInType[];+ [key: string]: ICopyDataInType[];};export interface ICopyDataInType {@@ -24,20 +24,21 @@ export interface ICopyDataInType {export type ICopyDataInTypeIndexInfo = number[];export interface IAutoFillRule {- type: DATA_TYPE;+ type: string;match: (cellData: Nullable<ICellData>) => boolean;isContinue: (prev: IRuleConfirmedData, cur: Nullable<ICellData>) => boolean;applyFunctions?: APPLY_FUNCTIONS;+ priority: number;}export interface IRuleConfirmedData {- type?: DATA_TYPE;+ type?: string;cellData: Nullable<ICellData>;}export type APPLY_FUNCTIONS = {[key in APPLY_TYPE]?: (- data: Array<Nullable<ICellData>>,+ dataWithIndex: ICopyDataInType,len: number,direction: Direction) => Array<Nullable<ICellData>>;