Dealing with multiple strings and corresponding integers in a single cell - google-sheets

I’ve got a question about something I’m trying to achieve in Google spreadsheets. I’ve got the following table:
| NAME | COUNTRY |
--------------------------------------------------
| Alpha | GER*1, SWE*3 |
--------------------------------------------------
| Beta | GER*5, SWE*1 |
--------------------------------------------------
| Gamma | SWE*5, GER*3 |
--------------------------------------------------
| Delta | SWE*2, GER*1 |
--------------------------------------------------
Now I’d like to be able to calculate how many SWE there are in line Gamma. I’d need spreadsheets to return an integer value, in my example the correct one would be 5.
However, I’d also like to be able to calculate the total number of GER in line Alpha to Delta, also returned as an integer value, in this case the result should be 1+5+3+1 = 10.
As you can see I’m dealing with both strings and integers in a single cell. If it would be possible to for example create an array of all GER*(INTEGER), then delete the GER* and have an array of only the integers that might help me?
I’ve searched here, googled around and fiddled with spreadsheets to try and come to a solution but either I’m too daft or it’s not as trivial as I thought it’d be. Any help would be much appreciated.

To calculate the total number of GER in line Alpha to Delta (assuming your data extends from row 2 to row 5) , try:
=sum(ArrayFormula(iferror(regexextract(B2:B5, "GER\*(\d+)")+0,0)))
To calculate how many SWE there are in line Gamma, try:
=sum(ArrayFormula(iferror(regexextract(filter(B2:B6, A2:A6="Gamma"), "SWE\*(\d+)")+0,0)))
Change the ranges so that they suit your actual data range and see if that works ?

You could use one of the Text functions, like FIND together with MID. REGEXEXTRACT is even more powerful but harder to use. When you have the substring, use VALUE to get the numeric value from it.
For the summary, the easiest is probably to create an extra column, extract the GER value into it as above, and SUM that column.
Here's the function reference for Google Spreadsheets: https://support.google.com/docs/table/25273?hl=en

Related

Is there a way to remove 2 letters from a cell with a string (if they exist), and add a symbol on the end of the string?

