Quantcast
Channel: Active questions tagged worksheet-function - Super User
Viewing all articles
Browse latest Browse all 906

Can't make my code work with ARRAYFUNCTION [migrated]

$
0
0

I'm new to this, but I finally got something working that does what I need in Google Sheets, and I got it all into one cell. But it won't work with the ARRAYFUNCTION which is necessary for the sheet I'm making; where new Rows could be added in the list at any time.


To explain what the function needs to do:

First it needs to look at 3 data fields input by a human. All of these are in 3 separate cells on the same row.

  1. Column A, Name
  2. Column B, Number
  3. Column C, Category (this is a drop-down)

Then it takes the input of #1 and uses TRIM to and SUBSTITUTE (spaces " " or dashes "-") to remove any human error and place underscores between words.

Next is checks the category and if it matches 1 type it formats the output in style 1, else the format becomes style 2. When it does this TEXTJOIN is used to place the result of the last part together with the rest of the formatting.

Finally it displays the output in all capital letters.


The Code in Question:

IF( ISTEXT( INDIRECT( "RC[-3]", FALSE)), UPPER(  TEXTJOIN( , , IF( INDIRECT( "RC[-1]", FALSE)="type1", TEXTJOIN( , , "type1_ref(", IF( N( INDIRECT( "RC[-2]", FALSE))<9, " "&N( INDIRECT( "RC[-2]", FALSE)), N( INDIRECT( "RC[-2]", FALSE))), ", ref_" ), "ref_"), SUBSTITUTE( SUBSTITUTE( TRIM( INDIRECT( "RC[-3]", FALSE)), " ",  "_"), "-", "_"), IF( INDIRECT( "RC[-1]", FALSE)="type1", "),", ","))), "—")

This is the completely nested version, but I'll break it down by sections:

IF( ISTEXT( INDIRECT( "RC[-3]", FALSE)), ... , "—”)

This IF determines if there is text in Column A. If there's not then the output is just a dash. INDIRECT is used to always target a cell exactly 3 cells of the left.

UPPER(  TEXTJOIN( , , IF(( INDIRECT( "RC[-1]", FALSE)="type1", ... ", ref_", "ref_"), SUBSTITUTE(), IF((INDIRECT( "RC[-1]", FALSE)="type1", "),", ","))

When there is text in Column A then it uses the TEXTJOIN function to generate the formated output on the left and right side of the output. All of which is made uppercase by UPPER at the end.

The IF functions both check if the drop-down Category in Column C is type1 or not, synchronizing the left and right side of the TEXTJOIN output. And the SUBSTITUTE in the middle will produce the words from Column A.

TEXTJOIN( , , "type1_ref(", IF( N( INDIRECT( "RC[-2]", FALSE))<9, " "&N( INDIRECT( "RC[-2]", FALSE)), N( INDIRECT( "RC[-2]", FALSE))),

In the "..." above goes this TEXTJOIN which primarily has the function of formatting the number from Column B based on the number of digits it has. If it's only 1 (determined if it's value is 9 or less) then a space is used in the 10's place. Then it outputs "type1_ref(##, ref_" where the ## are the numbers. (The numbers should only ever be between 0-99)

The N() function is used because in testing the INDIRECT function was copying everything over, including formatting or even font choice. N() provides only the numeric contents.

SUBSTITUTE( SUBSTITUTE( TRIM( INDIRECT( "RC[-3]", FALSE)), " ",  "_"), "-", "_"),

In the empty parenthesis above goes this. Which simply targets the contents of Column A and removes leading or lagging spaces and double spaces. Once only the expected amount of spaces remain, Substitute replaces any dashes or spaces with underscores.


All combined these nested functions, when the data is entered correctly, will output either:

  • TYPE1_REF(##, REF_EXAMPLE_TEXT),
  • REF_EXAMPLE_TEXT,

This is exactly the right format I want for the final output.

But it won't work with ARRAYFUNCTION!

What can I do? Do I need to change something? Is there something I'm missing or did wrong?


Viewing all articles
Browse latest Browse all 906

Trending Articles