Skip to content

Instantly share code, notes, and snippets.

@NickFoden
Last active May 6, 2020 16:16
Show Gist options
  • Save NickFoden/5eff85660508f444d824ac1aadffbaff to your computer and use it in GitHub Desktop.
Save NickFoden/5eff85660508f444d824ac1aadffbaff to your computer and use it in GitHub Desktop.
G Sheet
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const fetch = require("node-fetch");
admin.initializeApp(functions.config().firebase);
const firestore = admin.firestore();
// write a function to take the data and return the JSON you want to upload to the database
const normalizeData = require("./src/normalizeData");
exports.goGetFinancialSheets = functions.pubsub
.schedule("every day 00:01")
.onRun(async (context) => {
// if not public will need to set up authorization
const data = await fetch("https://www.google.com/sheets/about/")
//maybe have to .json the data about/").then(res => res.json())
const organizedData = normalizeData(data)
// example with firestore collection of financialsheet and a made up id of docId
const databaseRef = firestore.collection("financialsheet").doc("docId")
//if updating the same document then can use update
databaseRef.update({
data: organizedData
})
//making a new document each time
const databaseRef = firestore.collection("financialsheet").doc()
//the document id
const key = databaseRef.id
// since new document use set
// reccomend to also set another key such as date so you can query the documents by something
databaseRef.set({
data: organizedData,
date: date.now(),
key
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment