Okay so I have a sheet that I use to keep track of tips at work. Since its a shared sheet to keep it simple I just copy the sheet when its done and then archive the copied sheet to a subfolder based on the year. All the code works perfect except when I try to move the copied sheet to the folder that I want. Since I want to keep this as automatted as possible I have code to read the date within the sheet and either make a new folder for the year or move the file to the year folder that already exist. For some reason the file will not move to the folder I want and I've tried multiple ways: within the 'makeCopy' function as well as the DriveApp.getFileById().moveTo(); Here is my code, error runs at bottom of code:
//Create folder if does not exists only
function createFolder(folderID, folderName){
var parentFolder = DriveApp.getFolderById(folderID);
var subFolders = parentFolder.getFolders();
var doesntExists = true;
var newFolder = '';
// Check if folder already exists.
while(subFolders.hasNext()){
var folder = subFolders.next();
//If the name exists return the id of the folder
if(folder.getName() === folderName){
doesntExists = false;
newFolder = folder;
return newFolder.getId();
};
};
//If the name doesn't exists, then create a new folder
if(doesntExists == true){
//If the file doesn't exists
newFolder = parentFolder.createFolder(folderName);
return newFolder.getId();
};
};
function start(){
//Parent folder (Location)
var FOLDER_ID = '1yinFsKfMP3_pWbM7BnOKcNRk-BDcty6E';
//Add the name of the folder here (Year of the sheet):
var year = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Instructions").getRange('L2').getDisplayValue();
var NEW_FOLDER_NAME = year;
//Create a new folder if a folder for the year doesnt exist (reverts to function:createFolder)
var myFolderID = createFolder(FOLDER_ID, NEW_FOLDER_NAME);
//Get the file ID of the parent file (Tip Tracker)
var getID = SpreadsheetApp.getActive().getId();
//Get the current name of the Tip Tracker (with dates)
var getName = SpreadsheetApp.getActive().getName();
//Create copy of Tip Tracker to be filed accordingly
var newFileID = DriveApp.getFileById(getID).makeCopy(getName + " ").getId();
var getNewFile = DriveApp.getFileById(newFileID);
getNewFile.moveTo(myFolderID);
Logger.log(newFileID);
Logger.log(myFolderID);
};
Try creating a getMyFolder as you did with getNewFile and moveTo the folder versus the ID.
const myFolder = DriveApp.getFolderById(myFolderID)
// ...
getNewFile.moveTo(myFolder)
File.moveTo(folder) takes a Folder, not an ID.