自学内容网 自学内容网

【Luckysheet】修改源码,实现 rangePullAfter 钩子,获取下拉复制后的范围

导语

Luckysheet 的官方文档:整体配置 | Luckysheet文档

可以看到:官方提供的 rangePullAfter 处于 TODO 状态,也就是还没开发。。。。。。

但是我项目中又需要用到这个,所以我就斗胆改了下源码,暂时实现了 rangePullAfter 的钩子函数,但不保证会不会出现乱七八糟的问题,只是发出来分享一下。

修改

1. 准备工作

Luckysheet 的 git 地址:

Luckysheet: 🚀Luckysheet ,一款纯前端类似excel的在线表格,功能强大、配置简单、完全开源。

把项目拉到本地,clone也好,直接下载也行。

2. 打开 src/controllers/dropCell.js 文件

找到 update 方法,在方法的最后面,加上这两句代码:

import method from "../global/method";

Store.luckysheet_cell_selected_applyRange = applyRange
method.createHookFunction('rangePullAfter', Store.luckysheet_cell_selected_applyRange);

 用不用这个 Store.luckysheet_cell_selected_applyRange 都没关系,也可以直接写成:

import method from "../global/method";

method.createHookFunction('rangePullAfter', applyRange);

我主要是怕万一以后要用上呢,所以就存起来了。

如果还是不清楚加在哪里,下面是完整的 update 方法代码:

    update: function(){
        let _this = this;
        if(!checkProtectionLockedRangeList([_this.applyRange], Store.currentSheetIndex)){
            return;
        }
        if(Store.allowEdit===false){
            return;
        }
        let d = editor.deepCopyFlowData(Store.flowdata);
        let file = Store.luckysheetfile[getSheetIndex(Store.currentSheetIndex)];
        let cfg = $.extend(true, {}, Store.config);
        let borderInfoCompute = getBorderInfoCompute();
        let dataVerification = $.extend(true, {}, file["dataVerification"]);
        let direction = _this.direction;
        let type = _this.applyType;
        //复制范围
        let copyRange = _this.copyRange;
        let copy_str_r = copyRange["row"][0], copy_end_r = copyRange["row"][1];
        let copy_str_c = copyRange["column"][0], copy_end_c = copyRange["column"][1];
        let copyData = _this.getCopyData(d, copy_str_r, copy_end_r, copy_str_c, copy_end_c, direction);
        let csLen;
        if(direction == "down" || direction == "up"){
            csLen = copy_end_r - copy_str_r + 1;
        }
        else if(direction == "right" || direction == "left"){
            csLen = copy_end_c - copy_str_c + 1;
        }
        //应用范围
        let applyRange = _this.applyRange;
        let apply_str_r = applyRange["row"][0], apply_end_r = applyRange["row"][1];
        let apply_str_c = applyRange["column"][0], apply_end_c = applyRange["column"][1];
        if(direction == "down" || direction == "up"){
            let asLen = apply_end_r - apply_str_r + 1;

            for(let i = apply_str_c; i <= apply_end_c; i++){
                let copyD = copyData[i - apply_str_c];

                let applyData = _this.getApplyData(copyD, csLen, asLen);

                if(direction == "down"){
                    for(let j = apply_str_r; j <= apply_end_r; j++){
                        let cell = applyData[j - apply_str_r];

                        if(cell.f != null){
                            let f = "=" + formula.functionCopy(cell.f, "down", j - apply_str_r + 1);
                            let v = formula.execfunction(f, j, i);

                            formula.execFunctionGroup(j, i, v[1], undefined, d);

                            cell.f = v[2];
                            cell.v = v[1];

                            if(cell.spl != null){
                                cell.spl = v[3].data;
                            }
                            else{
                                if(isRealNum(cell.v) && !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(cell.v)){
                                    if(cell.v == Infinity || cell.v == -Infinity){
                                        cell.m = cell.v.toString();
                                    }
                                    else{
                                        if(cell.v.toString().indexOf("e") > -1){
                                            let len = cell.v.toString().split(".")[1].split("e")[0].length;
                                            if(len > 5){
                                                len = 5;
                                            }

                                            cell.m = cell.v.toExponential(len).toString();
                                        }
                                        else{
                                            let mask;
                                            if(cell.ct.fa === "##0.00"){
                                               /* 如果是数字类型 */
                                               mask = genarate(Math.round(cell.v * 1000000000) / 1000000000 + ".00") ;
                                               cell.m = mask[0].toString();
                                            }else {
                                                mask = genarate(Math.round(cell.v * 1000000000) / 1000000000);
                                                cell.m = mask[0].toString();
                                            }
                                        }
                                    }

                                    cell.ct = cell.ct || { "fa": "General", "t": "n" };
                                }
                                else{
                                    let mask = genarate(cell.v);
                                    cell.m = mask[0].toString();
                                    cell.ct = mask[1];
                                }
                            }
                        }

                        d[j][i] = cell;

                        //边框
                        let bd_r = copy_str_r + (j - apply_str_r) % csLen;
                        let bd_c = i;

                        if(borderInfoCompute[bd_r + "_" + bd_c]){
                            let bd_obj = {
                                "rangeType": "cell",
                                "value": {
                                    "row_index": j,
                                    "col_index": i,
                                    "l": borderInfoCompute[bd_r + "_" + bd_c].l,
                                    "r": borderInfoCompute[bd_r + "_" + bd_c].r,
                                    "t": borderInfoCompute[bd_r + "_" + bd_c].t,
                                    "b": borderInfoCompute[bd_r + "_" + bd_c].b
                                }
                            }

                            cfg["borderInfo"].push(bd_obj);
                        }
                        else if(borderInfoCompute[j + "_" + i]){
                            let bd_obj = {
                                "rangeType": "cell",
                                "value": {
                                    "row_index": j,
                                    "col_index": i,
                                    "l": null,
                                    "r": null,
                                    "t": null,
                                    "b": null
                                }
                            }

                            cfg["borderInfo"].push(bd_obj);
                        }

                        //数据验证
                        if(dataVerification[bd_r + "_" + bd_c]){
                            dataVerification[j + "_" + i] = dataVerification[bd_r + "_" + bd_c];
                        }
                    }
                }
                if(direction == "up"){
                    for(let j = apply_end_r; j >= apply_str_r; j--){
                        let cell = applyData[apply_end_r - j];

                        if(cell.f != null){
                            let f = "=" + formula.functionCopy(cell.f, "up", apply_end_r - j + 1);
                            let v = formula.execfunction(f, j, i);

                            formula.execFunctionGroup(j, i, v[1], undefined, d);

                            cell.f = v[2];
                            cell.v = v[1];

                            if(cell.spl != null){
                                cell.spl = v[3].data;
                            }
                            else{
                                if(isRealNum(cell.v) && !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(cell.v)){
                                    if(cell.v == Infinity || cell.v == -Infinity){
                                        cell.m = cell.v.toString();
                                    }
                                    else{
                                        if(cell.v.toString().indexOf("e") > -1){
                                            let len = cell.v.toString().split(".")[1].split("e")[0].length;
                                            if(len > 5){
                                                len = 5;
                                            }

                                            cell.m = cell.v.toExponential(len).toString();
                                        }
                                        else{
                                            let mask = genarate(Math.round(cell.v * 1000000000) / 1000000000);
                                            cell.m = mask[0].toString();
                                        }
                                    }

                                    cell.ct = { "fa": "General", "t": "n" };
                                }
                                else{
                                    let mask = genarate(cell.v);
                                    cell.m = mask[0].toString();
                                    cell.ct = mask[1];
                                }
                            }
                        }

                        d[j][i] = cell;

                        //边框
                        let bd_r = copy_end_r - (apply_end_r - j) % csLen;
                        let bd_c = i;

                        if(borderInfoCompute[bd_r + "_" + bd_c]){
                            let bd_obj = {
                                "rangeType": "cell",
                                "value": {
                                    "row_index": j,
                                    "col_index": i,
                                    "l": borderInfoCompute[bd_r + "_" + bd_c].l,
                                    "r": borderInfoCompute[bd_r + "_" + bd_c].r,
                                    "t": borderInfoCompute[bd_r + "_" + bd_c].t,
                                    "b": borderInfoCompute[bd_r + "_" + bd_c].b
                                }
                            }

                            cfg["borderInfo"].push(bd_obj);
                        }
                        else if(borderInfoCompute[j + "_" + i]){
                            let bd_obj = {
                                "rangeType": "cell",
                                "value": {
                                    "row_index": j,
                                    "col_index": i,
                                    "l": null,
                                    "r": null,
                                    "t": null,
                                    "b": null
                                }
                            }

                            cfg["borderInfo"].push(bd_obj);
                        }

                        //数据验证
                        if(dataVerification[bd_r + "_" + bd_c]){
                            dataVerification[j + "_" + i] = dataVerification[bd_r + "_" + bd_c];
                        }
                    }
                }
            }
        }
        else if(direction == "right" || direction == "left"){
            let asLen = apply_end_c - apply_str_c + 1;

            for(let i = apply_str_r; i <= apply_end_r; i++){
                let copyD = copyData[i - apply_str_r];

                let applyData = _this.getApplyData(copyD, csLen, asLen);

                if(direction == "right"){
                    for(let j = apply_str_c; j <= apply_end_c; j++){
                        let cell = applyData[j - apply_str_c];

                        if(cell.f != null){
                            let f = "=" + formula.functionCopy(cell.f, "right", j - apply_str_c + 1);
                            let v = formula.execfunction(f, i, j);

                            formula.execFunctionGroup(i, j, v[1], undefined, d);

                            cell.f = v[2];
                            cell.v = v[1];

                            if(cell.spl != null){
                                cell.spl = v[3].data;
                            }
                            else{
                                if(isRealNum(cell.v) && !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(cell.v)){
                                    if(cell.v == Infinity || cell.v == -Infinity){
                                        cell.m = cell.v.toString();
                                    }
                                    else{
                                        if(cell.v.toString().indexOf("e") > -1){
                                            let len = cell.v.toString().split(".")[1].split("e")[0].length;
                                            if(len > 5){
                                                len = 5;
                                            }

                                            cell.m = cell.v.toExponential(len).toString();
                                        }
                                        else{
                                            let mask = genarate(Math.round(cell.v * 1000000000) / 1000000000);
                                            cell.m = mask[0].toString();
                                        }
                                    }

                                    cell.ct = { "fa": "General", "t": "n" };
                                }
                                else{
                                    let mask = genarate(cell.v);
                                    cell.m = mask[0].toString();
                                    cell.ct = mask[1];
                                }
                            }
                        }

                        d[i][j] = cell;

                        //边框
                        let bd_r = i;
                        let bd_c = copy_str_c + (j - apply_str_c) % csLen;

                        if(borderInfoCompute[bd_r + "_" + bd_c]){
                            let bd_obj = {
                                "rangeType": "cell",
                                "value": {
                                    "row_index": i,
                                    "col_index": j,
                                    "l": borderInfoCompute[bd_r + "_" + bd_c].l,
                                    "r": borderInfoCompute[bd_r + "_" + bd_c].r,
                                    "t": borderInfoCompute[bd_r + "_" + bd_c].t,
                                    "b": borderInfoCompute[bd_r + "_" + bd_c].b
                                }
                            }

                            cfg["borderInfo"].push(bd_obj);
                        }
                        else if(borderInfoCompute[i + "_" + j]){
                            let bd_obj = {
                                "rangeType": "cell",
                                "value": {
                                    "row_index": i,
                                    "col_index": j,
                                    "l": null,
                                    "r": null,
                                    "t": null,
                                    "b": null
                                }
                            }

                            cfg["borderInfo"].push(bd_obj);
                        }

                        //数据验证
                        if(dataVerification[bd_r + "_" + bd_c]){
                            dataVerification[i + "_" + j] = dataVerification[bd_r + "_" + bd_c];
                        }
                    }
                }
                if(direction == "left"){
                    for(let j = apply_end_c; j >= apply_str_c; j--){
                        let cell = applyData[apply_end_c - j];

                        if(cell.f != null){
                            let f = "=" + formula.functionCopy(cell.f, "left", apply_end_c - j + 1);
                            let v = formula.execfunction(f, i, j);

                            formula.execFunctionGroup(i, j, v[1], undefined, d);

                            cell.f = v[2];
                            cell.v = v[1];

                            if(cell.spl != null){
                                cell.spl = v[3].data;
                            }
                            else{
                                if(isRealNum(cell.v) && !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(cell.v)){
                                    if(cell.v == Infinity || cell.v == -Infinity){
                                        cell.m = cell.v.toString();
                                    }
                                    else{
                                        if(cell.v.toString().indexOf("e") > -1){
                                            let len = cell.v.toString().split(".")[1].split("e")[0].length;
                                            if(len > 5){
                                                len = 5;
                                            }

                                            cell.m = cell.v.toExponential(len).toString();
                                        }
                                        else{
                                            let mask = genarate(Math.round(cell.v * 1000000000) / 1000000000);
                                            cell.m = mask[0].toString();
                                        }
                                    }

                                    cell.ct = { "fa": "General", "t": "n" };
                                }
                                else{
                                    let mask = genarate(cell.v);
                                    cell.m = mask[0].toString();
                                    cell.ct = mask[1];
                                }
                            }
                        }

                        d[i][j] = cell;

                        //边框
                        let bd_r = i;
                        let bd_c = copy_end_c - (apply_end_c - j) % csLen;

                        if(borderInfoCompute[bd_r + "_" + bd_c]){
                            let bd_obj = {
                                "rangeType": "cell",
                                "value": {
                                    "row_index": i,
                                    "col_index": j,
                                    "l": borderInfoCompute[bd_r + "_" + bd_c].l,
                                    "r": borderInfoCompute[bd_r + "_" + bd_c].r,
                                    "t": borderInfoCompute[bd_r + "_" + bd_c].t,
                                    "b": borderInfoCompute[bd_r + "_" + bd_c].b
                                }
                            }

                            cfg["borderInfo"].push(bd_obj);
                        }
                        else if(borderInfoCompute[i + "_" + j]){
                            let bd_obj = {
                                "rangeType": "cell",
                                "value": {
                                    "row_index": i,
                                    "col_index": j,
                                    "l": null,
                                    "r": null,
                                    "t": null,
                                    "b": null
                                }
                            }

                            cfg["borderInfo"].push(bd_obj);
                        }

                        //数据验证
                        if(dataVerification[bd_r + "_" + bd_c]){
                            dataVerification[i + "_" + j] = dataVerification[bd_r + "_" + bd_c];
                        }
                    }
                }
            }
        }
        //条件格式
        let cdformat = $.extend(true, [], file["luckysheet_conditionformat_save"]);
        if(cdformat != null && cdformat.length > 0){
            for(let i = 0; i < cdformat.length; i++){
                let cdformat_cellrange = cdformat[i].cellrange;

                let emptyRange = [];

                for(let j = 0; j < cdformat_cellrange.length; j++){
                    let range = conditionformat.CFSplitRange(cdformat_cellrange[j], {"row": copyRange["row"], "column": copyRange["column"]}, {"row": applyRange["row"], "column": applyRange["column"]}, "operatePart");
                    if(range.length > 0){
                        emptyRange = emptyRange.concat(range);
                    }
                }

                if(emptyRange.length > 0){
                    cdformat[i].cellrange.push(applyRange);
                }
            }
        }
        //刷新一次表格
        let allParam = {
            "cfg": cfg,
            "cdformat": cdformat,
            "dataVerification": dataVerification
        }
        jfrefreshgrid(d, Store.luckysheet_select_save, allParam);
        Store.luckysheet_cell_selected_applyRange = applyRange
        // 重点!!!!!!!
        method.createHookFunction('rangePullAfter', Store.luckysheet_cell_selected_applyRange);
        selectHightlightShow();
    },

 3. 打开 src/index.html

在 hook 里面添加 rangePullAfter

这个应该蛮好找的吧~ 可以搜 rangeSelect ,加在它后面就行了

rangePullAfter: function (range) {
// console.info(range)
},

4. 打包 npm run build

把打包后的dist文件拖入自己的项目中就行了,因为我们的项目是本地引用的,所以是这种方式,其他的方式,看你们自己需要就好~


原文地址:https://blog.csdn.net/alikami/article/details/144266677

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!