I am trying to build a Google Apps Script web app that will pull data from a Google sheet and display it in rows and columns in an HTML page in the browser.
By following the samples etc I wrote this code that WORKS!
function doGet(){
return HtmlService
.createTemplateFromFile('Report3')
.evaluate();
}
function getData(){
var spreadsheetId = '1Z6G2PTJviFKbXg9lWfesFUrvc3NSIAC7jGvhKiDGdcY';
var rangeName = 'Payments!A:D';
var values = Sheets
.Spreadsheets
.Values
.get(spreadsheetId,rangeName)
.values;
return values;
}
the data lying in columns A,B,C,D is getting pulled and being displayed correctly through the following HTML template
<? var data = getData(); ?>
<table>
<? for (var i = 0; i < data.length; i++) { ?>
<tr>
<? for (var j = 0; j < data[i].length; j++) { ?>
<td><?= data[i][j] ?></td>
<? } ?>
</tr>
<? } ?>
</table>
Instead of getting all the rows and all the columns from A,B,C,D I would like to run an SQL Query to retrieve some of the columns with a WHERE clause like SQL. I understand that the =QUERY() function that works in the spreadsheet does not work inside the GAS. So my next attempt was to retrieve SOME of the rows by using a getBatch method .. and this is where I get ERRORs
in this case, i want to exclude column C and get only A,B and D,E the code that throws an error is as follows :
function getData2(){
var spreadsheetId = '1Z6G2PTJviFKbXg9lWfesFUrvc3NSIAC7jGvhKiDGdcY';
/* var rangeName1 = 'Payments!D'; */
/* var rangeName2 = 'Payments!A'; */
var values = Sheets
.Spreadsheets
.Values
.batchGet(spreadsheetId,{ranges: ['Payments!D:E', 'Payments!A:B']})
.values;
return values;
}
In the corresponding HTML template, all that changes is getData is replaced with getData2
<? var data = getData2(); ?>
with this code, I get the following error :
TypeError: Cannot read property "length" from undefined. (line 6, file "Code", project "Report003")
Now I have two questions :
- What is wrong in my code and how can i fix it?
- Is it possible to use SQLite to simplify the process of extracting the desired rows and columns
I have seen this question but I am not able to understand the answer adequately