Google Sheets – Apply Complicated Formula to Entire Column

google sheets

I tried searching for this but to no avail – how can I copy a quite complicated formula through the whole column AND make it apply to rows which might be added to the bottom of the sheet?

The formula is this:

    =CONCATENATE(A5,IF(ISERROR(FIND("?",A5,1)),CONCATENATE("?"),CONCATENATE("&")),"utm_campaign=",SUBSTITUTE(B5," ", "%20") ,"&utm_medium=",SUBSTITUTE(C5," ", "%20"),"&utm_source=",SUBSTITUTE(D5," ", "%20"),IF(E5<>"",CONCATENATE("&utm_content=",SUBSTITUTE(E5," ", "%20")),),IF(F5<>"",CONCATENATE("&utm_term=",SUBSTITUTE(F5," ", "%20")),))

Basically, what it does is it concatenates data from columns A, B, C, D, E, F with some Google "utm" tags and creates tagged destination link. Along the way it checks for various errors and replaces " " with "%20".

Best Answer

I was able to reduce the formula and add ARRAYFORMULAE to make it work with ranges.

Formula

 =ARRAYFORMULA(
    IF(ISBLANK(A:A), 
      "", 
      SUBSTITUTE(
        ARRAYFORMULA(
          A:A & 
          IF(ISERROR(FIND("?", A:A, 1)), "?", "&") & 
          "utm_campaign=" & 
          B:B & 
          "&utm_medium=" & 
          C:C & 
          "&utm_source=" & 
          D:D & 
          IF(E:E<>"","&utm_content=","") & 
          E:E & 
          IF(F:F<>"","&utm_term=","") & 
          F:F
        )
      , " ",  "%20"
      )
    )
  )

 copy / paste
 =ARRAYFORMULA(IF(ISBLANK(A:A), "", SUBSTITUTE(ARRAYFORMULA(A:A & IF(ISERROR(FIND("?", A:A, 1)), "?", "&") & "utm_campaign=" & B:B & "&utm_medium=" & C:C & "&utm_source=" & D:D & IF(E:E<>"","&utm_content=","") & E:E & IF(F:F<>"","&utm_term=","") & F:F), " ",  "%20")))

Explained

It is not possible to use CONCATENATE formulae in combination with ARRAYFORMULAE. Therefore we need to go about this differently, by using an AMPERSAND to join different strings together. If the concatenated string is prepared and the ranges are chosen, then substitute all empty spaces with %20. This, together with an IF statement to check column A for presence, is all wrapped inside yet another ARRAYFORMULA.

Screenshot

enter image description here

Example

I've created an example file for you: Apply complicated formula to whole column