I have this script that replaces line breaks for a comma and space and triggered by edit event. However, it only replaces one line break per edit event. My sheet is populated by a form (not Google Forms) and in three columns I always receive up to four line breaks in cells of columns M, N and O (result of a multiple-choice fields in the form).
function testReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSheet()
var range = sheet.getRange("M2:O");
replaceInSheet(sheet,'values','test');
}
function replaceInSheet(sheet, to_replace, replace_with) {
//get the current data range values as an array
var values = sheet.getRange("M2:O").getValues();
//loop over the rows in the array
for(var row in values){
//use Array.map to execute a replace call on each of the cells in the row.
var replaced_values = values[row].map(function(original_value){
return original_value.toString().replace("\n",", ");
});
//replace the original row values with the replaced values
values[row] = replaced_values;
}
//write the updated values to the sheet
sheet.getRange("M2:O").setValues(values);
}
With this code I have to edit my sheet every time I need to remove a line break so it only replaces one line break at a time. How do I replace this: COLUMN M, N, O:
word1
word2
word3
word4
into this:
word1, word2, word3, word4
without having to edit my sheet three times?
I already set the trigger in three ways: "on edit", "on change", and "on form submission" but that does not remove all line breaks after form submission.