Random Cells From A List In Google Sheets - google-sheets

Sample
Code:
=ArrayFormula((VLOOKUP(QUERY(UNIQUE(RANDBETWEEN(ROW(INDIRECT("A1:A"&COUNTA(A:A)*10))^0;COUNTA(A:A)));"limit 4");{ROW(INDIRECT("A1:A"&COUNTA(A:A)));FILTER(A:A;A:A<>"")};2;0)))
I'm trying but got this error,
test 1
Can anyone tell what's wrong? To make my code
to work as in the first picture
error in #REF!
VLOOKUP evaluates outside the range bounds.
im try change code
=ArrayFormula((VLOOKUP(QUERY(UNIQUE(RANDBETWEEN(ROW(INDIRECT("A1:A"&COUNTA(A:A)*10))^0,COUNTA(A:A))),"limit 4"),{ROW(INDIRECT("A1:A"&COUNTA(A:A)));FILTER(A:A,A:A<>"")},1,0)))
but number
solution for that?

=ARRAYFORMULA(VLOOKUP(FLOOR(RANDARRAY(5)*COUNTA(A2:A)),{SEQUENCE(COUNTA(A2:A),1,0),A2:INDEX(A2:A,COUNTA(A2:A))},2))
Create 5 random integers using RANDARRAY between 0(inclusive) and number of entries in A2:A(i.e., COUNTA)(exclusive).
Create a artificial side by side array({arr1,arr2}) of SEQUENCE of numbers (from 0) and actual values in A2:A
VLOOKUP the random integers in the created artificial array to give random values in A:A
If you're in locales that use comma as decimal separators, The artificial array should be created using \ instead of ,({arr1\arr2}).
=ARRAYFORMULA(VLOOKUP(FLOOR(RANDARRAY(5)*COUNTA(A2:A));{SEQUENCE(COUNTA(A2:A);1;0)\A2:INDEX(A2:A;COUNTA(A2:A))};2))

On the first formula
{ROW(INDIRECT("A1:A"&COUNTA(A:A)));FILTER(A:A;A:A<>"")}
replace the semicolon ; between INDIRECT() and FILTER() by a backslash \ as using a semicolon appends the results of FILTER to the results of INDIRECT but you are looking to put the results of each function on their own column. Please note that this formula is using semicolons as argument separator.
On the second formula replace the semicolon ; between INDIRECT() and FILTER() by a comma , (and replace the third argument of VLOOKUP, 1, by 2. Please note that this formula is using commas as argument separators.
Explanation
Commas are used as argument separator on spreadsheets that use dot as decimal separator (=SUM(1,2,3)) but also use commas as columns separator on arrays ({"a","b"})
Semicolons are used as argument separator on spreadsheets that use comma as decimal separator (=SUM(1;2;3)). On these spreadsheets, backslashes are used as columns separator on arrays ({"a"\"b"});
References
Using arrays in Google Sheets

Related

ArrayFormula with TextJoin of a row of columns

I have a table as follows:
I would like to use the following formula =SPLIT(TEXTJOIN(",",TRUE, Data!B2:C2),",") with ArrayFormula, but I am not sure how I should adapt B2:C2 to work with the function.
Expected output (result of dragging formula down):
Use the Ampersand Operator (&) Instead
The function TEXTJOIN() does not work with ARRAYFORMULA(). Instead, you may use the Ampersand Operator (&) expression to join the texts together. In your case, you may use the following:
=ARRAYFORMULA(split(arrayformula(B2:B7 & "," & C2:C7),","))
Sample Output
Based from the output below, your desired result was met.

Extract dollar amount in sheets text cell

Ideally what I'm looking for is to get the dollar amount extracted no matter the format.
Sheet link:
https://docs.google.com/spreadsheets/d/1drTPlnQmVTsbUXwJDfQr7DnHjSbnGx-fLthad6KxfM8/edit?usp=sharing
Delete everything from Column B, including the header. Then place the following formula in cell B1:
=ArrayFormula({"Header"; IF(A2:A="",,VALUE(IFERROR(REGEXEXTRACT(A2:A,"\$(\d+\.?\d*)"))))})
You may change the header text within the formula as you like.
If a cell in A2:A is blank, the corresponding cell in B2:B will be left blank as well.
Otherwise REGEXEXTRACT will look for a pattern that begins with a literal dollar sign. The parenthesis within the quotes denote the beginning and end of a capture group (i.e., what will be returned if found) following that literal dollar sign. The pattern \d+\.?\d* means "a group of one or more digits, followed by zero or one literal period symbols, followed by zero or more digits."
IFERROR will cause null to be rendered instead of an error if such a pattern is not able to be extracted.
VALUE will convert the extracted string (or null) to a real number.
If you would prefer that null be returned instead of 0 where no pattern match is found, you can use the following variation of the formula instead:
=ArrayFormula({"Header"; IFERROR(VALUE(IFERROR(REGEXEXTRACT(A2:A,"\$(\d+\.?\d*)"),"x")))})
If your strings may include numbers with comma separators, use the following versions of the above two formulas, respectively:
=ArrayFormula({"Header V1"; IF(A2:A="",,VALUE(IFERROR(REGEXEXTRACT(SUBSTITUTE(A2:A,",",""),"\$(\d+\.?\d*)"))))})
=ArrayFormula({"Header V2"; IFERROR(VALUE(IFERROR(REGEXEXTRACT(SUBSTITUTE(A2:A,",",""),"\$(\d+\.?\d*)"),"x")))})
try:
=INDEX(IFNA(REGEXEXTRACT(A2:A, "\$(\d+.\d+|\d+)")*1))

Using multiple SUBSTITUTE functions dynamically

Let's say I have a list of strings and I want to remove specific words from them. I can easily use multiple SUBSTITUTE functions, for example, this will remove the strings in B2, B3 and B4 from the string in A2:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,$B$2,""),$B$3,""),$B$4,"")
How can I make this dynamic so that when I add more terms to remove in the B column they'll be removed automatically from A2. I tried the following methods but they didn't work:
1 - add the B cells as an array
=SUBSTITUTE(A2,{$B$2:$B$4},"") or =SUBSTITUTE(A2,{$B$2,$B$3,$B$4},"")
2 - Make a single condition
cat|donkey|mouse
3 - Using Indirect and concatenate - I built the correct function as a string (using REPT and CONCATENATE) and tried to activate it with INDIRECT) but this also failed.
Here's the spreadsheet (Col A are the strings to clea, B are the words to remove, D is the manual method that works, F, H and K are the failed 3 attempts).
https://docs.google.com/spreadsheets/d/15u8qZ0xQkjvTRrJca6AInoQ4aPkijccouAETE4Gyr9I/edit#gid=0
In the 'Copy' of the tab I entered
=ArrayFormula(IF(LEN(A2:A), REGEXREPLACE(A2:A, TEXTJOIN("|", 1, B2:B),),))
See if that works for you?
EXPLANTION
LEN(A2:A) basically limits the output to the rows that a value in column A
REGEXREPLACE uses a regular expression to replace parts of the string. That regular expression is constructed by the TEXTJOIN function.
TEXTJOIN combines the text from the range B2:B, with a specifiable delimiter separating the different texts. Here the pipe character (which means 'or' in regex) is used. The second paramater of this function is set to TRUE (or 1) so that empty cells selected in the text arguments won't be included in the result.
REFERENCES
TEXTJOIN
REGEXREPLACE
You can also try-
=TEXTJOIN(" ",TRUE,FILTER(SPLIT(A2," "),ISERROR(MATCH(SPLIT(A2," "),$B$2:$B$7,0))))

