3
votes

I am trying to make automated reports by filling a form. The data is read out by the script from the last spreadsheet line. Then the report is made as a Google Doc. Here some tags inside this document present where the items should be. %meterx% is for meters.

These can be images or text. For normal paragraphs, this is working fine with the first loop where the type = paragraph. But it skips tables. I need to replace the %meterx% in a table cell with the image, just like I do with the paragraph, but I am stuck at the code to look through the table.

I see some ways to replace text but this seems to be the only way to replace it with images.

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

   if (type =='PARAGRAPH'){
      el[j]=element.getText()

      if(el[j]=='%meter3%'){element.removeFromParent();
      var newimage = UrlFetchApp.fetch('http://chart.googleapis.com/chart?chf=bg,s,67676700&chs=280x150&cht=gm&chds=0,10&chd=t:'+row[4]+'&chdlp=b').getBlob();   
      doc.insertImage(j, newimage); 

   if (type =='TABLE'){
      var tablerows=element.getNumRows();
      Logger.log(tablerows);
      for ( var i = 0; i < tablerows; ++i ) {
        var tablerow = element.getRow(0)
        Logger.log(tablerow);   // <--- gives TableRow
      }  /// STUCK !! :)
1

1 Answers

3
votes

A TableRow contains child elements, of type TableCell, which also contain child elements. Those are type PARAGRAPH if they are blank or contain text.

You can access the text within a TableCell with getText(), but it's a good idea to confirm that the cell contains text first.

The code below handles PARAGRAPH and TABLE element types, and for tables it explores the TABLECELL elements. I don't know what you're doing with your array el[], so I've left that out, and also commented out the image-replacement code - instead, I'm just logging the structure and content of the table for illustration. To complete your goal, you should replace the logging with the same match & replace behavior you have for PARAGRAPH.

NOTE: I am using a couple of helper functions not detailed here, which should be self-explanatory, getFileByName_() and elementTypeToText_().

function Q13869576() {
  var folder = "StackOverflow";
  var docname = "Q13869576.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:
        el[j]=element.getText()

        if(el[j]=='%meter3%'){
          Logger.log( "Found tag in paragraph" );
//          element.removeFromParent();
//          var newimage = UrlFetchApp.fetch('http://chart.googleapis.com/chart?chf=bg,s,67676700&chs=280x150&cht=gm&chds=0,10&chd=t:'+row[4]+'&chdlp=b').getBlob();   
//          doc.insertImage(j, newimage);
        }
        break;

      case DocumentApp.ElementType.TABLE:
        var tablerows=element.getNumRows();
        Logger.log(tablerows);
        for ( var row = 0; row < tablerows; ++row ) {
          var tablerow = element.getRow(row)
          for ( var cell=0; cell < tablerow.getNumCells(); ++cell) {
            Logger.log( "Table Row("+row+") Cell ("+cell+")");
            Logger.log(
                  elementTypeToText_(tablerow.getChild(cell).getType())
                  +" with child type "
                  +elementTypeToText_(tablerow.getChild(cell).getChild(0).getType()));
            var celltext = tablerow.getChild(cell).getText();
            Logger.log( "Text is ("+celltext+")" );
          }
        }
        break;
    }
  }
}

This is an excerpt of the logs from a sample doc, with a table containing a cell with the %meter3% tag:

...
Table Row(1) Cell (2)
TABLE_CELL with child type PARAGRAPH
Text is (%meter3%)
...