Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
Folder ID displays in Logger but the value wont allow me to move file to said folder ID

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);
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda