gpt4 book ai didi

excel - 使用 perl 在同一工作表中的直方图

转载 作者:行者123 更新时间:2023-12-04 20:22:18 25 4
gpt4 key购买 nike

我试图在与数据表相同的工作表上绘制直方图。下面是我正在使用的 perl 脚本。我在这里面临的问题是创建了多个图表工作表,但我希望图表与数据表位于同一个工作表中。

use strict;
use warnings;
use Excel::Writer::XLSX;
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);

{
my $output_fn = 'result.xlsx';
my $input_fn = 'accuracy_final.txt.gz';
my $workbook = Excel::Writer::XLSX->new( $output_fn );
my $worksheet = $workbook->add_worksheet();
my $zip = IO::Uncompress::Gunzip->new( $input_fn )
or die "gunzip failed: $GunzipError\n";

my $col = 0;
my $row = 1;
while (!$zip->eof()) {
my $line = $zip->getline();
chomp($line);
next if $line !~ /\S/; # skip empty lines
my $value = $line;
$worksheet->write( $row, $col, $value );
$row++;

# Add a worksheet chart.
my $chart = $workbook->add_chart( type => 'line' );

# Configure the chart.
$chart->add_series(
values => '=Sheet1!$A$2:$A$5000',
);
}
$workbook->close();
}
预期输出如下所示:
enter image description here
输出我得到:
enter image description here

最佳答案

以下对我有用。您需要使用 insert_chart() 方法将图表添加到工作表。同时设置embedded文档中描述的图表选项:

use feature qw(say);
use strict;
use warnings;
use Excel::Writer::XLSX;
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);

{
my $output_fn = 'result.xlsx';
my $input_fn = 'input.txt.gz';
my $workbook = Excel::Writer::XLSX->new( $output_fn );
my $worksheet = $workbook->add_worksheet();
my $chart = $workbook->add_chart( type => 'line', embedded => 1 );
my $zip = IO::Uncompress::Gunzip->new( $input_fn )
or die "gunzip failed: $GunzipError\n";

my $col = 0;
my $row = 1;
while (!$zip->eof()) {
my $line = $zip->getline();
chomp($line);
next if $line !~ /\S/; # skip empty lines
my $value = $line;
$worksheet->write( $row, $col, $value );
$row++;

}
# Configure the chart.
$chart->add_series(
values => '=Sheet1!$A$2:$A$6',
);
$worksheet->insert_chart( 'B7', $chart );
$workbook->close();
}
enter image description here

关于excel - 使用 perl 在同一工作表中的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69657120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com