Cannot add Firebase library to Apps Script - database

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.

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:
Libraries
Firebase

Related

Google.Cloud.AppEngine.V1 client libraries and traffic splitting in .NET

I am trying to use the Client Libraries provided by Google to move traffic from one version of an app in AppEngine to another. However, the documentation for doing this just talks about using the rest API and not the client libraries.
Here is some example code:
var servicesClient = Google.Cloud.AppEngine.V1.ServicesClient.Create();
var updateServiceRequest = new UpdateServiceRequest();
updateServiceRequest.Name = "apps/myProject/services/myService";
var updateMask = new Google.Protobuf.WellKnownTypes.FieldMask();
updateServiceRequest.UpdateMask = updateMask;
// See below for what should go here...
var updateResponse = servicesClient.UpdateService(updateServiceRequest);
My question is what format do I use for the update mask?
According to the documentation I should put in:
split {"split": { "allocations": { "newVersion": 1 } } }
But when I try: updateMask.Paths.Add(#"split { ""split"": { ""allocations"": { ""myNewVersion"": 1 } } }");
... I get the exception:
"This operation is only supported on the following field(s): [labels, migration_config, network_settings, split, tag_to_target_map], but got field(s): [split { "split": { "allocations": { "myNewVersion": 1 } } }] from the update request.
Any ideas where I should put the details of the split in the field mask object? The property Paths just seems to be a collection of strings.
The examples for these libraries in Google's doco is pretty poor :-(
I raised a support ticket with Google and despite them suggesting a solution which didn't work exactly (due to trying to assign a string to the UpdateMask which needs a FieldMask object), I managed to use it to find the correct solution.
The code should be:
// appService is a previously retrieved Service object from the ListServices method
var updateServiceRequest = new UpdateServiceRequest();
updateServiceRequest.Name = appService.Name;
updateServiceRequest.UpdateMask = new Google.Protobuf.WellKnownTypes.FieldMask();
updateServiceRequest.UpdateMask.Paths.Add("split");
appService.Split.Allocations.Clear();
appService.Split.Allocations["newServiceVerison"] = 1;
updateServiceRequest.Service = appService;

How to move a shortcut in google drive

I need to move a shortcut file from one destination to another using google apps script.
Usually I would move a file or folder like this:
function move(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sStamdata = ss.getSheetByName("New");
var folderOld = DriveApp.getFolderById(folderOldId);
var destination = DriveApp.getFolderById(folderId);
var id = sStamdata.getRange('D77').getValue();
var file = DriveApp.getFileById(id);
folderOld.removeFile(file)
destination.addFile(file)
But this doesn't work with shortcuts. Any ideas?
How about this answer?
Modification points:
It seems that in the current stage, the shortcut cannot be moved using Drive service. I think that this might be resolve in the future update.
So as the current workaround, in this answer, I would like to propose to use the method of "Files: patch" Drive API v2, because Drive API of Advanced Google services is used.
When your script is modified, please modify as follows.
Modified script:
Before you run the script, please enable Drive API at Advanced Google services.
function move(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sStamdata = ss.getSheetByName("New");
// var folderOld = DriveApp.getFolderById(folderOldId);
// var destination = DriveApp.getFolderById(folderId);
var id = sStamdata.getRange('D77').getValue();
// var file = DriveApp.getFileById(id);
Drive.Files.patch({parents: [{id: folderId}]}, id); // Added
}
folderId is the destination folder ID.
Please confirm the file ID of the shortcut again.
Note:
For example, when the file ID of the shortcut is confirmed with the shared link using the browser, the file ID is ID of the original file. Please be careful this.
References:
Advanced Google services
Files: patch

How to migrate RealmJS db after change the column type?

I'm using realmjs DB in my react-native app where I have a schema settings. I want to change a property type of that schema from int to string. So I understand that I need to perform migration and I decided to perform a linear migration. For the migration I followed the example from Realm documentation and ended up doing something like below:
const schemaList = [schemaV1,schemaV2];
let nextSchemaIndex = Realm.schemaVersion(Realm.defaultPath);
while (nextSchemaIndex < schemaList.length) {
const migratedRealm = new Realm(schemaList[nextSchemaIndex++]);
migratedRealm.close();
}
export default new Realm(schemaList[schemaList.length - 1]);
schemaV1 is the old version of the db and schemaV2 is the latest version of the db after changes of property type. The schemaV2 also has a migration function like below:
if (oldRealm.schemaVersion < 1) {
const oldObjects = oldRealm.objects(TBL_MOBILE_SETTING);
const newObjects = newRealm.objects(TBL_MOBILE_SETTING);
for (let i = 0; i < oldObjects.length; i++) {
newObjects[i].module = oldObjects[i].module;
newObjects[i].setting = oldObjects[i].setting;
}
}
But at the end, when I'm trying to run the app, it's crashing with an error message that
Database migration is needed
Does it mean that the migration function in schemaV2 never runs? If so, how to make sure that all the migration functions are running properly? Or am I missing something else?
EDIT
I found that column type change is not allowed in RealmJS and I added a new column and tried to perform migration but still same error.
The problem was what I guessed. The migration function was not being called. I had to create an export const function and call the migration function in the index file cause it's the file which is called at the starting of the app.

Updating Redux with Sqlite3 in an Electron application

I am writing an Electron application and I want to display some data from a local sqlite3 database file. I am using React as my front-end framework and Redux to update the table data. However I am having trouble figuring out what's the best way to query from the .db file and update Redux with the new data. Can someone give me some insights on what is the best way to go about it?
I was able to load a .db file using the node module sqlite3 and included a javascript function as such:
var sqlite3 = require('sqlite3').verbose();
let dbSrc = 'processlist.db';
var fetchDBData = (tablename) => {
var db = new sqlite3.Database(dbSrc);
var queries = [];
db.each("SELECT * FROM " + tablename, function(err, row) {
queries.push(row);
});
db.close();
return queries;
};
Since I am using React and Redux for my front end, I was able to invoke this function by calling
window.fetchDBData(tablename);

Google apps script, openByURL returns missing file?

I've been trying to figure out why part of my Google App script doesn't work, but I've failed to come up with an answer.
The script is downloading an attachment, CSV, from an email in Gmail and stores in with a specific name in a specific folder - this works perfectly fine.
But then I want to edit the CSV, and this is where I run into problems.
var newFolderIterator = DriveApp.getFoldersByName(destinationFolderName)
var newFolderId, myFileName, myFileId
while(newFolderIterator.hasNext()) {
newFolderId = newFolderIterator.next().getId()
var newFileList = DriveApp.getFolderById(newFolderId).getFiles()
while(newFileList.hasNext()) {
myFileName = newFileList.next()
myFileId = myFileName.getId()
var myFileURL = myFileName.getUrl()
Logger.log(myFileName.getId() + " " + myFileName.getName()) //Logs the ID and Name
Logger.log(myFileURL) //Logs the URL
var ss = SpreadsheetApp.openById(myFileName.getId()) //Error, cannot find the ID (error message: perhaps it's missing?)
}
}
I've tried using the openByURL as well, with the same error message.
Probably really easy to fix, any hints and tips is appreciated.
Thanks
The problem here is you are uploading a CSV but attempting to open it with SpreadsheetApp. SpreadsheetApp can only open Google Sheets documents, and your CSV is not automatically converted.
The CSV must first be converted to a Google Sheets document before you can access it with SpreadsheetApp.
You may be able to do this on upload, but I haven't tried this personally. This question looks relevant:
How to automatically import data from uploaded CSV or XLS file into Google Sheets

Resources