0

When I run this code while selecting a table element on my document, I get the following error:

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

The source of the issue lies in my function nameRange(doc, el, name):

  • rangeBuilder.addElement(el)

I don't understand how I'm receiving this error, given that the parameter for .addElement is an element; this code returns a table element just prior to assigning it to a namedRange.

If you could please help me spot the error in my code, and maybe a solution to this issue, I would greatly appreciate it.

function main() {
  const doc = DocumentApp.getActiveDocument()
  let selection = doc.getSelection().getRangeElements()
  let toRename = [];

  // looping through selection to get desired table element
  for (let i = 1; i < selection.length - 1; i++) {
    let element = selection[i]
    let type = element.getElement().getType()
    toRename.push(selection[i])
  }
  
  // naming the selection with the custom fuction nameRange()
  for (const item of toRename) {
    nameRange(doc, item, "Range Title")
  }
}

/**
 * Names the given range into an accessible Named Range
 * @type {RangeElement} RangeElement
 * @param {DocumentApp.Document} doc - The Document selected.
 * @param {RangeElement} el - The element object to give the Named Range to.
 * @param {string} name - The name to give the Named Range.
 * @returns void
 */
function nameRange(doc, el, name) {
  let rangeBuilder = doc.newRange()
  rangeBuilder.addElement(el)
  doc.addNamedRange(name, rangeBuilder.build())
}

1 Answer 1

2

First of all your script will not work if selection.length is 1. The loop will not execute. Secondly you are passing a RangeElement to nameRange not an Element.

Change

rangeBuilder.addElement(el)

To

rangeBuilder.addElement(el.getElement()) 
Sign up to request clarification or add additional context in comments.

2 Comments

selection.length works because my selection is always 2 elements, a Table then a Paragraph.
Thank you! rangeBuilder.addElement(el.getElement()) was definitely the solution!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.