更改电子表格每个单元格首字母大小写

9 投票
10 回答
5284 浏览
提问于 2025-04-17 09:29

我有很多单元格,想把每个单元格里的第一个字母变成大写。比如说,cook, chef, fireman 变成 Cook, Chef, Fireman

  • 我在使用OpenOffice.org这个电子表格软件,但它似乎只提供了“全部大写”或“全部小写”的选项。
  • 如果OpenOffice.org做不到,我可以在里面编辑,或者把文件导出为CSV格式,然后用BASH脚本来编辑这个CSV文件。

我该怎么把电子表格中每个单元格的第一个字母都变成大写呢?

10 个回答

7

也许你只需要更新到一个更新的版本。我现在用的是LibreOffice 3.4.4,在这里我看到有一个选项是格式 -> 更改大小写 -> 句子大小写,我觉得这个功能正好满足你的需求。

8

我正好在做这个任务。你需要安装两个模块,分别是 Spreadsheet::ParseExcelSpreadsheet::WriteExcel

use strict;
use warnings;

use Spreadsheet::ParseExcel::SaveParser;

my $parser   = Spreadsheet::ParseExcel::SaveParser->new();
my $workbook = $parser->Parse('Book1.xls');

if ( !defined $workbook ) {
    die $parser->error(), ".\n";
}

for my $worksheet ( $workbook->worksheets() ) {

    my ( $row_min, $row_max ) = $worksheet->row_range();
    my ( $col_min, $col_max ) = $worksheet->col_range();

    for my $row ( $row_min .. $row_max ) {
        for my $col ( $col_min .. $col_max ) {

            my $cell = $worksheet->get_cell( $row, $col );
            next unless $cell;

            # "ucfirst lc" make sure that only the first letter is capitalized
            # if you dont like that just remove lc
            $worksheet->AddCell( $row, $col, ucfirst lc $cell->value() );

        }
    }
}

# ofcouse save your work
$workbook->SaveAs('Book2.xls');
3

我有一个awk脚本,可以实现你想要的功能(我觉得是这样的)。

这是我的测试输入文件(test.input):

cook, chef, fireman
cook, chef, fireman
cook, chef, fireman
cook, chef, fireman
house, farm, road

我的awk脚本(up.awk):

# from: http://www.gnu.org/software/gawk/manual/html_node/Join-Function.html
function join(array, start, end, sep, result, i){
         if (sep == "")
            sep = " "
         else if (sep == SUBSEP) # magic value
            sep = ""
         result = array[start]
         for (i = start + 1; i <= end; i++)
            result = result sep array[i]
         return result
}
BEGIN {
    FS="\n";
}
{
    # split input on newline
    for(i=1;i<=NF;i++) {
        # split line on the commas
        size = split($i, s, ",")
        for(ii=1;ii<=size;ii++) {
            # trim whitespace
            gsub(/[[:space:]]*/,"",s[ii])
            # uppercase first char and glue it back together
            s[ii] = toupper(substr(s[ii], 0, 1)) substr(s[ii], 2)
        }
        # join array back and print it out
        print join(s, 1, size, ", ")
    }
}

我运行这个脚本的方法是: awk -f up.awk test.input >test.output

在我的测试输出文件(test.output)中的结果是:

Cook, Chef, Fireman
Cook, Chef, Fireman
Cook, Chef, Fireman
Cook, Chef, Fireman
House, Farm, Road

撰写回答