2

I want to sync a Google Sheet with my Firebase real-time database. My code is:

function writeDataToFirebase() {
  var ss = 

SpreadsheetApp.openById("18AU9ZP9UHs9lvIeTA0SyJv6tcHPe7ux0tLopkRAz1T0");
  var sheet = ss.getSheets()[0];
  var data = sheet.getDataRange().getValues();
  var dataToImport = {};
  for(var i = 2; i < data.length; i++) {
    var date = data[i][1];
    dataToImport[date] = {
    niftyclose:data[i][2],
    sensexclose:data[i][5]
  };
}

 var firebaseUrl = "<MY database URL>"; //Yes I have entered the URL in my actual code

 var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
 base.setData("", dataToImport);

}

But when I run the script, I get this error:

ReferenceError: FirebaseApp is not defined

I tried adding the Firebase Library and got this error: Firebase Library Error

Can someone help me figure this out? Thanks in advance.

1 Answers1

12

I think that MYeP8ZEEt1ylVDxS7uyg9plDOcoke7-2l is the project key. In this case, it can be used for the legacy editor. But, in the current stage, the script ID is used for installing the GAS library. And also, when you use new script editor, it is required to use the script ID for installing the library. I thought that this is the reason of your issue.

When the project key of MYeP8ZEEt1ylVDxS7uyg9plDOcoke7-2l is installed using the the legacy editor and check appsscript.json, the script ID of the library can be known. It's 1hguuh4Zx72XVC1Zldm_vTtcUUKUA6iBUOoGnJUWLfqDWx5WlOJHqYkrt. So please modify your situation as follows.

1hguuh4Zx72XVC1Zldm_vTtcUUKUA6iBUOoGnJUWLfqDWx5WlOJHqYkrt

Please install the library using above script ID. This script ID can be also seen at https://sites.google.com/site/scriptsexamples/new-connectors-to-google-services/firebase/source

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165