Mike Eburne
Mike Eburne

Reputation: 351

Google Apps Script Remove Folder From Drive

I have a very simple script that runs in a Spreadsheet and triggers an event on Form Submit. Basically what happens is when a new record is added to the spreadsheet a folder is created in a designated sub-folder. The script works 100% but the end result is a folder in the root of My Drive AND a folder in the destination sub-folder.

The spreadsheet columns are A = Timestamp, B = Organisation Name & C = Target Parent Folder (into which the new folder will be placed).

I need to then delete the folder located in the root and leave the other folder where it is.

Alternatively I need to place the designated folder directly into its target folder.

Here is my code:

  function newOrgFolder(){
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getActiveSheet();
      var lastRow = sheet.getLastRow();
      var folderCell = sheet.getRange(lastRow, 2);
      var folderName = folderCell.getValues();
      var contractStatusRange = sheet.getRange(lastRow, 3);
      var contractStatus = contractStatusRange.getValues();

      DriveApp.createFolder(folderName);

      var source = DriveApp.getFoldersByName(folderName).next();
      var folder = DriveApp.getFoldersByName(contractStatus).next();

      folder.addFolder(source);


}

Can anyone let me have the correct code to remove the folder from root and leave the copied folder where it is?

Or let me know how to place the new folder directly into the target folder without having it appear in root as well.

Upvotes: 1

Views: 1024

Answers (1)

Amit Agarwal
Amit Agarwal

Reputation: 11278

You can create the folder in the target folder directly.

  var folderName = folderCell.getValues();
  var folder = DriveApp.getFoldersByName(contractStatus).next();
  folder.createFolder(folderName);

Upvotes: 2

Related Questions