Google Apps Script - Document RangeBuilder Issue with Table Elements

I’m working with Google Apps Script and trying to create named ranges from selected table elements in a Google Document. However, I keep running into an error that I can’t figure out.

When I select a table in my document and run my script, I get this error message:

  • Exception: The parameters (DocumentApp.RangeElement) don’t match the method signature for DocumentApp.RangeBuilder.addElement.

The problem seems to be happening in my custom function createNamedRange(document, element, rangeName) specifically at this line:

  • builder.addElement(element)

I’m confused because the documentation says addElement should accept an element parameter, and my code is definitely passing a table element to it. The element exists and is valid right before I try to add it to the range builder.

Can someone help me understand what’s going wrong here? Any suggestions for fixing this would be really helpful.

function initialize() {
  const document = DocumentApp.getActiveDocument()
  let selectedRange = document.getSelection().getRangeElements()
  let elementsToProcess = [];

  // iterate through selected elements to find tables
  for (let index = 1; index < selectedRange.length - 1; index++) {
    let currentElement = selectedRange[index]
    let elementType = currentElement.getElement().getType()
    elementsToProcess.push(selectedRange[index])
  }
  
  // create named ranges using helper function
  for (const element of elementsToProcess) {
    createNamedRange(document, element, "My Range Name")
  }
}

/**
 * Creates a named range from the specified element
 * @param {DocumentApp.Document} document - The active document
 * @param {RangeElement} element - The element to convert to named range
 * @param {string} rangeName - Name for the new range
 */
function createNamedRange(document, element, rangeName) {
  let builder = document.newRange()
  builder.addElement(element)
  document.addNamedRange(rangeName, builder.build())
}

You’re mixing up RangeElement objects with Element objects. When you call getSelection().getRangeElements(), you get RangeElement wrappers that include extra metadata like partial selection info. But addElement() wants the actual Element object, not the wrapper. I hit this exact issue building a document automation tool last year. Just extract the real element with getElement() before passing it to the range builder. One heads up though - if you’re working with partial selections in table cells, you’ll need to handle the start and end offsets from the RangeElement to keep the exact selection boundaries. Doesn’t matter for complete element selections, but it’s crucial when users only select part of the text in table cells.

you’re passing a RangeElement to addElement when it needs just the Element. use element.getElement() instead of element in your createNamedRange function. RangeBuilder wants the actual document element, not the wrapper.

Had the exact same issue with a document parser project. You’re mixing up two different object types in the Google Apps Script API. Your selectedRange has RangeElement objects - these are basically wrappers that hold the actual document element plus metadata about the selection. But RangeBuilder.addElement needs the raw Element object, not the wrapper. Fix it by changing builder.addElement(element) to builder.addElement(element.getElement()). This pulls out the actual document element from the RangeElement wrapper. RangeElements have extra stuff like isPartial() and getStartOffset() for partial selections, but the range builder just wants the bare element reference.