Last active
December 27, 2019 04:00
-
-
Save dDondero/4ea6a74da90b064a734b to your computer and use it in GitHub Desktop.
Miss text to columns functionality (ex. .csv file to rows) in Google Apps. Here is a script that creates an extra menu with a few useful functions. Published by Evan Plaice on StackExchange. I share it here because it was very useful to me and hopefully more will find it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var menuEntries = []; | |
menuEntries.push({ name:"Text to columns", functionName:"textToColumns" }); | |
menuEntries.push({ name:"Text to columns (custom separator)", functionName:"textToColumnsCustom" }); | |
menuEntries.push(null); | |
menuEntries.push({ name:"Columns to Text", functionName:"columnsToText" }); | |
menuEntries.push({ name:"Columns to Text (custom separator)", functionName:"columnsToTextCustom" }); | |
ss.addMenu("Advanced", menuEntries); | |
} | |
function textToColumnsCustom() { | |
var separator = Browser.inputBox("Text to column","Enter the the separator",Browser.Buttons.OK); | |
textToColumns(separator); | |
} | |
function columnsToTextCustom() { | |
var separator = Browser.inputBox("Column to text","Enter the the separator",Browser.Buttons.OK); | |
columnsToText(separator); | |
} | |
// Expands a single cell of CSV formatted text to multiple columns | |
function textToColumns(separator) { | |
var sep = typeof(separator) !== 'undefined' ? separator : ','; | |
var ss = SpreadsheetApp.getActiveSheet(); | |
var r = ss.getActiveRange(); | |
// check that only one column was selected | |
var col = r.getColumn(); | |
if(col !== r.getLastColumn()) { | |
Browser.msgBox("Error", "Invalid selection, too many columns.", Browser.Buttons.OK); | |
return; | |
} | |
var firstRow = r.getRow(); | |
// short cut the one row selection | |
if(firstRow === r.getLastRow()) { | |
var values = r.getValues().toString().split(sep); | |
ss.getRange(firstRow,col+1,1,values.length).setValues(new Array(values)); | |
return; | |
} else { | |
var rows = r.getValues(); | |
var values = []; | |
var cols = 0; | |
for(var i = 0, len = rows.length; i < len; i++) { | |
var rowValues = rows[i].toString().split(sep); | |
var rowValuesLen = rowValues.length; | |
if(cols < rowValuesLen) { cols = rowValuesLen; } | |
values.push(rowValues); | |
} | |
// set all values at once (padding required because setValues doesn't accept jagged 2d arrays) | |
padRow(values, cols); | |
ss.getRange(firstRow,col+1,values.length,cols).setValues(values); | |
} | |
} | |
// Pads a row with empty values to the specified length | |
function padRow(array, length) { | |
for(var i = 0; i < array.length; i++) { | |
var arrLen = array[i].length; | |
if(arrLen < length) { | |
var padLen = length - arrLen; | |
var padding = new Array(padLen); | |
array[i].push.apply(array[i], padding); | |
for(var j = 0, len = array[i].length; j < len; j++) { | |
if(typeof(array[i][j]) === 'undefined') { | |
array[i][j] = ""; | |
} | |
} | |
} | |
} | |
return array; | |
} | |
function columnsToText(separator) { | |
var sep = typeof(separator) !== 'undefined' ? separator : ','; | |
var ss = SpreadsheetApp.getActiveSheet(); | |
var r = ss.getActiveRange(); | |
var col = r.getColumn(); | |
var firstRow = r.getRow(); | |
var rows = r.getValues(); | |
var values = []; | |
for(var i = 0, len = rows.length; i < len; i++) { | |
var value = rows[i].join(sep); | |
values[i] = [value]; | |
} | |
col -= 1; | |
ss.getRange(firstRow,col,values.length,1).setValues(values); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment