Home > Net >  Firebase | Google Sheet | App Script | How do I put the value on new row everytime the function is t
Firebase | Google Sheet | App Script | How do I put the value on new row everytime the function is t

Time:12-20

I have to extract the data from Firebase to the Google Sheet. I have managed to sync my Firebase data to the Google Sheet but I can't put it in a new row everytime the trigger is called in App Script.

Here's my code in App Script:

function getAllData() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var sheet = ss.getActiveSheet();

  var firebaseUrl = "firebase url";
  var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);

  var dataSet = [base.getData()];
  var rows = [], data;

  for (i = 0; i < dataSet.length; i  ) {
    data = dataSet[i];
    var in_time = Utilities.formatDate(new Date(), "GMT 8", "HH:mm:ss");
    var in_date = Utilities.formatDate(new Date(), "GMT 8", "yyyy-MM-dd");
    rows.push([data.Tank1, data.RandomTank1, data.RandomMain, in_time, in_date]);

  }
  a=2;
  dataRange = sheet.getRange(a, 1, rows.length, 5);
  dataRange.setValues(rows);
}

The output is this: enter image description here

And I want it to look like this and update the data in a new row: enter image description here

EDIT:

Thank you very much @onit for solving this for me! It worked!

Here's the screenshot of the output: enter image description here

CodePudding user response:

Try replacing the last 2 lines with: dataRange = sheet.getRange(sheet.getLastRow() 1, 1, rows.length, rows[0].length).setValues(rows);

References

getLastRow()

setValues()

  • Related