Home > Back-end >  Google Sheets : You do not have permission to call DriveApp.getFoldersByName
Google Sheets : You do not have permission to call DriveApp.getFoldersByName

Time:11-04

I have some code that I am trying to execute:

function createOrAppendFile(in_Folder, in_FileName, in_Content ) {
  var fileName=in_FileName;
  //var folderName=in_Folder;
  var folderName="TRADEARCHIVE";

  var content = in_Content;

  // get list of folders with matching name
  var folderList = DriveApp.getFoldersByName(folderName);  
  if (folderList.hasNext()) {
    // found matching folder
    var folder = folderList.next();

    // search for files with matching name
    var fileList = folder.getFilesByName(fileName);
   [... snip ...]

I have the following defined in appscript.json

{
  "timeZone": "America/Chicago",
  "dependencies": {},
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "webapp": {
    "executeAs": "USER_DEPLOYING",
    "access": "ANYONE_ANONYMOUS"
  },
  "oauthScopes": [
      "https://www.googleapis.com/auth/spreadsheets.readonly",
      "https://www.googleapis.com/auth/userinfo.email",
      "https://www.googleapis.com/auth/spreadsheets",
      "https://www.googleapis.com/auth/script.scriptapp",
      "https://www.googleapis.com/auth/drive.readonly",
      "https://www.googleapis.com/auth/drive"
  ]
}

I am getting the following error:

Nov 4, 2022, 12:28:53 AM    Error   Exception: You do not have permission to call DriveApp.getFoldersByName. Required permissions: (https://www.googleapis.com/auth/drive.readonly || https://www.googleapis.com/auth/drive)
    at createOrAppendFile(Code:33:29)
    at deleteRows(Code:173:11)
    at onMyEdit(Code:390:29)

What am I doing wrong? Any help, hints or advice would be greatly appreciated.

TIA

CodePudding user response:

What seemed to work for me ( in this case ) was to place an "Open" Trigger on the function doing the search for the Folder.

Deployment       Event                            Function
Head             From spreadsheet - On open       createOrAppendFile

CodePudding user response:

YOU CAN TRY TO

setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);

ADD THIS TO YOUR GOOGLE SCRIPT

THANKS

  • Related