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

170
Visualizações
Find a file back in folder structure in multiple situations (Apps Script)

I need to find with Apps Script a specific Sheet back in folder structure of my actual Sheet (found filtered by title fragment for example). But sometimes is in the direct parent and another two or three folders behind, so I want the most versatile solution to work in different situations. Which will be the best and clean way?

[ FILE TO FIND ] [ FOLDER 1 ] [ ACTUAL FILE ]
                 [ FOLDER 2 ] [ ]

[ FILE TO FIND ] [ ACTUAL FILE ]

My code to find the file (just in the parent folder right now)

var carpetapanelc = carpetabasecs.getParents();
var carpetapanelcid = carpetapanelc.next().getId();
var carpetapanelcname = DriveApp.getFolderById(carpetapanelcid);
var searchFor ='title contains "Panel"';
var names =[];
var filePanelIds=[];
var filesPanel = carpetapanelcname.searchFiles(searchFor);
  while (filesPanel.hasNext()) {
var filePanel = filesPanel.next();
var filePanelId = filePanel.getId();// To get FileId of the file
filePanelIds.push(filePanelId);
var filePanelname = filePanel.getName();
var filePanelurl = filePanel.getUrl();
names.push(filePanelname);
}

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Get Files in Parents That Include a String in their title

function getFilesInParentFolders(fileid="") {
    var file = DriveApp.getFileById(fileid)
    var fldrA = [];
    var filA = [];
    var folder = file.getParents();
    while (folder.hasNext()) {
      var f = folder.next();
      fldrA.push(f.getId());
      folder = f.getParents()
    }
    for(let i = 0;i<fldrA.length; i++) {
      let files = DriveApp.getFolderById(fldrA[i]).getFilesByType(MimeType.GOOGLE_SHEETS);
      while(files.hasNext()) {
        let file = files.next()
        if(file.getName().includes("Test")) {
          filA.push({name:file.getName(),id:file.getId(),parent:fldrA[i]});
        }
      }
    }
    
  Logger.log(JSON.stringify(filA));
}

This runs off of the folder id instead of a file id

function getFilesInParentFolders(folderid="") {
    var fldrA = [];
    var filA = [];
    var folder = DriveApp.getFolderById(folderid).getParents();
    while (folder.hasNext()) {
      var f = folder.next();
      fldrA.push(f.getId());
      folder = f.getParents()
    }
    for(let i = 0;i<fldrA.length; i++) {
      let files = DriveApp.getFolderById(fldrA[i]).getFilesByType(MimeType.GOOGLE_SHEETS);
      while(files.hasNext()) {
        let file = files.next()
        if(file.getName().includes("Test")) {
          filA.push({name:file.getName(),id:file.getId(),parent:fldrA[i]});
        }
      }
    }
    
  Logger.log(JSON.stringify(filA));
}
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