Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sheets "You do not have permission to call appendRow"

function myFunction() {
    var url = 'https://api.github.com/users/chaimf90/repos'
    var response = UrlFetchApp.fetch(url);
    var json = response.getContentText();
    var data = JSON.parse(json)
    var sheet = SpreadsheetApp.getActiveSheet();
    sheet.appendRow(['Repo Name', data[0].name]);
}

When I execute this function from the script editor it runs as expected, but when I try to call this function in the sheet itself by calling =myFunction(), I get an error saying that I do not have permission to call appendRow.

Why can I call this function from the script editor, but not from the sheet itself?

like image 935
Chaim Friedman Avatar asked Apr 24 '17 17:04

Chaim Friedman


People also ask

How do I enable script editor in Google Sheets?

Click Extensions > Apps Script to open the script editor, then copy the script text from the original spreadsheet and paste it into the script editor of another spreadsheet.

Can you call a REST API from Google Sheets?

Calling a REST API in Google Sheets is as easy as installing the Apipheny app, then opening the app in your Google Sheet, entering your API request, and clicking “Run”. Keep reading for instructions on how to import REST API data into Google Sheets.


1 Answers

I had the same problem. The solution seems to be to create a custom menu that runs an Apps Script function instead of writing a custom function and invoking it from a cell in the spreadsheet.

A function that is triggered from a menu will ask the user for authorization if necessary and can consequently use all Apps Script services. I learned this here.

Example:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('My Custom Menu')
      .addItem('Run My Function', 'myFunction')
  .addToUi();
}

function myFunction() {
  SpreadsheetApp.getUi()
     .alert('Running My Function');
}

After writing and saving this code:

  1. Close and re-open your google sheets document.
  2. After a few seconds, a new menu "My Custom Menu" will appear at the top, next to File, Edit, View, ..., Help.
  3. Click on "My Custom Menu" and then click on "Run My Function" in order to invoke the function myFunction.
like image 86
Keren Duchan Avatar answered Sep 18 '22 09:09

Keren Duchan