0

I've just started using Office Scripts in Excel but can't find the right function to do a simple substring operation. The code below isn't working for me but I'd like it to just return the first 3 characters "ABC".

let bigString = "ABCDEFG"
console.log(bigString.substring(0,3))
Will Man
  • 1
  • 3
  • What do you mean by "not working"? Have you checked the Output tab on the bottom left of the code editor? I've copied and pasted this code and it works fine. – Paul Kelly Jun 11 '21 at 04:23
  • You're right, it is working for me as well now! Although the substring part is still flagged as a 'Problem' ([32, 58] Property 'substring' does not exist on type 'string | number | boolean'. Property 'substring' does not exist on type 'number'.) – Will Man Jun 11 '21 at 09:43

1 Answers1

1

I think you are getting the string by calling getValue or getValues function on a Range. Since a value can be a string, boolean or a number, the return type of the getValue(s) functions is a combination (union) of the three. string | number | boolean. So, when you call string functions on it, it shows that 'Problem'.

If you always expect string values, you can convert the data to string like so -

let bigString = range.getValue() as string or

let bigStringValues = range.getValues() as string [][]