I am trying this code to copy a Google spreadsheet into a specific folder
var targetFolder = DocsList.createFolder(\"Testing\");
var mainDocBlob = DocsList.g
Below is the code that should work. I'm guessing when you get as a blob it.s defaulting to casting the new file as pdf. In the code below makeCopy is defaulting to making the new file name 'Copy of ...' but this can be set by a string.
var targetFolder = DocsList.createFolder("Testing");
var mainDoc = DocsList.getFileById("docId");
mainDoc.makeCopy().addToFolder(targetFolder);
As mentioned in this other post answer, Google docs are not "files" in the common sense, there is no need to use getBlob to copy them as they don't actually have Blob... (and they don't actually have a 'size' nor use space in your Gdrive for the same reason !)
Instead you should use (as mentioned in David's answer) addToFolder()
.
From this point your file will be moved to the corresponding folder. If you need to have a copy in your root folder then make a copy of it but depending on how you access your files this is not always necessary as you can always explore your Google drive using "All Items" where you see all items in their corresponding folders (and the name of the folder they belong to as well).
If the file was already in a folder then it will appear as belonging to both folders and again you can choose to leave it as it is or remove it from the original folder using removeFromFolder()
.
I know this seems a bit redundant with the other answers but I just hope it makes things more clear on the organization of Google drive.
The DriveApp Service was updated August 20, 2013, with new methods File.makeCopy(destination) and File.makeCopy(name, destination), which allow scripts to specify a folder to which a file should be copied.
I suggest using file makeCopy() to create the copy; addToFolder() to put it in the target folder; and removeFromFolder()