【问题标题】:How can I add hyperlinks in a array_ref in Spreadsheet::WriteExcel?如何在 Spreadsheet::WriteExcel 的 array_ref 中添加超链接?
【发布时间】:2011-03-01 18:33:08
【问题描述】:

我想像

一样将数组引用写入 Excel
@eec =  ( 
                ['maggie', 'milly', 'molly', 'may'  ], 
                [13,       14,      15,      16     ], 
                ['shell',  'star',  'crab',  'stone'] 
         ); 

$worksheet->write_col('A1', \@eec); 

但我的问题是我还想嵌入超链接

我知道如何将超链接添加到特定标签,例如,

$worksheet->write('A4',  'http://www.perl.com/', 'Perl', $format); 

但是我应该怎么做才能显示超链接让我们说 标签'may'?

更多详情请参阅Example of how to use the Spreadsheet::WriteExcel module to write 1D and 2D arrays of data

【问题讨论】:

    标签: perl excel spreadsheet


    【解决方案1】:

    Spreadsheet::WriteExcel 中的 write() 方法是许多其他方法的 syntactic wrapper

    一般来说,如果它不符合您的要求,有两种补救措施:使用您明确需要的包装方法或修改 write() 方法的行为以满足您的需要。

    如果是第一种情况,您只需要展开 write() 为嵌套数据提供的隐式循环。像这样的:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use Spreadsheet::WriteExcel;
    
    my $workbook  = Spreadsheet::WriteExcel->new( 'example.xls' );
    my $worksheet = $workbook->add_worksheet();
    
    
    my @eec = (
        [ 'maggie', 'milly', 'molly', 'may'   ],
        [ 13,       14,      15,      16      ],
        [ 'shell',  'star',  'crab',  'stone' ]
    );
    
    
    my $row = 0;
    
    for my $aref ( @eec ) {
    
        my $col = 0;
    
        for my $token ( @$aref ) {
    
            if ( $token eq 'may' ) {
                $worksheet->write_url( $row, $col, 'http://foo.com', $token );
            }
            else {
                $worksheet->write( $row, $col, $token );
            }
    
            $col++;
        }
    
        $row++;
    }
    
    __END__
    

    在第二种情况下,您可以使用 add_write_handler() 方法修改 write()。看看documentationexamples。这是稍微高级的类别,所以如果您是 Spreadsheet::WriteExcel 的新手,只需使用上述更简单的方法。

    【讨论】:

    • P.S.玛吉、米莉、莫莉、梅。有人吗?
    • 非常感谢您的建议。我也在考虑与您在第一个补救措施中给出的相同解决方案,但我也会尝试为该问题制定处理程序。
    • @Nikhil-Jain 您需要小心使用 add_write_handler() 和 write_url() ,因为最终可能会出现无限递归。稍后我将发布一个小的工作示例。
    【解决方案2】:

    Spreadsheet::WriteExcel 中的 AFIU 没有用于格式化 URL 以供以后编写的方法,因此最简单的方法是在数组中添加不带标签的 URL:

    @eec =   (
                    ['maggie', 'milly', 'molly', 'http://www.perl.com/'  ],
                    [13,       14,      15,      16     ],
                    ['shell',  'star',  'crab',  'stone'],
                );
    

    或使用$worksheet->write_url 方法覆盖特定单元格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 2015-06-23
      • 1970-01-01
      • 2010-11-13
      • 2010-10-06
      • 1970-01-01
      相关资源
      最近更新 更多