I have some CSV files on my G-drive that I want to import to preset sheets, based on file names. For example,
Target is to take first four letters of file name to match correct sheet name and import it to that sheet. Im new in this so i will appreciate any tips on how to compare those two names.
function loadcsvs() {
const fldr = DriveApp.getFolderById("Folder id");
const files = fldr.getFileByType(MimeType.CSV);
const ss = SpreadsheetApp.getActive();
const shts = ss.getSheets().map(sh => sh.getName());
while(files.hasNext()) {
let file = files.next();
let sname = file.getName().slice(0,4);
let idx = shts.indexOf(sname);
if(~idx) {
let sht = ss.getSheetByName(shts[idx]);
let vs = Utilities.parseCsv(file.getBlob().getDataAsString())
sht.getRange(1,1,vs.length,vs[0].length).setValues(vs);
}
}
}