Home > database >  Wrapper around importdata() in Google Sheets
Wrapper around importdata() in Google Sheets

Time:05-27

I am trying to write a wrapper around Google Sheets' importdata() in code.gs:

100 
101  function cryptofinance(token) {
102     var currentCell = SpreadsheetApp.getCurrentCell();
103     currentCell.setValue('=importdata("https://cryptoprices.cc/"  token)');
104  }
105
106 ...

The idea is to call the function from any sheet in the spreadsheet to get the price of any crypto currency token:

    |          A
 --- ------------------------- 
| 1 |  =cryptofinance("aion") |
 --- ------------------------- 

I get an error though when I try this:

    |          A
 --- ------------------------- 
| 1 |  #ERROR                 |
 --- ------------------------- 

#ERROR Exception: You do not have permission to perform that action. (line 102).

Why is this happening and how can I solve it?

CodePudding user response:

Some permissions are assumed by Apps Scripts when running functions but in some instances you need to update your appsscripts.json with the correct OAuth permissions manually to give permission to access data.

To show your appsscripts.json, navigate to your project settings and toggle the option "Show 'appsscript.json' manifest file in editor". This should reveal it in the sidebar in Apps Scripts.

Once in the appsscript.json, add

"oauthScopes": [
    "https://www.googleapis.com/auth/spreadsheets",
 ]

You can read more about it here

CodePudding user response:

Try this:

function cryptofinance(token = "AION") {
 //Logger.log(UrlFetchApp.fetch(`https://cryptoprices.cc/${token}/`));
 return UrlFetchApp.fetch(`https://cryptoprices.cc/${token}/`).getContentText();
}
  • Related