google sheets transpose new line to table

Code to Transpose New Line to Table in Google Sheets

function transposeNewLineToTable(inputString) {
  var dataArray = inputString.split('\n').map(row => row.split('\t'));
  var numRows = dataArray.length;
  var numCols = dataArray[0].length;
  var outputArray = [];
  for (var j = 0; j < numCols; j++) {
    var tempArray = [];
    for (var i = 0; i < numRows; i++) {
      tempArray.push(dataArray[i][j]);
    }
    outputArray.push(tempArray);
  }
  var outputString = outputArray.map(row => row.join('\t')).join('\n');
  return outputString;
}

Explanation:

  1. Function Definition:
  2. The transposeNewLineToTable function takes an input string as a parameter.

  3. Splitting the Input String:

  4. The input string is split into an array of rows using the split('\n') method, and then each row is split into an array of cells using the map function and split('\t').

  5. Determining Number of Rows and Columns:

  6. The number of rows and columns in the data array is calculated using the length property.

  7. Transposing the Data:

  8. Two nested loops are used to iterate through the rows and columns of the data array and construct a new transposed array called outputArray.

  9. Joining the Transposed Array:

  10. The transposed array is then converted back to a string format by mapping each row to a string with cells joined by tabs, and then joining the rows with new line characters.

  11. Returning the Transposed String:

  12. The transposed string is returned as the output of the function.