0

I am trying to save attachment file from Gmail to Drive I found a code sample here and its works BUT I need to save the file into the shared folder. at MyDrive I have a shortcut to the shared folder.

for example:

    var GDRIVE_FILE = '_sheredFolder_/one/$name

    function processThread(thread, label){
    var attachments = message.getAttachments();
    for(var i=0; i<attachments.length; i++) {
      var attachment = attachments[i];
      var info = {
        'name': attachment.getName(),
        'ext': getExtension(attachment.getName())}
var file = createFilename(GDRIVE_FILE, info);
        saveAttachment(attachment, file);
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
DanyC
  • 11
  • 1

2 Answers2

1

If you have a folder shortcut in your "My Drive", then it's very likely that the folder is owned by someone else.

In order to be able to add files to a folder owned by someone else you should it should be shared with you as "editor". If it's shared with you as "viewer" you will not able to add the file to that folder.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
1

Well after digging a while, I modify the core to support shortcuts or Shared folders I add % at the begging of the initial path to identify when its a shared folder or local folder

that's the code:

var GDRIVE_FILE = '%TheSharedFolder/FOLDER/$name';

function getOrMakeFolder(path) {

  var folder = DriveApp.getRootFolder();
  var names = path.split('/');
  while(names.length) {
    var name = names.shift();
      if(name.charAt(0)==='%')
      {
            var first = name.substring(1,name.length)
            var folders = DriveApp.searchFolders('sharedWithMe');
          while (folders.hasNext()) {
              var folder = folders.next();
              if(folder.getName()===first)
              {
                Logger.log("folder name is : "+folder.getName()+" folderID: " + folder.getId())
                folder = DriveApp.getFolderById(folder.getId())
              }
            }
            continue;
      }  
    if(name === '') continue;
      var folders = folder.getFoldersByName(name);
      if(folders.hasNext()) {
        folder = folders.next();
      } else {
        folder = folder.createFolder(name);
      }
  } 
  return folder;
}
DanyC
  • 11
  • 1