Convert text to equation and return the sum result in Google Sheets - google-sheets

Let's say I have text in the following format in Column A imported to another spreadsheet (impossible to add = manually because the data is imported automatically and change):
45+5
45+3
90+2
90+7
Is there any formula that can convert this text into an equation that gives the result of the sum in Column B?
For example:
=ARRAYFORMULA(FUNCTIONTOCONVERTTEXTTOEQUATION(A1:A))
Expected Result:
50
48
92
97
Note: The texts will always be a number after the + sign and then another number.

Given your response to my clarifying question above, let's assume that your raw data is in A2:A. Place the following in the Row-2 cell (e.g., B2) of an otherwise empty column:
=ArrayFormula(IF(A2:A="",,MMULT(IFERROR(TRIM(SPLIT(A2:A,"+"))*1,0),SEQUENCE(COLUMNS(SPLIT(A2:A,"+")),1,1,0))))
MMULT is a powerful yet underused function. I'll include a graphic that explains what it does better than words might:
SPLIT will form the elements of the first matrix, while SEQUENCE will simply create the second matrix consisting of a column of 1's the same length as the number of horizontal elements formed by the SPLIT (which, in your case, will apparently always be 2).

Try, assuming the imported data starts at A1
=arrayformula(sum(value(split(A1,"+"))))
or, in a single formula at the top of the column
=mmult(arrayformula(value(split(A1:A4,"+"))),sequence(2,1,1,0))

Related

Google Sheets // Is it possible to use a cell for value Input, but also as Output from a different input?

