0

I have a Sheet where I need to get the id of a file located in my Google-drive. I have written the folowing script:

function get_id_pdf() {
  var nom='INV432-altaïr-Famille XXX-XXX Marie-03-2016.pdf';
  var files = DriveApp.getFilesByName("'"+nom+"'");
  while (files.hasNext()) {
    var file = files.next();
    var name = file.getName();
    var type = file.getMimeType();
    var url = file.getUrl();
    var id = file.getId();
    // Logger.log(file.getId());
    return file.getId();
  }
}

If I execute it in the Script editor (with the run button), it is working well (I get the id the the Log). But If I call the script from a cell (in the cell: =get_id_pdf()) of my Google-Sheet, I get the error:

"You do not have permission to call getFilesByName" (see image)

I have of course all the authorization to access to this file (it is in my own drive, and the file havs been created by me).

Does somebody have an idea ?

Regards.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Does this answer your question? [Custom function throws a "You do not have the permission required to setValue" error](https://stackoverflow.com/questions/15933019/custom-function-throws-a-you-do-not-have-the-permission-required-to-setvalue-e) – Rubén Jun 21 '20 at 02:32

1 Answers1

0

You cannot make calls to services that require user authorization as an anonymous user, which is what custom functions execute as. You will need to put the behavior into a custom menu, or a sidebar so that it can be authorized by the user.

Authorization Documentation

Custom Function Documentation

If your custom function throws the error message "You do not have permission to call X service.", the service requires user authorization and thus cannot be used in a custom function.

Douglas Gaskell
  • 9,017
  • 9
  • 71
  • 128
  • I am sorry. I have read the document from the URLs but I just don't understand what to do. – Karl Reyka May 02 '16 at 16:42
  • You need to put the behavior in a custom menu or a sidebar. There is documentation on how to dot his, google `google spreadsheet custom sidebar` or `google spreadsheet custom menu`. My answer answers why you are receiving this error, if you want to know how to make a custom menu or sidebar then ask a new question related to what you are having trouble with. – Douglas Gaskell May 02 '16 at 18:36
  • @DouglasGaskell Do you think it's possible to call a custom menu function (that makes the request to google drive app) from a cell to work around that issue? – BenjaminK May 03 '20 at 19:15