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

227
Visualizações
Copy data of specific files in specific sheets with Apps Script

This code search files inside a folder (files are filtered by sufix) and return a list with two columns, one for filenames and other of file IDs.

var ss = SpreadsheetApp.getActive();
var sh = SpreadsheetApp.getActiveSheet();
var FILES_FOLDER = DriveApp.getFolderById("FOLDER ID");
var sufix = 'TXT' // adapt as necessary
var list = [];
var files = FILES_FOLDER.getFiles();
while (files.hasNext()) {
  file = files.next();
  list.push([file.getName(),file.getId()])
}
var result = [['FILENAME','FILE_ID], ...list.filter(r => r[0].includes(sufix)).sort()]
sh.getRange(2,3, result.length, result[0].length).setValues(result);

Well, I need to copy the data of each .txt file in a specific sheet (that already exist) for each .txt. There are like 5 possible .txt files but the 5 are not always in the folder (sometimes 2, or 3) so I need the code to be versatile.

if (TXTFILE EXIST) {
var TXT_FILE = DriveApp.getFileById(FILE ID);
TXT_FILE.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
var tsvUrl = "https://drive.google.com/uc?id="+FILEID+"&x=.tsv";
var tsvContent = UrlFetchApp.fetch(tsvUrl,{muteHttpExceptions: true }).getContentText();
var tsvData = Utilities.parseCsv(tsvContent, '\t');

var sheet_SP = SpreadsheetApp.getActive().getSheetByName('SHEET_TO_COPY_DATA');
sheet_SP.getRange(1, 1, tsvData.length, tsvData[0].length).setValues(tsvData);
} else {
  return
}

I suppose is something like attach each filename to his ID, and then if file with X word in his filename exist then get his id from the previous list. Or maybe the SHEET NAME has to be also linked to the list for the iteration. I don't know which would be the cleanest way to do this.

about 4 years ago · Juan Pablo Isaza
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