Home > Enterprise >  Break multiple value cell into different rows via Apps Scripts
Break multiple value cell into different rows via Apps Scripts

Time:11-09

I'm looking to split a cell that have multiple values separated by "\n" via App Script. Itried a few functions that were proposed but nothing seemed to work. Below you can see a sample format and desired outcome.

Screenshot of current format versus desired output

Thanks.

CodePudding user response:

I believe your goal is as follows.

  • You want to split each cell value with \n using Google Apps Script.
  • You want to put the split values in the vertical direction.

In this case, how about the following sample script?

Sample script:

function myFunction() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set your sheet name.
  const range = sheet.getRange("A2:A"   sheet.getLastRow());
  const values = range.getValues().flatMap(([a]) => a ? a.split("\n").map(e => [e.trim()]) : []);
  range.offset(0, 1, values.length).setValues(values);
}
  • When this script is run, the source values are retrieved from the column "A". And, the converted values are put in column "B".

Reference:

  • Related