TinyDb to Google Sheets

I am planning to create an app that can record attendance of students offline. I am almost finish yet I want the data to be sent online when YES button was pressed on a notifier. How can I able to do it.

I stored the data like this

How can I able to send all the data received in one click?

This is a trial but failed

Here is my script code

function updateData(month, date, studentNumber, data) {
  // Open the Google Sheets document using the specified ID
  var spreadsheet = SpreadsheetApp.openById('SPREADSHEET ID');
  
  // Find the sheet based on the month parameter
  var sheet = null;
  var sheets = spreadsheet.getSheets();
  for (var i = 0; i < sheets.length; i++) {
    if (sheets[i].getName() === month) {
      sheet = sheets[i];
      break;
    }
  }
  
  if (!sheet) {
    return 'Sheet for the specified month not found.';
  }

  // Get the values in row 5 as an array
  var row5Values = sheet.getRange(5, 1, 1, sheet.getLastColumn()).getValues()[0];

  // Convert date objects to text
  for (var i = 0; i < row5Values.length; i++) {
    if (row5Values[i] instanceof Date) {
      row5Values[i] = Utilities.formatDate(row5Values[i], 'GMT+0800', 'MM-dd-yy');
    }
  }

  // Find the column index for the specified date
  var dateColumnIndex = row5Values.indexOf(date);

  if (dateColumnIndex === -1) {
    return 'Date not found.';
  }

  // Find the row index for the specified student number
  var studentNumberColumn = sheet.getRange("A:A").getValues().flat();
  var studentRowIndex = studentNumberColumn.indexOf(studentNumber);

  if (studentRowIndex === -1) {
    return 'Student number not found.';
  }

  // Write the data to the cell at the intersection of the row and column
  sheet.getRange(studentRowIndex + 1, dateColumnIndex + 1).setValue(data);

  return 'Data updated successfully.';
}

function doGet(e) {
  var month = e.parameter.month;
  var date = e.parameter.date;
  var studentNumber = e.parameter.studentNumber;
  var data = e.parameter.data;
  var result = updateData(month, date, studentNumber, data);
  return ContentService.createTextOutput(result);
}

how many tags did you store in TinyDB?
you should send the data asynchronously to the Googlesheet… i.e. send the data of one tag and after it has been received in the Web.GotText event send the data of the next tag, etc.

for an example of asynchronous processing see App Inventor Tutorials and Examples: Webprefetch File by File | Pura Vida Apps

Taifun

1 Like

Around 47 tags

How can able to do this?

As shown here

Just adjust it to your needs

Taifun

Thank you but i don’t understand some concepts there, can you demo it?

Try the example as it is, pkay with it to get an better understanding of how the concept works

Then please try something yourself and if you got stuck, provide a screenshot of your relevant blocks

Taifun

I’m stuck here at getting all the values for all the tags with the key “Student Number”. I want to make a list of all the student number that had already scanned their codes in the app

It does not make sense to add static data into your app like this, it is a waste of blocks… 22 blocks for each student x 47 students = 1034 blocks

Store the static data in a file, upload it as asset into your app and read it on first run of the app

Alternatively store the data directly in your Google sheet… you do not have to upload all that static data…

Taifun

1 Like

Hello! thank you for your help. I already solved my problem by altering some of the codes in the AppScript. Again, thank you!

1 Like

Can you share your modified script here to help others?