So what I'm trying to accomplish here is having column (B) as Input.
Ascension: B5= Input, then it sums C5=B5/1000 for volume conversion.
Ullage in m3's.: D3=B3/C5
Ullage in mm's: D4=B4*C5
Row 2 and 5 are irrelevant for the request.
My question is: Can I Input a value in B3 so that Divides by C5 and shows in D3 like now, BUT, also updates D4 and therefor automatically B4.
Let's say my input in (B3) is 100 divided by 2 (C5) = (D3)50. Now I would like for (D4) to take over that 100 from (B3) and also divide that by 2 to show in (B4). BUT, now when I input a value into (B4), I want it to multiply by (C5) and do the same trick with D3 and B3.
Basically, a cross-working calculator based on what input value I have that updates the other to not get confused reading the data.
Might be too complex, but thought I'd give it a shot here. Thanks.
Google, Stackoverflow.com, Youtube, Messing around myself..
You could sort it out with hidden columns, and values that expand to the next column in which you'd like to have your result or input your value.
Instead a formula: =B3/B4 you'd have ={",",B3/B4}
So, if you manually insert a value in the right column, the formula in left column won't expand. As an example I've created a very dummy calculator of sides and hypothenuse. In B1 (column B in red would be hidden) I have:
={"","Res: "&(C3^2-C2^2)^(1/2)}
And so on in the next rows. In this picture I've inserted manual values in C1 and C2, so C3 has a result:
[]
1
And another example in another row:
You can test it out here and the extrapolate it to your formulas,
(I've erased the part with "Res. " so the result is an actual number, it was just for easier view in my examples):
https://docs.google.com/spreadsheets/d/1ZaEEGWtLU-LXBmg-xy80Ps_biuYhb8WxZBs0g1tGE00/edit?usp=drivesdk

Using ArrayFormula with a Dynamic Number of Column Header Names

My goal is to use ArrayFormula with the SPLIT() function, and name the headers of each column.
My problem is that the formula below only works when the number of headers declared exactly matches the first row's number of elements to split ie. if there are 3 elements being split on the first row, the formula needs 3 headers named (g1, g2, g3), but if any rows have more than 3 elements to split, it gives an error.
Is there a way to make the column header names dynamic in number, so that the number of elements to split can be, say, from 0-10? The elements to be split will always be separated by a comma and no spaces.
=ArrayFormula({"g1", "g2", "g3";if(A2:A="","",split(A2:A,","))})
link to example: https://docs.google.com/spreadsheets/d/1c2pskSYsGs12Yjbn-5gORQ22mDSaC9cSnp1nWeULlf4/edit?usp=sharing
You can try:
=index(iferror({"g"&sequence(1,max(len(substitute(
transpose(query(transpose(if(iferror(split(A2:A,","))="",,"z")),,9^9)),
" ",))));split(A2:A,",")}))
If we can use the Orders column, it's as simple as:
=index(iferror({"g"&sequence(1,max(B:B));split(A2:A,",")}))
You can achieve it by combining the index function, the sequence function and the max function. Here is the thought process behind it:
The max function (you can read more about it here) will retrieve the maximum value of the orders column.
The sequence function (you can read more about it here) will generate a series starting at 1 and ending at the previous maximum value.
The index function (you can read more about it here) will distribute the elements of the sequence (with a "g" in front) across as many cells as elements are in the sequence.
If you combine those, you get:
=INDEX("g"&SEQUENCE(1,MAX(B:B)))

Can change shape of range with ARRAYFORMULA() in Google Sheets?

My intention is to convert a single line of data into rows consist of a specific number of columns in Google Sheets.
For example, starting with the raw data:
A
B
C
D
E
F
1
id1
attr1-1
attr2-1
id2
attr2-1
attr2-2
And the expected result is:
(by dividing columns by three)
A
B
C
1
id1
attr1-1
attr1-2
2
id2
attr2-1
attr2-2
I already know that it's possible a bit manually, like:
=ARRAYFORMULA({A1:C1;D1:F1})
But I have to start over with it every time the target range is moved OR the subset size needs to be changed (in the case above it was three)!
So I guess there will be a much more graceful way (i.e. formula does not require manual update) to do the same thing and suspect ARRAYFORMULA() is the key.
Any help will be appreciated!
I added a new sheet ("Erik Help") where I reduced your manually entered parameters from two to one (leaving only # of columns to be entered in A2).
The formula that reshapes the grid:
=ArrayFormula(IFERROR(VLOOKUP(SEQUENCE(ROUNDUP(COUNTA(7:7)/A2),A2),{SEQUENCE(COUNTA(7:7),1),FLATTEN(FILTER(7:7,7:7<>""))},2,FALSE)))
SEQUENCE is used to shape the grid according to whatever is entered in A2. Rows would be the count of items in Row 7 divided by the number in A2 (rounded to the nearest whole number); and the columns would just be whatever number is entered in A2.
Example: If there are 11 items in Row 7 and you want 4 columns, ROUNDUP(11/4)=3 rows to the SEQUENCE and your requested 4 columns.
Then, each of those numbers in the grid is VLOOKUP'ed in a virtual array consisting of a vertical SEQUENCE of ordered numbers matching the number of data pieces in Row 7 (in Column 1) and a FLATTENed (vertical) version of the Row-7 data pieces themselves (in Column 2). Matches are filled into the original SEQUENCE grid, while non-matches are left blank by IFERROR
Though it's a bit messy, managed to get it done thanks to SEQUENCE() function anyway.
It constructs a grid by accepting number of rows/columns input, and that was exactly I was looking for.
For reference set up a sheet with the sample data here:
https://docs.google.com/spreadsheets/d/1p972tYlsPvC6nM39qLNjYRZZWGZYsUnGaA7kXyfJ8F4/edit#gid=0
Use a custom formula
Although you already solved this. If you are doing this kind of thing a lot, it could be beneficial to look into Apps Script and custom formulas.
In this case you could use something like:
function transposeSingleRow(range, size) {
// initialize new range
let newRange = []
// initialize counter to keep track
let count = 0;
// start while loop to go through row (range[0])
while (count < range[0].length){
// add a slice of the original range to the new range
newRange.push(
range[0].slice(count, count + size)
);
// increment counter
count += size;
}
return newRange;
}
Which works like this:
The nice thing about the formula here is that you select the range, and then you put in a number to represent its throw, or how many elements make up a complete row. So if instead of 3 attributes you had 4, instead of calling:
=transposeSingleRow(A7:L7, 3)
you could do:
=transposeSingleRow(A7:L7, 4)
Additionally, if you want this conversion to be permanent and not dependent on formula recalculation. Making it in run fully in Apps Script without using formulas would be neccesary.
Reference
Apps Script
Custom Functions

Import multiple ranges to a single sheet

I have two different sheets, with two of the same ranges (age). I want to combine these two separate ranges into one on a different sheet. Current formula / function I am using:
={(importrange("https...", "Sheet1!A2:A100"));(importrange(""https...", "Sheet2!A2:A100"))}"))
What am I doing wrong?
I was able to bring in one range at a time with this formula / function:
=IMPORTRANGE("https...", "Sheet1!A2:A100")
=IMPORTRANGE("https...", "Sheet2!A2:A100")
but I need them to be in one column together (the order does not matter, I just need the values to be pulled across).
Try two IMPORTRANGE functions within one formula separated by a semi-colon and wrapped in braces (e.g. { and } that you type yourself)
={IMPORTRANGE("https://docs.google.com/spreadsheets/d/1mYWnO8vzyb5o4jzp-Ti-369nSyQoCfg-WzqaaTb94tE", "Sheet1!A2:A10");IMPORTRANGE("https://docs.google.com/spreadsheets/d/1mYWnO8vzyb5o4jzp-Ti-369nSyQoCfg-WzqaaTb94tE", "Sheet2!A2:A")}
If you do not have a set number of rows in the source sheet1 (e.g. A2:A100), then the retrieved data from sheet2 will start on the 101st row with blanks above it. To get around this, concatenate a dynamic 'last populated' row number onto the range string.
={IMPORTRANGE("https://docs.google.com/spreadsheets/d/1mYWnO8vzyb5o4jzp-Ti-369nSyQoCfg-WzqaaTb94tE", "Sheet1!A2:A"&match(1E+99, IMPORTRANGE("https://docs.google.com/spreadsheets/d/1mYWnO8vzyb5o4jzp-Ti-369nSyQoCfg-WzqaaTb94tE", "Sheet1!A:A")));IMPORTRANGE("https://docs.google.com/spreadsheets/d/1mYWnO8vzyb5o4jzp-Ti-369nSyQoCfg-WzqaaTb94tE", "Sheet2!A2:A")}
source link
destination link
What am I doing wrong?
You have a couple of double inverted commas too many and unmatched parentheses (also some unnecessary spaces and parentheses). Following should work, with granting authorisation if required.:
={importrange(" k e y 1 ","Sheet1!A2:A100");importrange(" k e y 2 ","Sheet2!A2:A100")}
It might help to compare 'yours' and 'mine' in a word processor and fixed width font.

Small in arrayformula (Google Spreadsheet)

I have 5 columns of numbers that I want to sort per row into another set of columns. I figured I need to use small() (e.g. small(a2:e2,1) for f2; small(a2:e2,2) for g2 and so on). Is there away to iterate this for the next rows; if possible using only native google spreadsheet formulas?
Thanks in advance
I was able to make a temporary work around, but I had to use 3 cheat columns. It looks ok for now but I imagine it will be troublesome for really huge numbers.
Here's a sample sheet for reference: https://docs.google.com/spreadsheets/d/1MQTP2XkRsPRAnPQ5wLhkR8JoNVY6YOExVlOkkX8UeRs/edit#gid=0
The original data are in A3:E
The first cheat column (G3:G) simply creates a column of numbers from 1 to the largest number found in the source data. 1-9 is changed to 01-09 for easier searching. "#" is then added at the end-this will come handy later:
Cheat Column 1 =filter(if(row(A:A)=max(A:E)+1,ʺ#ʺ,text(row(A:A),ʺ00ʺ)),row(A:A)<=max(A:E)+1)
The second cheat column (H3:H) combines each row into a string separated by "-" with a "#" marker:
Cheat Column 2=filter(text(A3:A,ʺ00ʺ)&ʺ-ʺ&text(B3:B,ʺ00ʺ)&ʺ-ʺ&text(C3:C,ʺ00ʺ)&ʺ-ʺ&text(D3:D,ʺ00ʺ)&ʺ-ʺ&text(E3:E,ʺ00ʺ)&ʺ#ʺ,A3:A<>ʺʺ)
The last cheat column (I3:I) sorts each line (from cheat column 2) by finding each number from cheat column from 01 up to the max number, then the "#" char (this ensures that each line will still have the # end marker). "Find" will return the "position" of each number or an error if it's not found. By using "if", we can make "find" return the actual number or "" instead.
=filter(arrayformula(if(iferror(find(transpose(filter(G3:G,G3:G<>ʺʺ)),H3:H),ʺʺ), transpose(filter(G3:G,G3:G<>ʺʺ)),ʺʺ)),A3:A<>ʺʺ)
The formula above creates as many columns as there are numbers from cheat column 1. To prevent this, a "-" is added to each number then "Concatenate" is used to combine everything into one massive string with each set separated by "#". The string is then split using the "#" marker.
Cheat Column 3 =transpose(split(concatenate(filter(arrayformula(if(iferror(find(transpose(filter(G3:G,G3:G<>ʺʺ)),H3:H),ʺʺ),ʺ-ʺ&transpose(filter(G3:G,G3:G<>ʺʺ)),ʺʺ)),A3:A<>ʺʺ)),ʺ#ʺ))
Each number is then separated into each corresponding column by using mid().
Small 1 =filter(mid(I3:I,2,2)*1,A3:A<>ʺʺ)
Small 2 =filter(mid(I3:I,5,2)*1,A3:A<>ʺʺ)
Small 3 =filter(mid(I3:I,8,2)*1,A3:A<>ʺʺ)
Small 4 =filter(mid(I3:I,11,2)*1,A3:A<>ʺʺ)
Small 5 =filter(mid(I3:I,14,2)*1,A3:A<>ʺʺ)
Note that the formula above is only for numbers 1-99. For larger numbers, the Text() formulas should have more zeroes to correspond to the number of digits of the biggest number. The Mid() formulas should also be adjusted accordingly.
I would like to stress that I am very far from being a spreadsheet expert and that this solution is very "unoptimized". It requires several cheat columns; with the first one even having more rows than the original data. If anyone can help me get rid of the cheat columns (or at least the first one) I will be very grateful.
How about using SMALL like you mentioned in your question?
=small($A3:$E3,column()-columns($A3:$G3))
You will need to change the ranges accordingly. The last $G$3 is the cell just before the cell where the formula is placed.
Sample

Resources