Search code examples
arraysregexgoogle-sheetsnewlinetext-extraction

Extract only first line in each cell in google sheets


I want to extract only the first line of text in each cell. (Sorry for screenshots I can't figure out another way to illustrate). enter image description here

I am using this formula in the Company column to find the new-line character and extract the text to the left of it:

=LEFT(G2,Find(Char(10),G2))

If I manually go through and highlight the entire cell in the Description column then hit enter it will add a newline character after the text in the first line but otherwise there is no newline character present. Hence the #VALUE error. Any ideas how to bulk insert a newline character into every cell so the existing function will work? Open to other solutions as well. Thanks.


Solution

  • you can do:

    =LEFT(G2&CHAR(10), FIND(CHAR(10), G2&CHAR(10)))
    

    update 1:

    delete everything in column A and use this in A1:

    ={"Company"; INDEX(IFNA(REGEXEXTRACT(B2:B, "(.+)\n"), B2:B))}
    

    enter image description here


    update 2:

    ={"Company"; INDEX(TRIM(IFNA(REGEXEXTRACT(
     REGEXREPLACE(B2:B, "(\d+)", CHAR(10)&"$1"), "(.+)\n"), B2:B)))}
    

    enter image description here