Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get indices for attribute changes from content in a TableCell #12

Open
axemonkey opened this issue Nov 13, 2018 · 2 comments
Open

Comments

@axemonkey
Copy link

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.

@sourcesync
Copy link

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

@axemonkey
Copy link
Author

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!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants