question

RohanShaw-5155 avatar image
0 Votes"
RohanShaw-5155 asked YutaoHuang-MSFT answered

addNamedItem(name, reference, comment) not working

I am trying to use the addnameditem in a work sheet.
it looks like it works because if i run thescript twice with the same name it throws an error but i cant see the named range on the excel sheet.
is this function fully supported in the API yet?

office-scripts-excel-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

YutaoHuang-MSFT avatar image
0 Votes"
YutaoHuang-MSFT answered

@RohanShaw-5155 -

Would you mind sharing your script?

This code worked for me:

function main(workbook: ExcelScript.Workbook) {
  const worksheet = workbook.getWorksheet("Sheet1");
  worksheet.addNamedItem("my_named_item", "=A5:B5");
  const namedItem = worksheet.getNamedItem("my_named_item");
  console.log(namedItem.getRange().getValues());
}


Please note in order to correctly specify the range when adding a new named item, you need to use the form of =XXXX for the second parameter of addNamedItem().

If you just do worksheet.addNamedItem("my_named_item", "A5:B5") (without = in the second parameter), that will only create a named item that references a string value "A5:B5", not an actual reference to the range of A5:B5.

You can try getting an existing named item and calling getType() on it to see what type it is. If you mean to create a named item referencing a range, getType() should return Range.

console.log(worksheet.getNamedItem("my_named_item").getType());
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.