在Google表格中的标题大小写中大写单元格 [英] Capitalizing cells in Title Case in a Google Sheets

查看:237
本文介绍了在Google表格中的标题大小写中大写单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要大写输入到单元格中的每个单词的第一个字母.我对编程一无所知.

I want to capitalize the first letter of each word that gets entered into cells. I don't know anything about programming.

我发现以下脚本很接近,但是此脚本使每个单词都大写,这不是我想要的:

I found the following script which is close but this script CAPITALIZES EVERY WORD which isn't what I'm looking for:

function onEdit(e) {
if (typeof e.value != 'object') {
e.range.setValue(e.value.toUpperCase());
}
}

推荐答案

如果您仅需要调整信息的帮助(如上一个答案中已提供),则为:

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".

如果您希望仅大写首字母大写而只保留首字母大写,而每个单词的其余部分保持不变,则可以删除上面函数中的 .toLowerCase()部分.然后,文本一只大棕熊"将变成一只大棕熊".

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".

titleCase 函数取自此答案.

编辑:要跳过大写单词的部分,以撇号开头,您可以用此函数替换原始的 titleCase 函数,该函数引入了针对-单词撇号:

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:

function titleCase(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".

编辑2 : 要将此规则仅应用于某些列,可以使用以下版本替换 onEdit 函数:

EDIT 2: To apply this rule only to certain columns, you may replace the onEdit function with this version:

function onEdit(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));
  }
}

这篇关于在Google表格中的标题大小写中大写单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