Google docs query when contains value in a range - google-query-language

In google sheets, I want to return a row when a cell in column C contains any value in a range on a different sheet.
The query below does not work. What am I missing?
=QUERY(
'Form Responses 1'!$1:$1000,
" select A:G where C contains 'Sheet2'!$A1:$A26"
)

If you are trying to match a string value in a range on a different sheet:
=query('Form Responses 1'!$1:$1000, "SELECT A,B,C,D,E,F,G WHERE A MATCHES '"&JOIN("|",'Sheet2'!$A1:$A26)&"'",1)
Hopefully syntax highlighting in the image below makes the above string concatenation operators a little clearer:
GoogleSheets: Select partial row if target column in data set contains value within range declared in another sheet

I suggest flagging the rows of interest in Form Responses 1, say with in H1:
=ArrayFormula(countif(Sheet2!A:A,C1:C))
and then selection based on the flags, say with:
=QUERY('Form Responses 1'!$1:$1000,"select * where H=1")
or if you don't want the flags to appear:
=QUERY('Form Responses 1'!$1:$1000,"select A,B,C,D,E,F,G where H=1")

Related

Google form to generate a unique value for each form submission

On Google form submission, I am trying to get a unique patient id in column A automatically whenever there is a response being submitted. You can also see the formula in the formula bar.
As of now, I am able to do it with the following google script but due to the trigger limit, it is not getting fulfilled as the COVID response is quite higher and requests are keep coming in.
function myFunction1() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var lr=sheet.getLastRow()
sheet.getRange(lr,1).setFormula('="PID"&row()-1');
}
If I add the formula to all cells in column A of this sheet, every time a new row is being inserted on form submission and the formula is NOT available for that row by default.
So, Is there a way that I can automate generating the PID automatically.? or any other smart ideas without the trigger option?
Alternative ARRAYFORMULA in cell A1:
=arrayformula({"Patient Unique ID #";if(B2:B<>"","PID"&row(A2:A)-1,iferror(1/0))})
"Patient Unique ID #" puts the title (Patient Unique ID #) in cell A1, then the ; returns the line.
If B is not empty then generate the unique ID as per "PID" and a unique number based on ROW() less 1. If B is empty, then nothing iferror(1/0).
In Google Sheets, this sort of thing is usually done with an array formula. I would recommend that you leave the 'Form Responses 1' sheet as is and insert the timestamps in a new sheet with something like this:
cell A1:
=arrayformula(
ifs(
row('Form Responses 1'!A1:A)) = 1, "Patient ID",
isnumber('Form Responses 1'!A1:A), row('Form Responses 1'!A1:A) - 1,
true, iferror(1/0)
)
)
cell B1:
={ 'Form Responses 1'!A1:Z }
If you absolutely want to insert the Patient ID column directly in the form responses sheet, you may want to place it on the right after the columns that get updated by the form.
An array formula is more reliable than using a function that runs on a trigger, but do note that the patient IDs will be dynamic and only remain valid as long as no rows are inserted or deleted in the data. The same is true with your current approach.
To get static IDs, use an on form submit trigger that writes an ID on a row when that row is first submitted. See the insertUniqueId_ script for one example — it runs on an on edit trigger, but can be modified to run on an on form submit trigger.

Match with concatenate across google sheets

I have this formula:
=IF(MATCH($A1&B$1,Sheet2!$A$1:$A$100&Sheet2!$B$1:$B$100,0), 1, 0)
I expect the formula in Sheet 1, Column C to produce all ones as in Excel but I only get a 1 when there is a row with a,b in Sheet 2:
https://docs.google.com/spreadsheets/d/1pyuWNNU7S07AC7d4zMvXft5pEz3AeAbeHcQp-wNsXpo/edit?usp=sharing
Why is this and is there a way around it that will also work in Excel?
Edit to clarify:
I want to be able to look up the value in two cells concatenated, e.g., $A1&B$1 and check if this matches any item in the list:
[Sheet2!$A$1&$B$1, Sheet2!$A$2&$B$2, Sheet2!$A$3&$B$3, ...]
Other details that may be important:
In my actual example, the columns I am looking at are not adjacent.
I would like the result to be compatible with as many spreadsheet programs as possible, especially Excel, Google Sheets and LibreOffice.
I will be writing the formula using openpyxl for Python.
Thanks, in advance.
I do not believe MATCH() is the appropriate function. There are lots of other options like vlookup(), index().
I like using the QUERY() function personally.
=QUERY(Sheet2!$A1:$B, "SELECT COUNT(A) WHERE A = '"&A1&"' AND B = '"&B1&"' LABEL COUNT(A) ''",0)
QUERY() shows #N/A when no results are found so we can wrap the above function accordingly
=IF(ISNA(query(Sheet2!$A1:$B, "SELECT COUNT(A) WHERE A = '"&A1&"' AND B = '"&B1&"' LABEL COUNT(A) ''",0)), "",query(Sheet2!$A1:$B, "SELECT COUNT(A) WHERE A = '"&A1&"' AND B = '"&B1&"' LABEL COUNT(A) ''",0))
the best way to use MATCH is making both parameters (search_key and range) expressed as brackets keys, the answer is:
=IF(MATCH({$A1}&{B$1},{Sheet2!$A$1:$A$100}&{Sheet2!$B$1:$B$100},0), 1, 0)
You MUST press Mayus + enter to asign the formula as an array

Google Sheets 'Text' function & Conditional Formatting

Is there any way to use the 'TEXT' function in the Google sheets Conditional Formatting sidebar?
For example, if ten rows need to be formatted as currency, based on a text description value in column A -- can a custom formula be used to execute the 'consequence' of an IF/THEN... if a TRUE condition exists?
Desired Format Rules/ Custom Formula (since 'number formatting' is not a viable 'Conditional Formatting option):
Apply to range 1:1
=IF(a1="REVENUE", TEXT(a3,"$#,##0.00"))
(the flag in a1 will dictate the row formatting)
Trying to AVOID having to iterate through all rows using code (such as the sample below) and instead check for the 'flag' column and apply the required formatting in the UI.
var ss = SpreadSheetApp.getActiveSpreadsheet().getSheetByName('sheet1');
var values = ss.getRange('a1:a10').getValues();
values.forEach(row=>{if (Range=="REVENUE") {ss.getRange(r,1).setNumberFormat("$#,##0.00")}}....

How to use custom function ExtractAllRegex() as an array formula? [Google Sheets]

I'm using #Wiktor Stribiżew 's custom function ExtractAllRegex(). The script extracts all occurrences of a Regex pattern. In the example, I extract all words in column A starting with "v_"
Here is a Google Sheet showing what I'm trying to do.
The original strings are stored in column A. The custom function/the matches are in column B.
Wictors function works great for single cells. It also works great when I manually drag the formula down the column.
Here's Wictor's original code:
function ExtractAllRegex(input, pattern,groupId,separator) {return Array.from(input.matchAll(new RegExp(pattern,'g')), x=>x[groupId]).join(separator);}
Description:
input - current cell value
pattern - regex pattern
groupId - Capturing group ID you want to extract
separator - text used to join the matched results.
The question is, how do I turn column B into a working array formula? Or, perhaps better, how do I modify Wictor's script so it accepts a range instead and auto-fills down column B?
I updated your script to:
function ExtractAllRegex(input, pattern,groupId,separator) {
return input.map ? input.map( inp => ExtractAllRegex(inp, pattern, groupId, separator)) :
Array.from(input.matchAll(new RegExp(pattern,'g')), x=>x[groupId]).join(separator);
}
and changed the formula in B2 to
=ExtractAllRegex(A2:A13,"(v_.+?\b)",0," ")
See if that works for you?

How to create INDIRECT array string of multiple sheet references in Google Sheets?

I am attempting to use a query to display data off multiple Google Sheets. I make a new sheet every week that has a specific sheet name, e.g. Week of 01/13, Week of 01/06 and so forth.
The following is where my idea spawned from for reference:
I have a summary sheet that is using COUNTA(INDIRECT("'" & A5 &
"'!E4:E",true)
A5 being a cell that concatenates a date and words to replicate the
sheet names.
The row on the summary sheet does not populate until B5<=today()
So I am able to set it an forget it and the sheet will continue to
give me my weekly data as the days progress and keeps the sheets clean
until the week is upon us.
Long story short, I have a query that I use that gives me all the data I need with a specific parameter but I have to manually update the data syntax array with the new sheet names each week.
=QUERY({'Week of 01/13'!A:P;'Week of 01/06'!A:P;'Week of 12/30'!A:P;'Week of 12/23'!A:P;'WEEK OF 12/16'!A:P;'WEEK OF 12/09'!A:P;'WEEK OF 12/02'!A:P;'WEEK OF 11/25'!A:P;'WEEK OF 11/18'!A:P;'WEEK OF 11/11'!A:P;'WEEK OF 11/04'!A:P;'WEEK OF 10/28'!A:P;'WEEK OF 10/21'!A:P;'WEEK OF 10/14'!A:P;'WEEK OF 10/07'!A:P;'WEEK OF 09/30'!A:P;'WEEK OF 09/23'!A:P;'WEEK OF 09/16'!A:P;'WEEK OF 09/09'!A:P;'WEEK OF 09/02'!A:P},
"Select * where Col11 = 'RD' order by Col2 desc",0)
I would like to build a reference to an array that will auto-populate a concatenation based on the day.
Using the following code I can have the concatenate give me the array I need,
=if(H4<=today(),CONCATENATE("'",H$1,text(H4,"mm/dd"),"'!A:P;",),"")
but when I try to input it into the query function it just returns the concatenated text:
=QUERY(I1,"Select *")
'Week of 01/06'!A:P;'Week of 01/13'!A:P
I have tried with and without the curly brackets with no success.
I would like the sheet to be able to refresh and see that it is the correct day, the new sheet name is populated and the query gets updated.
I need help with making I1 work.
Link to Test Query Sheet
dudes who copy-pasted INDIRECT function into Google Sheets completely failed to understand the potential of it and therefore they made zero effort to improve upon it and cover the obvious logic which is crucial in this age of arrays.
in other words, INDIRECT can't intake more than one array:
=INDIRECT("Sheet1!A:B"; "Sheet2!A:B")
nor convert an arrayed string into active reference, which means that any attempt of concatenation is also futile:
=INDIRECT(MasterSheet!A1:A10)
————————————————————————————————————————————————————————————————————————————————————
=INDIRECT("{Sheet1!A:B; Sheet2!A:B}")
————————————————————————————————————————————————————————————————————————————————————
={INDIRECT("Sheet1!A:B"; "Sheet2!A:B")}
————————————————————————————————————————————————————————————————————————————————————
=INDIRECT("{INDIRECT("Sheet1!A:B"); INDIRECT("Sheet2!A:B")}")
the only possible way is to use INDIRECT for each end every range like:
={INDIRECT("Sheet1!A:B"); INDIRECT("Sheet2!A:B")}
which means that the best you can do is to pre-program your array like this if only part of the sheets/tabs is existant (let's have a scenario where only 2 sheets are created from a total of 4):
=QUERY(
{IFERROR(INDIRECT("Sheet1!A1:B5"), {"",""});
IFERROR(INDIRECT("Sheet2!A1:B5"), {"",""});
IFERROR(INDIRECT("Sheet3!A1:B5"), {"",""});
IFERROR(INDIRECT("Sheet4!A1:B5"), {"",""})},
"where Col1 is not null", 0)
so, even if sheet names are predictable (which not always are) to pre-program 100+ sheets like this would be painful (even if there are various sneaky ways how to write such formula under 30 seconds)
an alternative would be to use a script to convert string and inject it as the formula
A1 would be formula that treates a string that looks like real formula:
=ARRAYFORMULA("=QUERY({"&TEXTJOIN("; ", 1,
IF(A3:A<>"", "'Week of "&LEFT(A3:A, 5)&"'!A1:D5", ))&
"}, ""where Col1 is not null"", 1)")
further populating of A6:A will expand the string automatically
then this script will take the string from A1 cell and it will paste it as valid formula into C5 cell:
function onEdit() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Master Sheet');
var src = sheet.getRange("A1");
var str = src.getValue();
var cell = sheet.getRange("C5");
cell.setFormula(str);
}
of course, the script can be changed to onOpen trigger or with custom name triggered from the custom menu or via button (however it's not possible to use the custom function as formula directly)
If you're trying to update the data your query is looking at and you're feeding it a string, you need to put that string within the indirect() function. That will interpret your string as a data reference and point your query() in the right direction.
So for this you'd probably have
=QUERY(INDIRECT(I1),"Select *")