Can you install rowspan / colspan via apps script in google doc?

Does anyone have an example of how / if we can set the string / colspan on a TableCell (?) Element in a Google Apps Script app?

Do I need to do this with the .setAttributes () method?

+1


source to share


3 answers


No, there is currently no way to do this. I also don't see the feature request in the Issue Tracker.

There is an .merge()

object method TableCell

that sounds promising. However, when used, it concatenates the "current" TableCell with the "previous" Sibling TableCell, adding the contents of the "current" TableCell to the "previous" and then removing the "current".

Before

Before

After



After

code

I modified the code from the previous answer to experiment with .merge()

, here it is:

function mergeExperiment() {
  var folder = "StackOverflow";
  var docname = "Experiment.gdoc";
  var docId = getFileByName_(folder, docname).getId();

  var doc = DocumentApp.openById(docId);
  var docBody = doc.getActiveSection();

  var totalElements = doc.getNumChildren();
  var el=[]
  for( var j = 0; j < totalElements; ++j ) {
    var element = doc.getChild(j);
    var type = element.getType();

    switch (type) {
      case DocumentApp.ElementType.PARAGRAPH:
        break;

      case DocumentApp.ElementType.TABLE:
        var tablerows=element.getNumRows();
        for ( var row = 0; row < tablerows; ++row ) {
          var tablerow = element.getRow(row)
          for ( var cell=0; cell < tablerow.getNumCells(); ++cell) {
            // Experiment - merge two cells in the second row
            if (row==1 && cell==1) {
              tablerow.getChild(cell).merge();
            }
          }
        }
        break;
    }
  }
}

      

+3


source


I found a very awkward workaround due to a lack colspan

in GAS ...

  • create a table ( FlexTable

    , Grid

    etc.) with the columns / rows you want.
  • when you get to the area where you want to use colspan

    or rowspan

    , create a new table (with different cell widths and heights
  • keep creating a new table every time you want to change the width or height of the cell.
  • create VerticalPanel

    and add each of the tables created in steps 1-3 to


Now you have one table with functions colspan

and rowspan

.

Not very elegant, but this is the only solution I could come up with.

+1


source


I found this question because I wanted to get rid of the RowSpan inside the table and was looking for an answer to the question "How to set RowSpan or ColSpan to 1.

As you did a lot, I had to realize that there is no way to get write access for these hidden TableCell properties.

But from my research I found the answer to this question. This is an awkward workaround, but once set up correctly it works very well:

The key is that you can delete a cell and replace it with a deep copy of the cell that you manually used before.

If you have a table with some enclosing cells created by hand, such as a table with 3 rows and 3 cells that you spanned them, then you can make a deep separate copy of the enclosing cell and paste it anywhere on the other table. The entered cell in the target table will span the surrounding cells as it does in the source table.

You should be aware that the contents of the surrounding cells still exist, but are not visible. And if you remove the spanning cell again or you automatically fall away, they will be shown again.

This insertion of an already enclosing srcCell has the same effect as if you were placing 3x3 cells overlapping white paper in the destination table. All the cells are still there, but you can't see them. The guugel-doc engine only displays the contents of the enclosing cell.

And this behavior can be copied. Isn't that nice?

I know it's a joke if you need to cover many cells with many different widths and directions of coverage, but if you've designed it for your needs it might be worth taking the time to deploy cell templates.

You can create a document that contains only tables with all the enclosing cells. And you can use this document as your "Spanning Cells Source".

You can make a copy of the following test document which contains 3 tables and clearly shows the principle behind it. The first table contains a 3x3 cell covering tableindex 0,0. The second table contains the indexes of each cell in the cell itself as row pairs, col and "TestCell", which must span 3x3 other cells. The third table is for comparison only:

https://docs.google.com/document/d/1G8C2JP_4689RFmtxHZ2djslwoGWwHwieHfbIwr2XzeQ/edit

And then you have access to the linked script, which saves the content of the "TestCell", replaces it with 3 different cells, and puts the content back in. You will see the following associated script:

var doc     = DocumentApp.getActiveDocument();
var docBody = doc.getBody();

function myFunction() {
  var tables   = docBody.getTables();
  var srcTable = tables[ 0 ];
  var dstTable = tables[ 1 ];

  var foundText = docBody.findText( "TestCell" );
  if ( foundText ) {
    //  The cell that should span others
    var dstCell = foundText.getElement().getParent().getParent();

    //  If you want to preserve the contents of the cell
    var cellContents = [];
    var numChildren  = dstCell.getNumChildren();
    for ( var i = 0; i<numChildren; i++ ) {
      cellContents.push( dstCell.getChild( i ).copy() );
    }

    //  Row- and ColumnIndex of the future spanning cell
    var dstRowIndex = dstTable.getChildIndex( dstCell.getParent() );
    var dstColIndex = dstCell.getParent().getChildIndex( dstCell );

    //  Deep, detached copy of an already spanning cell
    var srcCell = srcTable.getCell( 0, 0 ).copy();

    // delete dstCell and insert the deep, detached copy of the spanning cell
    dstCell.removeFromParent();
    dstCell = dstTable.getChild( dstRowIndex ).insertTableCell( dstColIndex, srcCell );

    //  If you preserved the contents of the deleted, non-spanning cell,
    //     you can put them back again
    for ( var i = numChildren-1; i>=0; i-- ) {
      var childType = cellContents[ i ].getType();
      // Do not forget to extend this switch by your other ElementTypes !!!
      switch ( childType ) {
        case DocumentApp.ElementType.PARAGRAPH:
          dstCell.insertParagraph( 0, cellContents[ i ] );
          break;
      }
    }
    //  Get rid of the default single and empty PARAGRAPH of the srcCell,
    //      if you had to put back the contents of the delted non-spanning cell
    dstCell.removeChild( dstCell.getChild( dstCell.getNumChildren() - 1 ) );
  }
}

      

Hope this helps.

Thank you very much for your attention,

Richard

0


source







All Articles