I do gave a google spreadsheet №1 and spreadsheet №2. I need get data from spreadsheet №1 and filter out it and then put in spreadsheet №2. I do it without Google Scripts with help function Query.
Formula of query:
= query( main_data_sheet ;"select B, D, H, count(I)
where (O is not null and O > date '"&TEXT($B$5;"yyyy-mm-dd")&"'
and O < date '"&TEXT($B$6;"yyyy-mm-dd") &"') and (U = '"&$A$3&"') and
(R = '"&$B$8&"' or R = '"&$B$9&"' or R = '"&$B$10&"' or R = '"&$B$11&"' or R = '"&$B$12&"')
group by B,D,H label B '', D '', H '', count(I) '' ";0)
and this is work great. But i need to build this request by Google Scripts.
So i tried this function So my Code is
function test_gvizQuery() {
var result = Utils.gvizQuery( "ID_spreadsheet", "SELECT A,B,O WHERE O > date '01-01-2019' format O 'dd-MMM-yyyy' " , "РЕЕСТР2", 0);
Logger.log("result = " + result);
var sheet_2 = SpreadsheetApp.getActive().getSheetByName("Using_the_Query_Language");
sheet_2.getRange(1, 1, 1987, 3).setValues(result);
}
About using this function
Just drop that module into its own file in your GAS editor then you can call it as follows:
// result is an array of objects if header row is specified, otherwise it is an array of arrays var result = Utils.gvizQuery( "[YOUR_SPREADSHEET_ID]", "[YOUR_QUERY_STRING]", [SHEET_ID_IF_NEEDED], // can be a number (the sheetId), or the name of the sheet; if not needed, but headers are, pass in undefined [HEADER_ROW_INDEX_IF_NEEDED] // always a number );
so i did the same. when i use
"SELECT A,B,O WHERE O > date '01-01-2019' format O 'dd-MMM-yyyy' "
compiller said
TypeError:Cannot read property "rows" object undefined. (row 53, file qvizQuery)
and row 53 is
rows = table.rows.map(function(row) {
Please help me to solve this problem.
Problem was solve! Thanks @Dimu Designs. So everybody who want use this method please open my googleSheet and make COPY and you will be able to learn how script work. if spreadsheet is not available code of solve below
function testQuery() {
var ssId = SpreadsheetApp.getActive().getId();
//var ssId = SpreadsheetApp.openById("ID"); // external spreadsheet ID
var query = "SELECT A, B, O WHERE (O >= date '2020-01-01') AND (O < date '2020-02-01')";
//var query = "SELECT A, B, O WHERE O IS NOT NULL";
var range = "A3:O";
var sheetName = "SOURCE";
//var sheetName = "main_data";
var rows = Utils.gvizQuery(
ssId,
query,
sheetName,
range
);
var result = rows.map(function(row) {
var [projectId, block, planEnd] = row; // destructuring assignment
return [projectId, block, eval("new "+ planEnd)]; // return [projectId, block, eval(planEnd)];
});
Logger.log(result);
var sheet = SpreadsheetApp.getActive().getSheetByName("qvizQuery");
var maxRow = sheet.getMaxRows();
sheet.getRange(1,1,maxRow, 3).clearContent();
//removeEmptyRows(sheet);
sheet.getRange(1, 1, result.length , 3).setValues(result);
sheet.getRange(1,5).setFormula("QUERY({SOURCE!A:Z};\"select Col1,Col2,Col15 where (Col15>=date '2020-01-01') and (Col15 < date '2020-02-01') \";0)");