MergeCell in same Line ou Coloumn error

Hi.

I’ve tring mergeCells using macro… but when i’ve two merge cell in sequence, both for column and for row, the macro is falling.

I’ve tring in demos but recive the same error.

macro exemple:

  // INIT MACRO
      var oDocument = Api.GetDocument();
      var oSection = oDocument.GetFinalSection();
      var nWidth = oSection.GetPageWidth();
      var oTable = Api.CreateTable(6, 1);
      oTable.SetWidth("twips", 6912);
    
 var oTableRow = oTable.GetRow(0);
    oTableRow.SetHeight("atLeast", 320)

      // GET CELL 0,0
      var oCell00 = oTable.MergeCells([oTable.GetRow(0).GetCell(0),oTable.GetRow(0).GetCell(1),oTable.GetRow(0).GetCell(2)]);
      var oCellContent00 = oCell00.GetContent();
      var oCellElement00 = oCellContent00.GetElement(0);
      oCellElement00.AddText('MERGE MESMA LINHA');
    

      // ADD CELL TEXT STYLES
      oCellElement00.SetFontSize(22)
      oCellElement00.SetFontFamily('Calibri, "sans-serif"')
      oCellElement00.SetJc('center')
    

      // ADD CELL STYLES
      oCell00.SetVerticalAlign('bottom');
      oTable.AddElement(oCell00, 0, oCellContent00);
    

      // GET CELL 0,3
      var oCell30 = oTable.MergeCells([oTable.GetRow(0).GetCell(3),oTable.GetRow(0).GetCell(4),oTable.GetRow(0).GetCell(5)]);
      var oCellContent30 = oCell30.GetContent();
      var oCellElement30 = oCellContent30.GetElement(0);
      oCellElement30.AddText('LINHA AO LADO');
    

      // ADD CELL TEXT STYLES
      oCellElement30.SetFontSize(22)
      oCellElement30.SetFontFamily('Calibri, "sans-serif"')
      oCellElement30.SetJc('center')
    

      // ADD CELL STYLES
      oCell30.SetVerticalAlign('bottom');
      oTable.AddElement(oCell30, 0, oCellContent30);
    
      oDocument.InsertContent([oTable]);

Hello @ricardoaleixoo

This is a problem of cell count. You see, once first three cells are merged with this passage:

var oCell00 = oTable.MergeCells([oTable.GetRow(0).GetCell(0),oTable.GetRow(0).GetCell(1), oTable.GetRow(0).GetCell(2)]);

The resulting table will contain only 4 cells in a row. After that you are trying to merge non-existent cells:

var oCell30 = oTable.MergeCells([oTable.GetRow(0).GetCell(3),oTable.GetRow(0).GetCell(4),oTable.GetRow(0).GetCell(5)]);

Considering that initial three cells were merged, they now have index 0 for merged cell, the rest three cells are now with indexes 1, 2 and 3. So, if you modify the second MergeCells method like that:

var oCell30 = oTable.MergeCells([oTable.GetRow(0).GetCell(1), oTable.GetRow(0).GetCell(2), oTable.GetRow(0).GetCell(3)]);

It will successfully create a table and insert it into the document.