Alexasks
Alexasks

Reputation: 113

Conversion of csv to google sheet splitting data in cells

I have used the script which is available on here which helped with converting a csv file to sheets, Google App Script Import CSV to Google Sheets

The slight problem that is occurring currently is where there is multiple lines under each other in one cell but during the conversion it is splitting the data rather than keeping in the same format / cell

I hope I have explained this is ok

Thanks Alex

Upvotes: 0

Views: 114

Answers (1)

TheWizEd
TheWizEd

Reputation: 8606

You need to replace all occurances of new line \n with " ". It does not affect the end of line character.

Test sheet

enter image description here

function test() {
  try {
    let file = DriveApp.getFilesByName("Tests - Sheet1.csv").next();
    let blob = file.getBlob();
    let string = blob.getDataAsString();
    string = string.replace(/\n/g," ");
    console.log(string);
  }
  catch(err) {
    console.log(err);
  }
}

Execution log

9:02:44 AM  Info    before:
9:02:44 AM  Info    Id,Name,"Date
Hello",,TRUE,,,,,,
1,A,1/1/2020,,,,,,,,
2,B,2/2/2020,,,,,,,,
3,C,3/3/2021,,,,,,,,
4,D,4/4/2022,,,,,,,,
5,E,6/6/2022,,,,,,,,
9:02:44 AM  Info    after
9:02:44 AM  Info    Id,Name,"Date Hello",,TRUE,,,,,,
 1,A,1/1/2020,,,,,,,,
 2,B,2/2/2020,,,,,,,,
 3,C,3/3/2021,,,,,,,,
 4,D,4/4/2022,,,,,,,,
 5,E,6/6/2022,,,,,,,,
9:02:44 AM  Notice  Execution completed

Upvotes: 1

Related Questions