Reputation: 6253
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?
Upvotes: 10
Views: 9318
Reputation: 141
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:
Upvotes: 14