I have strings with each line in one cell. The number after # means the number of items.
Is it possible on Google Spreadsheet to write a formula that allows you to process like B cell if you input strings in A cell like the attached image?
I tried to combine formulas such as JOIN, SPLIT, ARRAYFORMULA, and SUBSTITUTE, but failed. Should I learn the query?

- 124,011
- 12
- 67
- 124

- 201
- 1
- 5
2 Answers
sure:
=INDEX(TEXTJOIN(CHAR(10), 1, IF(""=SPLIT(REPT(
INDEX(SPLIT(FLATTEN(SPLIT(A1, CHAR(10))), "#"),,1)&"-×",
INDEX(SPLIT(FLATTEN(SPLIT(A1, CHAR(10))), "#"),,2)), "×"),,SPLIT(REPT(
INDEX(SPLIT(FLATTEN(SPLIT(A1, CHAR(10))), "#"),,1)&"-×",
INDEX(SPLIT(FLATTEN(SPLIT(A1, CHAR(10))), "#"),,2)), "×")&
TEXT(SEQUENCE(1, 1000), "00"))))
or arrayformula:
=INDEX(SUBSTITUTE(SUBSTITUTE(TRIM(IFNA(VLOOKUP(ROW(A1:A5), SPLIT(TRIM(FLATTEN(
QUERY(SUBSTITUTE(QUERY(IFERROR(SPLIT(FLATTEN(IF(""=IFERROR(SPLIT(REPT(
INDEX(SPLIT(FLATTEN(SPLIT(A1:A5, CHAR(10))), "#"),,1)&"-×",
INDEX(SPLIT(FLATTEN(SPLIT(A1:A5, CHAR(10))), "#"),,2)), "×")),,SPLIT(REPT(
INDEX(SPLIT(FLATTEN(ROW(A1:A5)&"♠♦"&SPLIT(A1:A5, CHAR(10))), "#"),,1)&"-×",
INDEX(SPLIT(FLATTEN(SPLIT(A1:A5, CHAR(10))), "#"),,2)), "×")&
TEXT(SEQUENCE(1, 100), "00"))), "♦")),
"select max(Col2) where Col1 is not null group by Col2 pivot Col1"),
" ", CHAR(13)),,9^9))), "♠"), 2, 0))), " ", CHAR(10)), CHAR(13), " "))

