fhqvst / avanza

A JavaScript client for the unofficial Avanza API
MIT License
229 stars 59 forks source link

Google Sheets/Google Script? #59

Closed Lejoon closed 3 years ago

Lejoon commented 3 years ago

Is it possible to implement your implementation into Google Sheets/Google Script to be able to use the functions in spreadsheets?

fhqvst commented 3 years ago

While I haven't tried this myself, it looks like you could do so by leveraging this package:

https://www.npmjs.com/package/google-spreadsheet

Untested example below:

const { GoogleSpreadsheet } = require('google-spreadsheet')
const Avanza = require('avanza')

const avanza = new Avanza()
const p1 = avanza.authenticate({
  username: process.env.AVANZA_USERNAME,
  password: process.env.AVANZA_PASSWORD,
  totpSecret: process.env.AVANZA_TOTP_SECRET,
})

const doc = new GoogleSpreadsheet(process.env.GOOGLE_SHEET_ID)
const p2 = await doc.useServiceAccountAuth({
  client_email: process.env.GOOGLE_EMAIL,
  private_key: process.env.GOOGLE_PRIVATE_KEY,
})

Promise.all([p1, p2]).then(async () => {
  await doc.loadInfo()
  const sheet = doc.sheetsByIndex[0]
  const positions = await avanza.getPositions()
  await sheet.addRows(positions.instrumentPositions)
})
Lejoon commented 3 years ago

That looks awesome! I will check it out. I was hoping the other way around however, to implement your package somehow into Google Apps to be able to define functions spreadsheet that you can use in the cells directly in Google Sheets.

fhqvst commented 3 years ago

Right, I don't think that's possible unfortunately. Even if you make the HTTP calls manually using Google Sheets, I suspect you'd still run into CORS issues.