Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

导入带图片的excel时,不支持导入绑定到单元格上的图片 #51

Open
hizikata opened this issue Oct 13, 2021 · 3 comments

Comments

@hizikata
Copy link

场景描述:
使用luckysheet导入带图片的excel,没有任何问题。然后使用exceljs导出excel,使用的是将图片绑定到单元格的方式,具体如下图所示:
image
然后将导出的excel再次导入,发现图片丢失了。
经过分析luckyexcel的源码,原因应该是luckyexcel不支持单元格上的图片导入,源码位置如下:
image
以上代码只处理了插入到某个区间的图片(tagNamexdr:twoCellAnchor),而没有处理绑定到单元格的图片(tagNamexdr:oneCellAnchor),
请问这种情况应该如何解决比较好呢?

@morrystore
Copy link

我也发现有这个问题

@morrystore
Copy link

LuckySheet.ts / 行202加上如下代码


let oneCellAnchors = this.readXml.getElementsByTagName("xdr:oneCellAnchor", drawingFile);
if(oneCellAnchors!=null && oneCellAnchors.length>0) {
for(let i=0;i<oneCellAnchors.length;i++){
let oneCellAnchor = oneCellAnchors[i];
let editAs = getXmlAttibute(oneCellAnchor.attributeList, "editAs", "oneCell");
let xdrFroms = oneCellAnchor.getInnerElements("xdr:from"), xdrTos = oneCellAnchor.getInnerElements("xdr:to");
let xdr_blipfills = oneCellAnchor.getInnerElements("a:blip");
if(xdrFroms!=null && xdr_blipfills!=null && xdrFroms.length>0 && xdr_blipfills.length>0){
let xdrFrom = xdrFroms[0], xdr_blipfill = xdr_blipfills[0];

                    let rembed = getXmlAttibute(xdr_blipfill.attributeList, "r:embed", null);

                    let imageObject = this.getBase64ByRid(rembed, drawingRelsFile);

                    let x_n =0,y_n = 0;
                    let cx_n = 0, cy_n = 0;


                    let xdr_xfrms = oneCellAnchor.getInnerElements("a:xfrm");
                    let xdr_xfrm = xdr_xfrms[0];
                    let aoff = xdr_xfrm.getInnerElements("a:off"), aext = xdr_xfrm.getInnerElements("a:ext");

                    

                    if(aoff!=null && aext!=null && aoff.length>0 && aext.length>0){
                        let aoffAttribute = aoff[0].attributeList, aextAttribute = aext[0].attributeList;
                        let x = getXmlAttibute(aoffAttribute, "x", null);
                        let y = getXmlAttibute(aoffAttribute, "y", null);

                        let cx = getXmlAttibute(aextAttribute, "cx", null);
                        let cy = getXmlAttibute(aextAttribute, "cy", null);

                        if(x!=null && y!=null && cx!=null && cy!=null && imageObject !=null){
                            x_n = getPxByEMUs(parseInt(x),),y_n = getPxByEMUs(parseInt(y));
                            cx_n = getPxByEMUs(parseInt(cx)),cy_n = getPxByEMUs(parseInt(cy));
                        }
                    }

                    

                    imageObject.fromCol = this.getXdrValue(xdrFrom.getInnerElements("xdr:col"));
                    imageObject.fromColOff = getPxByEMUs(this.getXdrValue(xdrFrom.getInnerElements("xdr:colOff")));
                    imageObject.fromRow= this.getXdrValue(xdrFrom.getInnerElements("xdr:row"));
                    imageObject.fromRowOff = getPxByEMUs(this.getXdrValue(xdrFrom.getInnerElements("xdr:rowOff")));

                    imageObject.toCol = this.getXdrValue(xdrFrom.getInnerElements("xdr:col"));
                    imageObject.toColOff = cx_n + imageObject.fromColOff;
                    imageObject.toRow = this.getXdrValue(xdrFrom.getInnerElements("xdr:row"));
                    imageObject.toRowOff = cy_n + imageObject.fromRowOff;

                    imageObject.originWidth = cx_n;
                    imageObject.originHeight = cy_n;
                    
                    if(editAs=="absolute"){
                        imageObject.type = "3";
                    }
                    else if(editAs=="oneCell"){
                        imageObject.type = "2";
                    }
                    else{
                        imageObject.type = "1";
                    }

                    imageObject.isFixedPos = false;
                    imageObject.fixedLeft = 0;
                    imageObject.fixedTop = 0;

                    let imageBorder:IluckyImageBorder = {
                        color: "#000",
                        radius: 0,
                        style: "solid",
                        width: 0
                    }
                    imageObject.border = imageBorder;

                    let imageCrop:IluckyImageCrop = {
                        height: cy_n,
                        offsetLeft: 0,
                        offsetTop: 0,
                        width: cx_n
                    }
                    imageObject.crop = imageCrop;

                    let imageDefault:IluckyImageDefault = {
                        height: cy_n,
                        left: x_n,
                        top: y_n,
                        width: cx_n
                    }
                    imageObject.default = imageDefault;

                    if(this.images==null){
                        this.images = {};
                    }
                    this.images[generateRandomIndex("image")] = imageObject;
                    console.log(imageObject);

                }
            }
        }

@su1peng2
Copy link

su1peng2 commented Oct 12, 2022

@morrystore 你好,把以上代码复制到luckyexcel.js文件后,提示找不到LuckyExcel,
并且let imageBorder:IluckyImageBorder
let imageCrop:IluckyImageCrop
let imageDefault:IluckyImageDefault 都会爆红,请问这种情况怎么解决呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants