Cant seem to display an array of rows on the AI2 App

Hello all! im new to this community and only just discovered AI2 App inventor.

I run a Non profit Community Tabletop Club. Called Friockheim Tabletop Club.

Taking attendance by pen and paper every week was tedious and hard to read if the "Taker" of the attendance had bad hand writing. so i decided to give this a shot. So far the App takes attendance of folk using their membersip QR cards and updates a googlesheets doc online. The problem ive come across is that when the "Taker" forgets that they have scanned a person they have no way of finding out. So they asked for a screen whee they can check to see who is "Checked in". so far i have coded what i believe is the correct coding to allow this but when the variable is "returned" to the app. it will only display one result or if there is more than one result of "Todays date" then it will return "No Entries Found". which is confusing as i have a logger in the script which allows me to see that the variable did store the correct amount of rows needed when it did its filter.

Could anyone help me figure out why its not returning the whole array if there is more than one row in that array.

Thanks.

This is how the Googlesheets Table is laid out.

This is how the block is written

This is when there is more than one row in the array.

Code i used on the googlescript page.

// Commences function to retrieve action variable from app.
function doPost(e){
var action = e.parameter.action;

if(action == "check"){
return check(e);
}

}

// Commences Check input
function check(e) {

var rows = dailySheet.getDataRange().getValues();

var filteredRows = rows.filter(filterLogic);

Logger.log(filteredRows)

if (successId == 1){
return ContentService.createTextOutput(filteredRows).setMimeType(ContentService.MimeType.TEXT);
}

else{
return ContentService.createTextOutput("No Entries Found").setMimeType(ContentService.MimeType.TEXT);
}
}

var successId = 0;
var in_time = Utilities.formatDate(new Date(), 'Etc/GMT', 'yyyy-MM-dd');

var filterLogic = function(row){
if (row[4] == in_time){
successId = Number(successId)+1;
return true;

                }
                
                else{
                  return false;
                }
                
              }

I haven’t used googlescript.

But my question is,
why are you returning check(e) instead of just calling it?

Instead of

if(action == “check”){
return check(e);
}

Why are you not using

if(action == “check”){
check(e);
}

I do not know haha, I just presumed thats how it was accepted since it was app based. ill change that for the future. thanks.

By changing this

By changing this, I have received this new error

I’m sorry, the doPost needs a return. So your code was not wrong.

But, are you trying to send a PUT request or GET request? Wouldn’t you use doGet instead of doPost?
See the Docs here.
https://developers.google.com/apps-script/guides/web

The html in that huge font probably has a useful error message further off screen.
A smaller font might help show the error message.

In the interests of keeping it simple (to start off with)

I have not had a chance to test this, but try this script:

function doGet() {

var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetBy Name("Sheet1");
var rows = sh.getDataRange().getValues();
var output = [];

for (var i=0;i<rows.length;i++) {
if( row[i][4] == Utilities.formatDate(new Date(), ‘GMT’, ‘yyyy-MM-dd’) ) {
output.push([row[i][1] + " " + row[i][2]]);
} 
}

return ContentService.createTextOutput(output);

}

You may need to change a few things(sheet name ?), but it should return a list of tonight’s guests.

You use the script url and the Web1.get block

1 Like

Perfect! that worked! i had to change a few typos as Row wasnt "defined" etc and then it kept popping up saying cant find "doPost" in the app. so the final code that worked was this....

// Commences function to retrieve action variable from app.
function doGet(e){
var action = e.parameter.action;

if(action == "check"){
return doPost(e);
}

else{
return doPost(e);
}
}

function doPost(e){

var ss = SpreadsheetApp.openByUrl("https://");
var dailySheet = ss.getSheetByName("daily_attendance");
var row = dailySheet.getDataRange().getValues();
var output = ;

for (var i=0;i<row.length;i++) {
if( row[i][4] == Utilities.formatDate(new Date(), 'Etc/GMT', 'yyyy-MM-dd') ) {
output.push([row[i][1] + " " + row[i][2]]);
}
}

return ContentService.createTextOutput(output);

}

AAhh, how do i add “< br >” into this code without breaking it?


None of this is necessary:

return doPost(e);
}

else{
return doPost(e);
}
}

function doPost(e){

To get a vertical listing of names, you will have to iterate over the list returned and insert either the “\n” or the
(if htmlFormat is set) after each item added to join in label.

2 Likes

What I’d suggest is that since it is a data in comma seperated format, you can simply create a List using split text function and then display it in a ListView.

blocks-forum

Further if you need to separate the First Name and Last Name records using space as seperator, you may create a list of lists, where each record of the nameList contains a two-element list, with First name as first element and last name as second element.

Also, as Tim mentioned, you do not need doPost function in the code for the above mentioned task.

1 Like

This came up with a bad argument error (200)

In the end i just did this above which turned out to be
return ContentService.createTextOutput(output.join('\n'));

Result:

Oops sorry, not responseCode but nameList is to be displayed in the ListView.
Here's the corrected code.

blocks-forweb

Try this!

1 Like

Would just like to say thank you very much for both your guys help! This has helped alot with my understanding of how this prograam works and a little more of googlescript javascript as well :stuck_out_tongue:

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.