RemcoE33

Get Financialmodelingprep transcipts

May 26th, 2021 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Returns selected transscript from financialmodelingprep.
  3. *
  4. * @param {"AAPL"} ticker - Input the stock ticker.
  5. * @param {3} quarter - Input quarter number
  6. * @param {2020} year - Input year number
  7. * @param {"demo"} apikey - Input your apikey
  8. * @return {array} transscript
  9. * @customfunction
  10. */
  11. function GETTRANSCRIPT(ticker = 'AAPL', quarter = 3, year = 2020, apikey = 'demo'){
  12.   const url = `https://financialmodelingprep.com/api/v3/earning_call_transcript/${ticker}?quarter=${quarter}&year=${year}&apikey=${apikey}`
  13.   const res = UrlFetchApp.fetch(url)
  14.   const data = JSON.parse(res.getContentText())
  15.   const split = data[0].content.split(/\n/);
  16.   const output = []
  17.   split.forEach(line => { output.push([line])})
  18.   return output
  19. }
  20.  
  21. function onOpen(e){
  22.   const menu = SpreadsheetApp.getUi().createMenu('Transcript')
  23.     .addItem('Get transcript in current cell', 'transcript')
  24.     .addItem('Set api key', 'setApiKey')
  25.     .addItem('Get stored api key', 'readApiKey')
  26.     .addToUi()
  27. }
  28.  
  29. function transcript(){
  30.   const paramsRaw = SpreadsheetApp.getUi().prompt(`Enter url parameters '|' separated->: AAPL|3|2020 `).getResponseText()
  31.   const params = paramsRaw.split("|")
  32.   const prop = PropertiesService.getScriptProperties()
  33.   const token = prop.getProperty('TOKEN')
  34.   const response = GETTRANSCRIPT(params[0],params[1],params[2],params[3],(token) ? token : 'demo')
  35.  
  36.   const ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
  37.   const richTextValues = [];
  38.  
  39.   response.forEach(row =>{
  40.     const text = row[0]
  41.     const colonPositions = text.indexOf(':')
  42.     richTextValues.push([SpreadsheetApp.newRichTextValue()
  43.       .setText(text)
  44.       .setTextStyle(0, colonPositions, SpreadsheetApp.newTextStyle()
  45.       .setBold(true)
  46.       .build())
  47.       .build()])
  48.   })
  49.  
  50.   ss.getRange(ss.getCurrentCell().getRow(),ss.getCurrentCell().getColumn(),response.length,1).setRichTextValues(richTextValues)
  51.  
  52. }
  53.  
  54. function setApiKey(){
  55.   const token = SpreadsheetApp.getUi().prompt('Enter api key:').getResponseText()
  56.   const prop = PropertiesService.getScriptProperties()
  57.   prop.setProperty('TOKEN', token)
  58. }
  59.  
  60. function readApiKey(){
  61.   const prop = PropertiesService.getScriptProperties()
  62.   const token = prop.getProperty('TOKEN')
  63.   SpreadsheetApp.getUi().alert((token) ? token : "No token is set")
  64. }
Add Comment
Please, Sign In to add comment