1

Anyone know how create a Spreadsheet file in a specific folder on Google Drive?

I have already tried the answer from this link.

EDIT - Tried this answer :-D :

     //"Move" file to folder-------------------------------//
  var fileID = '12123123213321'
  var folderID = '21321312312'
  var file = DriveApp.getFileById(fileID).getName()
  var folder = DriveApp.getFolderById(folderID)
  var newFile = file.makeCopy(file, folder)

  //Remove file from root folder--------------------------------//
  DriveApp.getFileById(fileID).setTrashed(true)

But the "correct answer" gives me the follow error "Uncaught TypeError: Cannot find function makeCopy in object Spreadsheet."

I saw other answers but none of them work. I have already tried enable "Drive API" from "Advanced Google Services", but it's kind of complicate to work with.

UPDATE! Just to explain, my problem is that I was creating the Spreadsheet

var sheetId = SpreadsheetApp.create("filename").getId();

And then trying to makeCopy, or addFile in a folder. Something like that:

<someFolder>.addFile(sheet);

What I detect is that to work I have to get a File type, not a string like after. So I change to the code;

var sheetId = SpreadsheetApp.create("filename").getId();
var file = DriveApp.getFileById(sheet);
<someFolder>.addFile(file);
Community
  • 1
  • 1
  • Try using the debugger, and run each line of code one line at a time, and view the values. [Google Documentation - Troubleshooting - Debugger](https://developers.google.com/apps-script/troubleshooting#using_the_debugger_and_breakpoints) – Alan Wells Oct 16 '15 at 17:57

1 Answers1

1

Instead of making a copy, why don't you just move it?

var file = DriveApp.getFileById(fileID),
    folder = DriveApp.getFolderById(folderID),
    parents = file.getParents();

folder.addFile(file);

while( parents.hasNext() )
   parents.next().removeFile(file);
Kriggs
  • 3,731
  • 1
  • 15
  • 23