Skip to content

Instantly share code, notes, and snippets.

@neufeldtech
Created September 20, 2015 16:55
Show Gist options
  • Save neufeldtech/8d30684eac87fe7c4dca to your computer and use it in GitHub Desktop.
Save neufeldtech/8d30684eac87fe7c4dca to your computer and use it in GitHub Desktop.
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Extra Format')
.addItem('Horizontal', 'formatHorizontal')
.addItem('Vertical', 'formatVertical')
.addToUi();
}
function formatHorizontal() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var cellRange = sheet.getActiveSelection();
var numRows = cellRange.getNumRows();
var numCols = cellRange.getNumColumns();
for (var i = 1; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++) {
var cell = cellRange.getCell(i,j);
var cellValue = cell.getValue();
var newValue = "";
for (var c = 0; c < cellValue.length; c++) {
Logger.log("index: " + c + " " + cellValue[c]);
var char = cellValue[c];
var newChar = char.replace("\n","");
newValue += newChar;
} //end iterating thru letters in word
cell.setValue(newValue);
} //end for numColumns
} //end for numRows
}
function formatVertical() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var cellRange = sheet.getActiveSelection();
var numRows = cellRange.getNumRows();
var numCols = cellRange.getNumColumns();
for (var i = 1; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++) {
var cell = cellRange.getCell(i,j);
var cellValue = cell.getValue();
var newValue = "";
for (var c = 0; c < cellValue.length; c++) {
//Logger.log(cellValue[c]);
if (c == cellValue.length - 1) {
newValue += cellValue[c];
} else {
newValue += cellValue[c]+"\n"
}
} //end iterating thru letters in word
cell.setValue(newValue);
} //end for numColumns
} //end for numRows
}
onOpen();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment