0
votes

Back on another seemingly impossible task. Apparently people in my office can't move folders around the Drive, so my task is to automate the moving process. As you can see in the Sheet (link below) I have a setup for a routing system.

https://drive.google.com/file/d/0B2LmfUtAXAgkN1VHQlphOFEyNVk/view?usp=sharing

I have the criteria, such as the name of the folder, what folder it should be in, and what folder it needs to be moved to. Basically, I'm trying to find a script that does the following things:

1) Works when a button is selected in the Google Sheet 2) Moves a folder in the Drive from Folder A to Folder B. 3) Does so using cell references.

Don't know how to find the folder ID, which wouldn't help much as the folder to be moved would change based on the spreadsheet criteria. And I probably can't use any outside program as it's on a government system. Any ideas? I have this great piece of script, but it doesn't seem to work using cell references.

function copyAndMove(file,folder){
var newfile=file.makeCopy('copy of '+file.getName(D9));
newfile.addToFolder(B1);//
newfile.removeFromFolder(DocsList.getRootFolder());}

Any help is greatly appreciated!

2

2 Answers

0
votes

To get a value in a cell you have to use the format below

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];
 // First argument is row, second is column. 1,2 returns the value in B1
 var range = sheet.getRange(1, 1);
 var value = range.getValue();

So your final code would be

function copyAndMove() {
     var ss = SpreadsheetApp.getActiveSpreadsheet();
     var sheet = ss.getSheets()[0];
 // First argument is row, second is column. 9,4 returns the value in D9
     var D9 = sheet.getRange(9, 4).getValue();
     var B1 = sheet.getRange(1, 2).getValue();
     var file = DriveApp.getFilesByName(D9).next();
     var folder = DriveApp.getFoldersByName(B1).next();
     var newfile = file.makeCopy('copy of '+ D9, folder);
     file.setTrashed(true); 
}

I'm not sure what you wanted to do with newfile.removeFromFolder(DocsList.getRootFolder()); but if you wanted to delete the original file the last line of this function does that.

0
votes

Found code that moved one folder to another. I've been searching for it for weeks, stumbled on it quite by accident. Sorry for any inconvenience.

Implement a folder move function in Google Dirve