PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 28, 2022

[FIXED] How can I import multiple CSVs to preset sheets?

 August 28, 2022     csv, google-apps-script, google-sheets-api, import, string-comparison     No comments   

Issue

I have some CSV files on my G-drive that I want to import to preset sheets, based on file names. For example,

  1. I have CSV files named: "REP1-abc", "REP2-bcd", "REP3-cde",...
  2. I have spreadsheet with sheets named: "REP1", "REP2", "REP3",...

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.


Solution

Loading CSVs

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);
    }
  }
}


Answered By - Cooper
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing