1

I am trying this code to copy a Google spreadsheet into a specific folder

var targetFolder = DocsList.createFolder("Testing");
var mainDocBlob = DocsList.getFileById("docId").getBlob();
var newMainDoc = targetFolder.createFile(mainDocBlob);

But the resulting file is a Pdf file instead of a spreadsheet as the source was.

How to do this right using DocsList and always getting the same type of file as the source is.

Thanks

Fausto R.
  • 1,314
  • 3
  • 16
  • 29

4 Answers4

5

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.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
4

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.

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • that's what I have been doing so far, but I was looking for a more efficient way and it looks like doesn't exist, thanks for your reply ... File > getBlob is not even documented, I didn't realized that google docs have no blob, thanks – Fausto R. Nov 11 '12 at 16:07
  • @Fausto R Google Docs does support blobs. See https://developers.google.com/apps-script/class_blob and https://developers.google.com/apps-script/class_docslist#createFile I think you need to clarify your question – mhawksey Nov 12 '12 at 07:34
  • I was just trying to copy a file directly into a folder, instead of makecopy, addtofolder and remove from root folder – Fausto R. Nov 13 '12 at 13:44
2

I suggest using file makeCopy() to create the copy; addToFolder() to put it in the target folder; and removeFromFolder()

DavidF
  • 1,335
  • 1
  • 15
  • 26
  • that's what I have been doing so far, but I was looking for a more efficient way and it looks like doesn't exist, thanks for your reply – Fausto R. Nov 11 '12 at 16:03
2

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);
mhawksey
  • 2,013
  • 5
  • 23
  • 61
  • that's what I have been doing so far with the addition of removing from root folder, but I was looking for a more efficient way and it looks like doesn't exist, thanks for your reply – Fausto R. Nov 11 '12 at 16:05