简体   繁体   中英

google scripts file.makeCopy(name, destination) invalid argument

this is a segment of my code where I am trying to create a series of spreadsheets in a new folder that are all copies of a single template to later be edited individually. Here is some example code:

function myFunction() {
  var accountNames = [1,2,3,4,5];
  findSpreadSheets(accountNames);
}

function findSpreadSheets(accountNames) {
  var files = [];
  Logger.log(DriveApp.getFoldersByName("Automated Budgets").hasNext());
  var folder = DriveApp.getFoldersByName("Automated Budgets").next();
  for (var i=0; i<accountNames.length; i++) {
    var spreadsheetIterator = DriveApp.getFilesByName(accountNames[i] + " Dynamic Budget");
    if (spreadsheetIterator.hasNext()){
      var file = spreadsheetIterator.next();
      files.push(file);
    }
    else{
      var template = DriveApp.getFilesByName("Dynamic Budget Template").next()
      template.makeCopy(accountNames[i] + " Dynamic Budget", folder);
  var newFile = DriveApp.getFilesByName(accountNames[i] + " Dynamic Budget").next();
      newFile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
      files.push(newFile);
    }
  }

  return files;

}

I am getting the error "Invalid argument (line 21, file "")" which is referring to the line template.makeCopy(accountNames[i] + " Dynamic Budget", folder). After numerous tests I have concluded that the error is not an issue with the name, but with the destination. The Logger before the declaration of the folder prints true, the folder does exist in my drive, hence why I am confused as to why this does not appear to be a valid directory. Any help would be appreciated.

I'm not sure if this addresses your issue directly but I also encountered an odd "Invalid Argument" error when making calls to the makeCopy() function. It kept hanging up on a single item, despite the arguments being perfectly valid.

It turns out that the file in question was a Lucid Chart file, which is a 3rd party application that uses the Google Drive SDK to store their files on Google Drive. When I checked the MIME type for this file it was "application/vnd.google-apps.drive-sdk.7081045131". It would seem that this type of file is outside the capability of the makeCopy() function. Now we know.

If I was less lazy, I could now programatically avoid non-standard MIME types, but instead I just filtered out these files from the file list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM