OpenByUrl throwing error: You do not have permission to call openByUrl (line 39, file "Code")

google-apps-script

Solution

Custom functions run with limited permissions, and cannot take advantages of services that rely on user credentials, including DocumentApp.

https://developers.google.com/apps-script/execution_custom_functions#permissions

Problem

I have a script in my Google spreadsheet that opens a Google Doc and returns the text in a certain cell of a table, as follows: ``` function getItemStatus(docUrl) { var doc = DocumentApp.openByUrl(docUrl); var docBody = doc.getBody(); var docTbls = docBody.getTables(); var itmTbl = docTbls[0]; var itmStatus = itmTbl.getCell(3,1).getText(); return itmStatus; } ``` When I attempt to run the function in my spreadsheet, it returns this error: error: You do not have permission to call openByUrl (line 39, file "Code") If I create another function to call the above function directly it works just fine and returns the value I am after. ``` function testItemGet() { var docUrl = ("..."); itmStr = getItemStatus(docUrl); Logger.log("The item's value is " + itmStr) } ``` Resulting log file: [13-07-17 19:27:39:130 EDT] Starting execution [13-07-17 19:27:39:387 EDT] DocumentApp.openByUrl([...]) [0.253 seconds] [13-07-17 19:27:39:388 EDT] Document.getBody() [0 seconds] [13-07-17 19:27:39:388 EDT] Body.getTables() [0 seconds] [13-07-17 19:27:39:388 EDT] Table.getCell([3, 1]) [0 seconds] [13-07-17 19:27:39:389 EDT] TableCell.getText() [0 seconds] [13-07-17 19:27:39:389 EDT] Logger.clear() [0 seconds] [13-07-17 19:27:39:389 EDT] Logger.log([The item's value is Deferred , []]) [0 seconds] [13-07-17 19:27:39:390 EDT] Execution succeeded [0.256 seconds total runtime] From what I have Googled, as long as I have edit or owner rights to the document I should be able to do this. I am pretty new to Google Apps Scripting so please be gentle. :) Thank you ahead of time for your help and patience!

Original source