Capitalizing Cells In Title Case In A Google Sheets
Solution 1:
In case you just need help with adapting the information, already provided in the previous answer, then here it is:
function onEdit(e) {
if (typeof e.value != 'object') {
e.range.setValue(titleCase(e.value));
}
}
function titleCase(str) {
return str.toString().split(/\b/).map(function(word) {
return word ? word.charAt(0).toUpperCase() + word.slice(1).toLowerCase() : '';
}).join('');
}
This will explicitly convert the text of any updated cell to Title Case, e.g. "a BIG brown bear" becomes "A Big Brown Bear".
If you wish to capitalize the initial letters only and leave the remainder of each word untouched, you may delete the .toLowerCase() part in the function above. Then the text "a BIG brown bear" will become "A BIG Brown Bear".
The titleCase function was taken from this answer.
EDIT: To skip capitalizing parts of words, conaining an apostrophe, you can replace the original titleCase function with this one, which introduces a specific work-around for in-word apostrophes:
functiontitleCase(str) {
var isApostrophe = false;
return str.toString().split(/\b/).map(function(word) {
var result = word ? (!isApostrophe ? word.charAt(0).toUpperCase() : word.charAt(0).toLowerCase()) + word.slice(1).toLowerCase() : '';
isApostrophe = (word == "'");
return result;
}).join('');
}
So, now the text "there's a 'BIG' brown bear" will be transformed into "There's A 'Big' Brown Bear".
EDIT 2: To apply this rule only to certain columns, you may replace the onEdit function with this version:
functiononEdit(e) {
if (typeof e.value != 'object') {
if ([3, 5, 10, 11, 12, 13].indexOf(e.range.columnStart)<0) return;
e.range.setValue(titleCase(e.value));
}
}
Post a Comment for "Capitalizing Cells In Title Case In A Google Sheets"