Gubernaculum
Gubernaculum

Reputation: 17

ImportCSV from GoogleDrive

Hello and HAPPY father's day to all.

Trying to currently IMPORT a CSV file from a designated folder and specific file name based on Cell value A2 and B2 into a target SHEET(lets say INPUT), which I have not coded yet because I'm troubleshooting the first step of importing the CSV.

Do I have the right function? Or am I totally off. Any help or point in the right direction is appreciated.

function importCSV() {

  // get current spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // get folder
   var id = ss.getSheetByName("Dashboard").getRange("B1").getValue();
  var folder = DriveApp.getFolderById(id); // <--- your folder ID goes here 
  
  // get file name from the cell 
  var name = ss.getSheetByName("Dashboard").getRange("A2").getValue();
  var file = DriveApp.getFilesByName(name).next();

  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);

Upvotes: 0

Views: 49

Answers (1)

Cooper
Cooper

Reputation: 64032

Try this:

function importCSV() {
  const ss = SpreadsheetApp.getActive();
  const id = ss.getSheetByName("Dashboard").getRange("B1").getValue();
  const folder = DriveApp.getFolderById(id);
  const name = ss.getSheetByName("Dashboard").getRange("A2").getValue();
  const file = folder.getFilesByName(name).next();
  const csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
  const sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}

I just tested it and the filename includes the extension. If you include the extension it works. Without the extension you get the error.

Upvotes: 1

Related Questions