Combine Data From Separate Columns with a Delimiter with Arrayformula

This is the example sheet.
Alright, in cell V1!A1 is the formula ={"Languages";ARRAYFORMULA(TRANSPOSE(QUERY(TRANSPOSE(B2:F&","),,COLUMNS(B2:F))))}. I need to combine data from B2:F with the delimiter ,. But now I need to delete the unnecessary delimiters.
In sheet V2, I tried ={"Languages";ARRAYFORMULA(REGEXREPLACE(REGEXREPLACE(TRANSPOSE(QUERY(TRANSPOSE(B2:F&","),,COLUMNS(B2:F))),"(^(,(\s,){4})$)|(^(,\s)+)|(,(\s,)?\s?$)",""),"(,\s,)+\s?",", "))} but it's not consistant and still leaves delimiters in the output.
Is there a better way to do this?
I added a sheet called "Erik Help" which replaces your formula with the following:
=ARRAYFORMULA({"Languages";SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(B2:F&" "),,COLUMNS(B2:F))))," ",", ")})
Essentially, instead of appending a comma to elements of the range B2:F, I appended a space. Then I applied TRIM to the results, which will leave no spaces before or after each concatenation and only one between each element. To that, I applied a SUBSTITUTE of the single spaces with a comma-space.

Google sheets arrayformula and ranges rules

I was looking a the formula for cumulative sum using arrayformula:
ARRAYFORMULA(SUMIF(ROW(A1:A10), "<=" & ROW(A1:A10), B1:B10))
I used arrayformulas before, but mostly for simple arithmetic, so I am trying to wrap my head around this.
The reason it is unclear for me is that I don't know what the rules are for ARRAYFORMULA to use the range, or to use the scalar value.
If I tabulate it it becomes something like this:
row 1: SUMIF(ROW(A1:A10), "<=" & ROW(A1), B1:B10))
row 2: SUMIF(ROW(A1:A10), "<=" & ROW(A2), B1:B10))
...
row n: SUMIF(ROW(A1:A10), "<=" & ROW(An), B1:B10))
But that doesn't actually work because ROW doesn't work like that if not used in an ARRAYFORMULA, according to the docs:
ROW([cell_reference])
if cell_reference is a range more than one cell wide and the formula is not used as an array formula, only the numeric value of the first row in cell_reference is returned.
Looking at the syntax for SUMIF it is:
SUMIF(range, criterion, [sumrange]).
Do I understand it correctly that the rules for ARRAYFORMULA are:
If a function parameter inside ARRAYFORMULA is expected to be a scalar (criterion in the examle) and a range is given it will expand in the cells below iterating this range
If a function parameter inside ARRAYFORMULA is expected to be a range (range and sumrange) and a range is given it will pass on the range to the formula
So, the question is:
What is exactly happening row by row with the formula pasted above and how does ROW behave in this? ROW is 'aware' that it is being used in an arrayformula and behaves differently?
It's not about the ROW. Its more about the & and the strings used. Used inside a arrayformula, it creates series(a array) of strings. So, "<="&ROW(A1:A10) becomes "<1","<2" and so on. Criterion argument of SUMIF expects a string and only a single string. Given the array, it creates a array of results for each criterion, the Range argument being the same for each criterion. Also, SUMIF already expects array for the range argument.

Resources