Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the google spreadsheet 'query' function be used in google apps script?

Tags:

I'm looking for a way to programmatically populate a spreadsheet that filters data from another spreadsheet based on the logged in user.

I am able to do this using the query function inside a spreadsheet. BUT, unable to figure out a way to call the query function from apps script?

Can this be done? Would appreciate sample code. Thanks.

like image 977
awsamar Avatar asked Jul 29 '13 17:07

awsamar


People also ask

How do you access a Google spreadsheet from the App script?

On the menu bar, select Tools > Script editor to open the browser code editor for Apps Script. In a new browser tab, the code editor shows the default container-bound script for the sheet.


2 Answers

I do not know whether there is a restriction on that ...

function test () {   var req = query("=QUERY(shopT!B2:E; \"select min(E) where (B=3 or B=4 and D=2) group by C, D\")");    Logger.log(req); }  function query(request) {    var sheet = sp.insertSheet();   var r = sheet.getRange(1, 1).setFormula(request);    var reply = sheet.getDataRange().getValues();   sp.deleteSheet(sheet);    return reply; } 
like image 100
zizix Avatar answered Oct 27 '22 09:10

zizix


No, there is no API for the Query function that allows it to be called from Google Apps Script. (There is no way to call ANY spreadsheet function in this way, in fact.)

You can get some similar functionality without writing it all yourself, though. The 2D Arrays Library includes a variety of "filter" functions that let you retrieve matching rows.

like image 38
Mogsdad Avatar answered Oct 27 '22 09:10

Mogsdad