Home > Software engineering >  Exception: Access denied: DriveApp while running script from separate Google Account
Exception: Access denied: DriveApp while running script from separate Google Account

Time:01-08

I'm looking to create a script that makes a copy of the active spreadsheet, gives edit access to the person who created the sheet, but then sets me as the owner.

This works when I run it using the owner account, but I receive

Exception: Access denied: DriveApp

when triggered from a separate Google account.

I thought my script would add both accounts as editors, then set my personal as the owner.

Any idea why?

function makeCopy() {
  
var title = SpreadsheetApp.getActiveSheet().getRange("B1").getValue();
  SpreadsheetApp.getActive().toast("Your Sheet is being created", "Good Luck!", 5);


  var email = Session.getActiveUser().getEmail(); //get the user's email who ran the script
  var ssID = SpreadsheetApp.getActiveSpreadsheet().getId(); //get the current Spreadsheet's ID
  var copyID = DriveApp.getFileById(ssID).makeCopy().setName(title).getId(); //make a copy of the spreadsheet and retrieve the new ID
  DriveApp.getFileById(copyID).addEditor(email); //add the user as an editor to the new copy
   DriveApp.getFileById(copyID).addEditor("[email protected]")
  DriveApp.getFileById(copyID).setOwner("[email protected]");




var newsheet = SpreadsheetApp.openById(copyID);



// Get the file associated with the sheet
var file = DriveApp.getFileById(newsheet.getId());

// Set the permissions to allow anyone to edit the file
file.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);


//var sheet = newsheet.getSheetByName("Sheet");
//sheet.deleteRows(15,3)



// var sheetId = "1234567890abcdefghijklm";

// Open the sheet in a new tab
var url = "https://docs.google.com/spreadsheets/d/"   copyID;
var html = HtmlService.createHtmlOutput('<script>window.open("'   url   '");</script>').setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi().showModelessDialog(html, "Showtime!");


}

CodePudding user response:

Modification points:

  • In the current stage, it seems that the specification for transferring the file ownership has been changed. Ref I thought that this might be the reason for your current issue of Exception: Access denied:.

When these points are reflected in your script, how about the following modification?

Modified script:

Before you test this script, please enable Drive API at Advanced Google services.

function makeCopy() {
  var title = SpreadsheetApp.getActiveSheet().getRange("B1").getValue();
  SpreadsheetApp.getActive().toast("Your Sheet is being created", "Good Luck!", 5);
  var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
  var copyID = DriveApp.getFileById(ssID).makeCopy().setName(title).getId();
  Drive.Permissions.insert({ role: "writer", type: "user", value: "[email protected]", pendingOwner: true }, copyID);
  DriveApp.getFileById(copyID).setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
  var url = "https://docs.google.com/spreadsheets/d/"   copyID;
  var html = HtmlService.createHtmlOutput('<script>window.open("'   url   '");</script>').setSandboxMode(HtmlService.SandboxMode.IFRAME);
  SpreadsheetApp.getUi().showModelessDialog(html, "Showtime!");
}
  • When the user of another account runs this script, you (the owner) got an email for transferring the ownership of the file. When you accept it, the owner is transferred.

References:

  • Related