oazabir / GoogleDoc2Html

Export Google Doc as clean html. Handy to make a Wordpress post from Google Doc.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot get indices for attribute changes from content in a TableCell

axemonkey opened this issue · comments

This script is working almost exactly as I need it, but for one crucial thing.

The processText function uses getTextAttributeIndices() to work out where bold, links, italics etc start and finish in a block of text. That works perfectly on a Text element, but not on a TableCell element.

I have content editors entering content into tables for specific HTML components, which I need to parse and render. But when trying to use this method the console gives me the message:

TypeError: Cannot find function getTextAttributeIndices in object TableCell.

...because there is mo method getTextAttributeIndices for a TableCell. And if I use getText() on my table cell first, I lose the formatting and the indices vanish.

I cannot find a workaround at the moment.

Did you find anything ? I have the same problem...I use a lot of tables for formatting.

I did, but it was some time ago. Looking at the code I haven't touched for a while it looks like what I did was this (apols for massive code paste...):

function processTextFromTable(item) {
  var cellOutput = [];
  
  var text = item.getText();
  if (!text.length) {
    return '';
  }
  
  var childItems = item.getNumChildren();

  for (var childItemIndex = 0; childItemIndex < childItems; childItemIndex++) {
    var childItem = item.getChild(childItemIndex);
    
    if (childItem.getType() == 'PARAGRAPH') {
      if (childItem.getText().length) {      
        var textElement = childItem.findElement(DocumentApp.ElementType.TEXT).getElement();
        if (textElement) {
          cellOutput.push(processItem(textElement));
          if (childItem.getNextSibling()) {
            cellOutput.push('<br>');
            
            if (childItem.getNextSibling().getType() == 'PARAGRAPH') {
              var nextPara = childItem.getNextSibling();
              if (nextPara.getText().length == 0) {
                cellOutput.push('<br>');
              }
            }
          }
        }
      } else {
        cellOutput.push('');
      }
    } else if (childItem.getType() == 'LIST_ITEM') {
      cellOutput.push(processListItem(childItem));
    }
  }

  var trimmedCellOutput = trimArray(cellOutput);
  var cellOutputString = trimmedCellOutput.join('');
  return cellOutputString;
}

function processItem(textItem) {
  var output = [];
  var text = textItem.getText();
  var indices = textItem.getTextAttributeIndices();

...

processTextFromTable gets sent a TableCell object, then cycles through child items enumerated with getNumChildren using getChild, then for each one gets a text item by doing var textElement = childItem.findElement(DocumentApp.ElementType.TEXT).getElement(); then sending that to another function which is able to use getTextAttributeIndices on the output.

Hope that helps!!