Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!