Forum Discussion
Office Script - Bug in .map()?
Hi,
I'm pretty sure this is a bug, but maybe I'm doing something silly when constructing the formula?
I'm trying to set formulas down a column in a table, from an array called test1. The test1 array is created using .map() from an original array (file_names) that uses .getTexts from another column in the same table.
- The dimensions of target_range (another column in the same table) match those of test1 array.
- The test1 array is appropriately 2D (i.e. [[formula1],[formula2]...[formulaN]].
- The test1 array is correct when inspected in console.log() (in all cases, including when an equals sign is used.....explained more below).
The problem is that trying to use .setFormulas, is only using the first value from test1 for every cell in target_range, not each respective element from test1.
After a massively frustrating amount of time, I've traced the problem solely to this (as far as I can tell):
The presence of an equals sign in the .map() when using string interpolation to create the formula.
This code correctly writes the text of the formula into each cell of target_range, using each respective value from write_array with appropriate interpolation:
let file_names: string[][] = rng_file_names.getTexts();
let test1: string[][] = file_names.map( row => [`\"${row}\"&\"TestString\"`] );
target_range.setFormulas(test1);If you try to do exactly the same, but try turning it into a formula by including the equals sign at the start of the interpolated string inside the array returned by the .map(), it will only write the first value of test1 into all cells of target_range:
let file_names: string[][] = rng_file_names.getTexts();
let test1: string[][] = file_names.map( row => [`=\"${row}\"&\"TestString\"`] );
target_range.setFormulas(test1);I'm pretty sure this is a bug, but if someone can point if I'm doing something stupid, it would be appreciated!
1 Reply
- m_tarlerSilver Contributor
I'm not sure. It seems to have worked fine for me:
oh dang, I'm pretty sure I know why. you are trying to put it in a Table. If you are putting a formula in a table it will repeat the formula down the column. To get that formula to reference a different row you want to use the @ symbol. so you don't even need to use the .map just assign that column a formula like
=[@file_names]&"TestString"
assuming file_names is the header name of that column. If it is "file name" then you will need double [[]] like this:
=[@[file names]]&"TestString"
ok so I confirmed if putting into a TABLE that is what will happen. Here is my modified script that appears to work:
notice i did need the double [[ ]] even though the name didn't have a space in it.
and to my point that you don't even need the .map here I change the last line to directly set the formula of the 1st cell and it will autopopulate the whole column: