Home > Blockchain >  Unable to Import CSV data in Google Apps Script. What is wrong?
Unable to Import CSV data in Google Apps Script. What is wrong?

Time:06-11

I'm trying to simply import a CSV file into a specific Google spreadsheet and specific sheet. I've seen multiple versions people have used to include CSVs into Google Sheets, but I can't seen to get any to work. What am I doing wrong? In this example I get the error "Could not parse text". Link to CSV file in Google Drive

function importConduitRunsSchdeule() {
  var ss = SpreadsheetApp.getActive();
  var file = DriveApp.getFilesByName("CONDUIT_RUNS_REVIT.csv")
  var csv = file.next().getBlob().getDataAsString();
  var csvData = Utilities.parseCsv(csv);
  if(csvData.length > 0) {
    ss.getSheetByName('CONDUIT_RUNS_REVIT')
      .getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
  } else
    throw 'Blank file';
}

CodePudding user response:

When I saw your provided CSV data, it seems that the delimiter is \t. And, between the column "C" and "D" has no delimiter like "4""". When this is reflected in your script, how about the following modification?

From:

var csv = file.next().getBlob().getDataAsString();
var csvData = Utilities.parseCsv(csv);

To:

var csv = file.next().getBlob().getDataAsString().replace(/"""/g, '"\t""');
var csvData = Utilities.parseCsv(csv, "\t");
  • In this case, the column "D" is the empty column. If you want to remove this empty column, please modify it as follows.

    • To:

        var csv = file.next().getBlob().getDataAsString().replace(/"""/g, '"');
        var csvData = Utilities.parseCsv(csv, "\t");
      

Reference:

  • Related