Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving Files In Google Drive Using Google Script

I'm trying to create documents using information posted through Google forms, then once the document is created I would like to move the document into a shared folder for people to view.

At the moment I have the script taking all of the information from the Google Forms linked spreadsheet.

Using that information I'm using the following code to create the document:

  var targetFolder = DriveApp.getFolderById(TARGET_FOLDER_ID);
  var newDoc = DocumentApp.create(requestID + " - " + requestSummary);

This is creating the document successfully in my Google Drive root folder, but I can't seem to move it where I want to move it to.

I've seen a lot of posts suggesting use stuff like targetFolder.addFile(newDoc) but that doesn't work, similarly I've seen examples like newDoc.addToFolder(targetFolder) but again this isn't working for me.

It seems that all the online questions people have already asked about this are using the previous API versions that are no longer applicable and these methods do not apply to the new DriveApp functionality.

What I would like, if possible, is to create the new document as above so that I can edit the contents using the script, then be able to move that file to a shared folder. (From what I understand there is no 'move' function at present, so making a copy and deleting the old one will suffice).

like image 559
S Woodhouse Avatar asked Aug 06 '16 21:08

S Woodhouse


People also ask

How do I automatically move files to Google Drive?

Google offers Backup and Sync, an application you can install on your computer in order to back up any folder on your computer over to Google Drive automatically. Simply install Backup and Sync and you can add any folder on your computer to automatically upload all files to Google Drive.

How do I move a file to another folder in Google Drive?

You can either Drag the file to a folder or use the Move to option. Go to drive.google.com. Right-click the item you want to move. Choose or create a folder, then click Move.


2 Answers

If we make a copy of the file and trash the original, it would change the file URL and also the file sharing settings won't be preserved.

In Drive, it is possible to add a file to multiple folders with the .addFolder() method of DriveApp service. You can add the file to the target folder and then remove the file from the immediate parent folder.

function moveFiles(sourceFileId, targetFolderId) {
  var file = DriveApp.getFileById(sourceFileId);
  var folder = DriveApp.getFolderById(targetFolderId);
  file.moveTo(folder);
}
like image 125
Amit Agarwal Avatar answered Sep 18 '22 16:09

Amit Agarwal


This is my first post! I know this has been answered a few times, but I actually came across this question while working on my project, and while reviewing the Apps Script documentation, I figured out a concise way to do it. A variation of some1's answer.

var file = DriveApp.getFileById(fileid);
DriveApp.getFolderById(folderid).addFile(file);
DriveApp.getRootFolder().removeFile(file);

Hope it helps!

like image 25
Aaron McKeehan Avatar answered Sep 20 '22 16:09

Aaron McKeehan