Simon GIS
Simon GIS

Reputation: 1055

Google Sheets APP script: Import CSV in a specific Delimited Character

I have this script to import my CSV file from my drive folder into my Google Sheets.

function importCSVFromGoogleDrive() {

  var file = DriveApp.getFilesByName("Test.csv").next();
  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Testoutputs');
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);

}

My CSV is delimited either by a ";" or vertical bar "|"...

Is there simple solution to import my CSV based on that delimiter?

Upvotes: 0

Views: 573

Answers (1)

Mike Steelson
Mike Steelson

Reputation: 15318

The easiest way is to replace the separator with a comma, but you need to consider if the comma already exists and if you need to replace them first and probably adopt US regional parameter. I tried an example on my own dsv file

function importCSVFromGoogleDrive() {

  var file = DriveApp.getFilesByName("myDSVFile.dsv").next();
  var csvString = file.getBlob().getDataAsString()
  csvString = csvString.replace(/,/g, ".")
  csvString = csvString.replace(/\|/g, ",")
  var csvData = Utilities.parseCsv(csvString);
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Testoutputs');
  sheet.clear();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);

}

Upvotes: 1

Related Questions