Nick Dorogavtsev
Nick Dorogavtsev

Reputation: 13

Adding File to a Folder

I'm trying to relocate newly created Google Docs file to a folder within google drive (using Google Apps).

var newFile = DocumentApp.create('New File');
var newFileID = Docs.getFileById(newFile);
var newFileRelocated = newFileID.addToFolder(newFolder);

And I'm getting "Cannot find method addToFolder(. (Line ...)". What am I doing wrong? They method drops down as an option when I'm writing it and still it cannot find it.

Upvotes: 1

Views: 2980

Answers (4)

C Ried
C Ried

Reputation: 291

The following function is a simple google script to pass in an image URL.

function fetchImageToDrive_(imageUrl)
 {
 //Fetch image blob
    var imageBlob = UrlFetchApp.fetch(imageUrl).getBlob();
 //Create image file in drive
    var folder = DocsList.getFolder('test Folder');
    folder.createFile(imageBlob);
 }

Upvotes: 0

JimCampbell
JimCampbell

Reputation: 114

Just to add to this, I recently dealt with this issue.

I noticed the default location is to store the DocsList.create() file in the root folder (aka My Drive).

This could lead to a real headache if you were doing lots of files.

I added this as the line after the .addToFolder()

newFile.removeFromFolder(DocsList.getRootFolder());

Upvotes: 1

Serge insas
Serge insas

Reputation: 46794

The logic of this is not exactly as you tried...

here is a working example :

function myFunction() {
var newFile = DocumentApp.create('New File');
var newFileID = newFile.getId()
var newFolder = DocsList.createFolder('test Folder')
DocsList.getFileById(newFileID).addToFolder(newFolder)
}

Upvotes: 2

Henrique G. Abreu
Henrique G. Abreu

Reputation: 17752

It's likely that your newFolder is not what's expected. Is it a string? Where you defined it?

Anyway, the parameter expected in addToFolder must be a Folder object you got using some other method in DocsList. e.g. DocsList.getFolder("/path/to/folder") or DocsList.getFolderById("folder-id") and so on.

There seems to be other "inconsistencies" with your code, I'll paste what I you're trying to do:

var newDoc = DocumentApp.create('New Google Doc');
//a DocumentApp file and a DocsList file are not the same object, although they may point to the same Google Doc
var newFile = DocsList.getFileById(newDoc.getId());
var folder = DocsList.getFolder("/path/to/folder"); //I'm assuming the folder already exists
newFile.addToFolder(folder);

Upvotes: 2

Related Questions