- 124,011
- 12
- 67
- 124
-
It works perfectly on my case. I stayed up all night, but I couldn't achieve it. And do you have any tips for writing long formulas? Do you write all the formulas at once? Or do you divide steps across cells for debugging and finally combine them into one formula? – J. SungHoon Nov 19 '21 at 02:32
-
@J.SungHoon depends on the formula, but mostly (like in this case) I start right in the middle and add it up like onion layers and every step is checked if it holds coz at the end when formula is assembled I am pretty much lost if something somewhere needs to be fixed. this formula can be considered as small one :) compared for example to https://webapps.stackexchange.com/a/125018/186471 – player0 Nov 19 '21 at 02:45
-
1I went to the link page, looked at step 5, and my chin fell to the floor. It's so interesting. Thank you for helping me. – J. SungHoon Nov 19 '21 at 03:07
-
@J.SungHoon if you are interested in an example of formula assembly: https://docs.google.com/spreadsheets/d/1Wtw-PMjUtIN4KMm-6088tAXtmozxwQFDAsup8KbDHRM/edit#gid=2098948025 – player0 Nov 19 '21 at 14:00
Suppose that you want to do this for multiple cells of data in the range A2:A. Clear B2:B and place the following formula in B2:
=ArrayFormula(IF(A2:A="",,IFERROR(TRIM(SUBSTITUTE(SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(REPT(REGEXEXTRACT(SPLIT(A2:A,CHAR(10)),"[^#))]+")&"~",REGEXEXTRACT(SPLIT(A2:A,CHAR(10)),"#(\d+)")*1)),,COLUMNS(SPLIT(A2:A,CHAR(10)))))),"~ ","~"),"~",CHAR(10))))))
This formula should produce all results for all rows where A2:A contains data.
If you only want to process the one cell (say, A2), you can use this version in, say, B2:
=ArrayFormula(IFERROR(TRIM(SUBSTITUTE(SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(REPT(REGEXEXTRACT(SPLIT(A2,CHAR(10)),"[^#))]+")&"~",REGEXEXTRACT(SPLIT(A2,CHAR(10)),"#(\d+)")*1)),,COLUMNS(SPLIT(A2:A,CHAR(10)))))),"~ ","~"),"~",CHAR(10)))))
ADDENDUM (after comments):
The OP realized that I'd left off the sequencing of numbers. This definitely made things quite a bit trickier. However, I was able to write an array formula that should work.
Again, supposing that the raw data runs A2:A, clear B2:B and then place the following into cell B2:
=ArrayFormula(REGEXREPLACE(REGEXREPLACE(TRANSPOSE(QUERY(TRANSPOSE(REGEXREPLACE(IFERROR(VLOOKUP(SEQUENCE(ROWS(A2:A))&"^"&SEQUENCE(1,COLUMNS(SPLIT(A2:A,CHAR(10))))&"*",TRANSPOSE(QUERY(TRANSPOSE(REGEXREPLACE(SPLIT(QUERY(FLATTEN(FILTER(IFERROR(SEQUENCE(ROWS(A2:A))&"^"&SEQUENCE(1,COLUMNS(SPLIT(A2:A,CHAR(10))))&"^"&REPT(REGEXEXTRACT(SPLIT(A2:A,CHAR(10)),"[^#))]+")&"~",REGEXEXTRACT(SPLIT(A2:A,CHAR(10)),"#(\d+)")*1)),A2:A<>"")),"Select * WHERE Col1 Is Not Null"),"~")&TEXT(SEQUENCE(1,MAX(IFERROR(SPLIT(REGEXREPLACE(A2:A,"[^#\d]",""),"#",1)))),"00")&"~","^\d+~$","")),,MAX(IFERROR(SPLIT(REGEXREPLACE(A2:A,"[^#\d]",""),"#",1))))),1,FALSE)),"^\d+\^\d+\^","")),,COLUMNS(SPLIT(A2:A,CHAR(10))))),"~\s*$",""),"~\s*",CHAR(10)))
Is it long? Sure. Does it work? Yes, it should. And being an array formula, it actually saves processing and eliminates the need to drag the formula down as new rows of data are added in A2:A.

- 9,079
- 2
- 7
- 11
-
It is good to split, repeat, and re-join the strings. However, there seems to be no formulas about the numeric suffix. – J. SungHoon Nov 19 '21 at 02:27
-
1Yes, I seem to have missed that part of the request. If I have time, I will look into this again. In the meantime, it seems player0 has provided a working formula for you. – Erik Tyler Nov 19 '21 at 03:27
-
1
-
Your formula works well, too! One regrettable thing is that even if `00` in the formula is changed to `-00` to decorate the form of suffix, if `data#2` in A2 cell and `data#5` is entered in A3 cell, `data-00`, `data-01`, and `data-02` are printed in B2, and unnecessary `-03`, `-04`, and `-05` are added. But the current formula is also cool. I wanna study steadily so that I can add the requirements I need on my own. Thanks your help. – J. SungHoon Nov 22 '21 at 03:18
-
I don't understand your extended goal. (Or, I should say, I do not have the time to analyze the extended goal for understanding.) As it is, the solutions offered have required quite a bit more time than I can generally donate to answering all posts in a given week. But I'm glad that it at least gives you food for thought. Perhaps upon analyzing the solutions offered here, you can manage extending the application of them yourself. Good luck. – Erik Tyler Nov 22 '21 at 03:26