I'm trying to code a google sheet to help develop a system to keep track of what training different people have. This is for an online role-playing game, to keep track of what spells/abilities different people have learnt. We've assigned different trainings a letter in the alphabet for ease of use. When someone has multiple trainings under a certain category, I want these to be able to combine to give a symbol rather than a letter. For example, if "A" and "T" are both related, I want them to be able to combine to give the "#" symbol.
It's a strange concept, I know, but this is the way our system has been built. I want to be able to have all people in the tracker sheet, then on a separate sheet, I want to be able to type their name in, and it will spit out their compressed training history.
The sheet's columns are formatted like so:
1. | A | B | C | D | E | F |... (Column)
2. |Name | Position | A | B | C | D |... (Header)
3. |John | Wizard | T | F | T | T |... (Data)
I've set up the main sheet, so that when I type their name, it uses the formula:
=JOIN("",
ARRAYFORMULA(
IF(
INDEX(Tracker!C:AB,MATCH(A2,Tracker!A:A,0),Tracker!C:AB),Tracker!C1:AB1,
)))
I've tried using regex extract/replace, however I'm not too good with regex, and I only want to replace characters with a symbol if BOTH exist, otherwise just leave it.
https://docs.google.com/spreadsheets/d/1cYGgmFJGGMtmwy6LF5k91H3-6UBVYIH_n8TIn1TU1Cw/edit?usp=sharing
Up there is a link to a sheet I just made, showing what I want to do. If anyone has suggestions, or questions, feel free to leave them here, or comment it on the spreadsheet.
Thank you in advance for any responses.
You may try below formula-
=REGEXREPLACE(JOIN("",FILTER(Tracker!C1:AB1,INDEX(Tracker!C2:AB,MATCH(A2,Tracker!A2:A,0)))),"[AT]","")&"#"
Here INDEX()/MATCH() will detect and return row based on name.
FILTER() will return characters based on selection of checkboxes of person row.
JOIN() will concat all characters in a single string.
Finally REGEXREPLACE(#,"[AT]","") will remove A & T.

Google Sheets: extract one column of a named range

update
My original question was about taking the average of a single column of a named range. But upon reflection, the question generalizes to "how do I extract a single column from a named range?"
original question
I'm using a named range to extract two columns from a large dataset. Assume MyRange defines three rows and two columns out of a larger dataset:
... |G |H | ...
+----|----|----|
| ... |
|2000|15.1| 10|
|2001|15.2| 23|
|2002|15.3| 30|
| ... |
Using MyRange, how would I take the average of (only) column H?
I've tried various things like AVERAGE(MyRange:H), and AVERAGE(INDEX(MyRange 1)) but haven't figured out the proper syntax.
(In the above example, the result should be 21.)
Or there's the tried and trusted way with index:
=average(index(NamedRange1,0,2))
(specifying 0 for the row means use the entire column)
I just stumbled upon a simpler approach using FILTER():
=AVERAGE(FILTER(MyRange, {FALSE, TRUE}))
It works because FILTER(MyRange, {FALSE, TRUE}) extracts (only) the second column, which is in turn passed to AVERAGE().
Following the now updated question:
My original question was about taking the average of a single column of a named range. But upon reflection, the question generalizes to "how do I extract a single column from a named range?"
One could also use:
=AVERAGE(OFFSET(MyRange,0,2))
About OFFSET
You could use a query:
=Query(ArrayFormula(MyRange), "select avg(H) label avg(H) ''")
As an alternative.
Why not just create a new named range from the 3 rows in column He.g. MyRangeH?
Then, use:
=AVERAGE(MyRangeH)

Google Sheets Function to get value of adjacent cell based on key

I need some help figuring out some Google Sheets function. I'm trying to look for the value of an adjacent cell based on a key. The problem is that the table has several rows and columns to search for. For example:
| A | B | C | D |
1 | Alpha | 5 | Bravo | 10 |
2 | Charlie | 15 | Delta | 20 |
The list goes on for several more rows and colums. What I'm looking for for example, is when a search use "Delta" as my search key, it will return the value 20 for me. I appreciate your help. Thanks!
Here is the answer I got on the web application community:
https://webapps.stackexchange.com/questions/90198/how-to-get-value-of-adjacent-cell-based-on-key-if-the-table-is-split-into-multi
You can still use vlookup for this, by stacking the lookup tables into one using the array notation {first ; second; third} which means first above second above third. Example:
=vlookup("Delta", {A:B; C:D}, 2, False)
returns 20.
Try:
=offset(lookup("Delta",A1:D2),0,1)
You could use the following:
=ArrayFormula(INDEX($A$1:$D$2,MIN(IF($A$1:$D$2=$G$1,ROW($A$1:$D$2))),MIN(IF($A$1:$D$2=$G$1,COLUMN($A$1:$D$2)))+1))
This could by done in several ways.
Use RegEx
=regexextract(CONCATENATE(A1:D),F6 & "(\d+)")
where F6 is cell with lookup value i.e. "Delta"
Use lookup
=INDEX(A:D,MATCH(LOOKUP(F6,A1:D,A1:A),A1:A),MATCH(F6,INDIRECT(MATCH(LOOKUP(F6,A1:D,A1:A),A1:A)&":"&MATCH(LOOKUP(F6,A1:D,A1:A),A1:A)))+1)
where F6 is cell with lookup value i.e. "Delta", look at example.
MATCH(LOOKUP(F6,A1:D,A1:A),A1:A) is used 3 times, so it could be counted in separate cell.

Performing lookups with merged cells in Google Spreadsheets

Given a set of multi-dimensional data represented in a Google Spreadsheet:
A | 1 | x
A | 2 | y
A | 3 | z
It is pretty straightforward to do a lookup on multiple values using the FILTER and INDEX functions. Doing a lookup for A2 would result in y. However, if the similar data is merged into a single cell:
| 1 | x
A | 2 | y
| 3 | z
Is there a function that would accomplish the same thing? the FILTER method only returns the first row in the implicit set because there is only an A actually on the first row.
If interested, you can view the live sheet here using the 'Rules' sheet.
I have been working on a custom function, but am hoping for something more portable. Helper cells would be fine, as long as I can hide them on a separate sheet.
One option, using the helper cells idea, would be to "normalise" your Rules sheet on a separate (hidden) sheet, and perform lookups on that sheet. Eg, in A3 of that sheet:
=ArrayFormula(HLOOKUP(IF(ROW(Rules!A3:A),Rules!A2),Rules!A2:A,VLOOKUP(ROW(Rules!A3:A),FILTER(ROW(Rules!A3:A),LEN(Rules!A3:A)),1)-1,0))
and fill that formula across to the right as far as required. Note: this formula will only work on the new version of Sheets (which you are using).

Is there a multiple-and-add formula in Google's spreadsheet?

What I want is to easily multiply a number by another number for each column and add them up at the end in Google Sheets. For example:
User | Points 1 | Points 2 | Points 3 | Total
| 5 | 1 | 4 |
-----+----------+----------+----------+------
Jane | 2 | 3 | 0 | 13 (2*5 + 3*1 + 0*4)
John | 1 | 11 | 4 | 32 (1*5 + 11*1 + 4*4)
So it's easy enough to make this formula for the total:
= B3*$B$2 + C3*$C$2 + D3*$D$2
The problem is I frequently need to insert additional columns or even remove some columns. So then I have to mess with all the formulas. It's a pain... we have many spreadsheets with these formulas. I wish there was a formula like SUM(B3:D3) where I could just specify a range. Is there anything like MULTIPLY_AND_SUM(B2:D2, B3:D3) that would do this? Then I could insert columns in the middle and the range would still work.
There is a built in function in Google Sheets that does exactly what you are looking for: SUMPRODUCT.
In your example the formula would be:
=sumproduct(B$2:D$2,B3:D3)
Click here for more information about this function.
You can accomplish that without requiring a special-purpose function.
In E3, try this (and copy it to the rest of your rows):
=sum(arrayformula(B3:D3*B$2:D$2))
You can read about arrayformula here.
As long as you introduce new columns between B and D, this formula will automatically adjust. If you add new columns outside of that range, you'll need to edit (and cut & paste).
On it's own, arrayformula(B3:D3*B$2:D$2) operates over each value in B3:D3 in turn, multiplying it by the corresponding value in B$2:D$2. (Note the use of absolute references to 'lock down' to row 2.) The result in this case is three values, [10,3,0], arranged horizontally in three rows because that matches the dimensions of the ranges.
The enveloping sum() function adds up the values of the array produced by arrayformula, which is 13 in this case.
As you copy that formula to other rows, the relative range references get updated for the new row.

Resources