ejsExcel icon indicating copy to clipboard operation
ejsExcel copied to clipboard

是否能把自动合并单元格的功能加入?

Open zhaohuiyingxue opened this issue 4 years ago • 0 comments

导出的时候,希望一列数据,相同的自动进行合并,为此写了一点js,希望能加入到组件中。

      const autoMergeCellArr={}
      /* @param  {Object} o 当前数据行对象
      * @param  {String|null} parent 合并参照属性,无参照可以填null或空字符串
      * @param  {String} name 合并列属性
      * @param  {Uint} col 当前列号,在模板中固定为_col
      * @param  {Uint} row 当前行号,在模板中固定为_row
      * @param  {Array} f 单元格合并范围数组,在模板中固定为_mergeCellArr_
      * @return {undefined} 没有返回值
      */
      const autoMergeCell = (o,parent,name,col,row,f)=>{
          //模板里调用需要添加以下语句,其中后三个参数不变
          //《%autoMergeCell(r,null,"XZ",_col,_row,_mergeCellArr_)%》
          const key = parent+"_"+name
          const now = {col_s : col,col_e :col, row_s :row ,row_e :row,p : o[parent],v:o[name]}
          const last = autoMergeCellArr[key]
          if(last === undefined || now.p!==last.p || now.v!==last.v){
            autoMergeCellArr[key]=now;
            return;
          }
          last.col_e = col
          last.row_e = row
          last.p=now.p
          last.v=now.v
          if(last.col_s!==last.col_e || last.row_s!==last.row_e){
            //需要合并
            let s = last.col_s+last.row_s+":"+last.col_e+last.row_e
            let i = f.findIndex(v=>v.startsWith(last.col_s+last.row_s+":"))
            if(i>-1)f[i] = s
            else f.push(s)
          }
        }

实现效果如下: image

模板内占位符内容如下

序号	:
施工性质:
施工级别:

如果能加入到新版本中的话,autoMergeCell的最后三个参数其实应该是可以不用的了

PS:我在VUE中使用的代码如下

      //生成Excel文件
      const that = this
      const ejsexcel = require("ejsexcel");
      (async function() {
        //不写这一行导出会出错
        if(window.setImmediate === undefined)window.setImmediate = f=>setTimeout(f,0)
        const exlBuf = await JSZipUtils.getBinaryContent('Template/' + that.xlsxName + '.xlsx')
        const exlBuf2 = await ejsexcel.renderExcel(Buffer.from(exlBuf), xlsData);
        saveAs(new Blob([exlBuf2]) , that.xlsxName + '.xlsx')
      })();

zhaohuiyingxue avatar Dec 09 '20 05:12 zhaohuiyingxue