Is there an SQL-like feature in Google Apps Script? - google-sheets

Is it possible to connect to Google Spreadsheet in GAS using SQL-type queries? If so, any working samples?
Thanks.

Depending on what you want to do, there are two options.
1) You can have a SQL Database and connect to it using the Jdbc Service ( https://developers.google.com/apps-script/service_jdbc ).
2) The other option, which I've used once earlier, is by making use of the QUERY() function. You can set the formula on a cell to the SQL like query and then read the subsequent cells.
( https://support.google.com/docs/bin/answer.py?hl=en&answer=1388882 ).
Update after Google I/O 2012:
As you might have already noticed, Google possibly heard you and introduced ScriptDB which is better than the two options mentioned

You can also used the "Structured Query" parameters available in the Spreadsheets API to make calls like: &sq=filter:7 where filter is the name of your column and you want to return the results of any row where the value in that column is 7. See the List Feed section of the Spreadsheet API.

Related

Google Sheets: Parsing users who are Program Managers

I have a spreadsheet that is always updating with 50+ rows. I am trying to retrieve users who are Program Managers (PGM) by parsing text but I am having a hard time since the data is not consistent since it's filled out by 20+ users.
I googled "google sheet parse text" but it's giving me functions such as =SEARCH, =LENS, =LEFT which I cannot use since my data is not consistent. Are there any other options or am I out of luck and must parse my info manually? Thanks in advance.
Google Sheet Link Example
in C2 use:
=ARRAYFORMULA(IFERROR(REGEXEXTRACT(B2:B,"PGM:.*")))
You may try:
=byrow(B2:B,lambda(z,if(z="",,ifna(regexextract(z,"(PGM:.*)(?:\n|$)")))))
row 6 outcome is varying a bit

How to filter with user-input data? [Google Sheets]

I have a question about dynamic filtering.
Suppose I have a number of services and their providers, and I'm trying to filter by a particular criteria. Easy enough.
But suppose that I wasn't the one generating the criteria? How do I code a filter to show results when I don't know what column we're checking again?
See my spreadsheet (permissions open) for more explanation
https://docs.google.com/spreadsheets/d/1Bj_OGyCyobXmoFq72M-v4lcNiGfrgi1AX5QeeZIi-FY/edit?usp=sharing
Here's how I would approach this
=filter(B4:B6,filter(C4:D6,C3:D3=B22)="Y")
Or you can do it by unpovoting the data and running a query()
=ArrayFormula(query(split(flatten(B4:B6&"❄️"&C3:D3&"❄️"&C4:D6),"❄️"),"select Col1 where Col2='"&B22&"' and Col3='Y'"))
Another way to dynamically detect the column/row is with indirect(R1C1)-match
=filter(B4:B6,indirect("R4C"&match(B22,3:3,0)&":R6C"&match(B22,3:3,0),0)="Y")

Google data studio - Use multiple datasheet with same data keys/headers

So I've been stuck in this for some days, tryed a lot of search terms but all of them seems to bring me the same answers and i really need this:
I have a demand to join two different company's datas from the same owner, all of them have the same data sources (excel data sheets from FB ADS).
So they all share the same (keys/headers), like this:
COMPANY(1)'S ADS DATA
COMPANY(2)'S ADS DATA
So this way I need to put then togheter without having to join both of then on excel every time and also give him some nice data manipulation power.
The results should be something like this
By now I was trying to join data from the two companys but I couldn't really figure out how to properly do this so far I've made some tests and tryed reading a couple of articles and google data studio's help files. The merging data function seems to mess everything.
As a result of this merge, GDS gives me this fields:
Shouldn't I see like only one field labeled as cnt and cmp? I've noticed that GDS creates not one, but two data fields. If I try adding all data I need as key the left sheet turns all "0s". What Am I doing wrong here?
I have read your descriptions. It seems that you are looking for a solution to append both tables instead of merging the tables.
Do note that the data blending in GDS is a left outer join.
Hence, instead of doing the blending in GDS, I'd suggest you to append both datasets in Google Sheet in a separate tab before importing to GDS for visualisation. (assuming you don't mind copy-pasting the data into the Google Sheet).
Here is the formula to append both datasets in Google Sheets:
= {QUERY(A!A1:D1000,"SELECT A,B,C,D WHERE A <> ''",1);QUERY(B!A2:D, "SELECT A,B,C,D WHERE A <> '' ")}
I've created some dummy data in this google sheets and appended the data using the formula provided , you may take a look to understand further.
If you are unclear on the difference between merge and append, you may take a look in the Google Sheet documentation as well.
On a side note, I've screencast the process of answering this question and posted on my youtube channel. You may take a look if needed. (Thanks for the question and inspiration you provided for the video)

Error when applying multiple filters in Google Analytics Spreadsheet Add-on

I am trying to apply 2 filters with Google Analytics Spreadsheet Addon. I want to find the organic session in the specific pagePath. I used semicolon between the 2 filters
="ga:pagePath"&Report!$P9&Report!$G9;ga:medium==organic.
But after entering, I got Formula parse error. return. I couldn't figure out why it doesn't work.
You may try using , instead of ; as seen in this example. Also, from this documentation, to add more than one filter, you need to first think about how you want to filter the data. It's either be an and(;) or an or(,).

Union clause in Google Visualization Query Language

I need to create a single datatable pulling datas from different sheets of a Google Spreadsheet, is there a way to emulate in Google Visualization API Language the union clause provided by SQL?
Google Query Language documentation shows there's no union avaliable and actually this sounds reasonable because changing sheet the part affected is the address (different gid) not the query statement.
One way to emulate the SQL UNION clause is to append the different source ranges by using the array handling features of Google Sheets.
Example:
Assume that there are two ranges to put together as a single array, Sheet1!A1:B10 and Sheet2!A5:B25. Then write the following formula in the cell A1 of new sheet
={Sheet1!A1:B10;Sheet2!A5:B25}
For further details checkout Using arrays in Google Sheets

Resources