Created
October 1, 2021 07:09
-
-
Save mengwong/d5e6bb36ae939f0a623bf53978da3107 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// usage: select a range to titlecase. go to tools / script edit. run myFunction. | |
function myFunction() { | |
var selection = SpreadsheetApp.getActiveSpreadsheet().getSelection(); | |
var ranges = selection.getActiveRangeList().getRanges(); | |
for (var i = 0; i < ranges.length; i++) { | |
Logger.log('Active Ranges: ' + ranges[i].getA1Notation()); | |
var range = ranges[i]; | |
for (var y = 1; y <= range.getHeight(); y++) { | |
for (var x = 1; x <= range.getWidth(); x++) { | |
var cell = range.getCell(y, x); | |
var v = cell.getValue(); | |
if (v === undefined) return; | |
cell.setValue(titleCase(v)); | |
} | |
} | |
} | |
} | |
function titleCase(str) { | |
var splitStr = str.toLowerCase().split(' '); | |
for (var i = 0; i < splitStr.length; i++) { | |
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1); | |
} | |
return splitStr.join(' '); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment