How to convert strings to Proper case in Apps Script?

If you've worked with spreadsheets at a school or a company, you know that data can often be messy and not formatted correctly. One common issue that I've run into is that names are not capitalized correctly.

Google Sheets has a function called PROPER() that converts a string in your spreadsheet to Proper case, but unfortunately, there is no such function in Apps Script.

In this tutorial, you'll learn how to write a custom function to convert a string to Proper case.

/**
 * Convert a string to Proper case.
 
 * @param {string} str The string value to be converted.
 * @return The string value in Proper case.
 * @customfunction
*/
function PROPER_CASE(str) {
  if (typeof str != "string")
    throw `Expected string but got a ${typeof str} value.`;
  
  str = str.toLowerCase();

  var arr = str.split(/.-:?—/ );
  
  return arr.reduce(function(val, current) {
    return val += (current.charAt(0).toUpperCase() + current.slice(1));
  }, "");
}

Conclusion

In this tutorial, you learned how to write a custom function to convert a string to Proper case.

Thanks for reading!

Sign up to be notified when I publish new content

By signing up you agree to the Privacy Policy & Terms.