0
votes

I want to copy google drive folder to another google Drive by using App Script. Right now I am sharing folder to target account and try to copy folder to target google drive.Here is my App Script Code:

// Make copy in Google Drive
function CopyTamplate(companyName) {

var folder = DriveApp.getFolderById("<<folder_id>>");
var folderid = folder.getId();

folder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);

Drive.Permissions.insert(
  {
    'role': 'reader',
    'type': 'user',
    'value': "[email protected]"
  },
  folderid,
  {
    'sendNotificationEmails': 'false'
  });  


var sourceFolder = "Company1_copy_tamplete";
var targetFolder = companyName;

var source = DriveApp.getFoldersByName(sourceFolder);
var target = DriveApp.createFolder(targetFolder);

 if (source.hasNext()) {
   copyFolder(source.next(), target);
  }
}

This is a Code for Share folder from One Drive to another by Target Email, Here is a function copyFolder:

function copyFolder(source, target) {

var folders = source.getFolders();
var files   = source.getFiles();

while(files.hasNext()) {
  var file = files.next();
  file.makeCopy(file.getName(), target);
}

while(folders.hasNext()) {
  var subFolder = folders.next();
  var folderName = subFolder.getName();
  var targetFolder = target.createFolder(folderName);
  copyFolder(subFolder, targetFolder);
 }   
}

I have used Google Script Execution API for calling this Script function, and i am getting this Error. Script error message: "Action not allowed". Please help me to solve the issue or suggest another way to perform this task. Thank you.

1

1 Answers

1
votes

This is happening because the role you specified in Drive.Permissions.insert is 'role': 'reader'. Reading the Sharing Files docs, these are the only permitted operations for 'reader':

-Read the metadata (e.g. name, description) of the file or folder

-Read the content of the file

-Read the list of items in the folder

I suggest setting the role to owner or writer instead.