Cheng
Cheng

Reputation: 770

How to concatenate a whole row of values with a symbol to separate them?

I have a spreadsheet with a many values and wish to create 1 string for each row but concatenating the values together but with each separated by a symbol, such as a ";".

For example, if row 1 consists of:

1 | abc | 23 | test

I want to create a string as follows "1;abc;23;test" using apps script. I know I can easily do this with a for loop, but is there a faster way to do this?

Thanks!

Upvotes: 1

Views: 1076

Answers (1)

St3ph
St3ph

Reputation: 2288

For me the faster way is to do that in spreadsheet with function JOIN(), e.g. =join(";",A1:E1) And you expand the formula to the bottom

With apps script with a for loop

function join(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = sheet.getDataRange().getValues();
  for(var i = 0; i<data.length;i++){
    Logger.log(data[i].join(";"));
  }
}

Stéphane

Upvotes: 1

Related Questions