简体   繁体   English

列只能在创建的组中隐藏

[英]Columns can be hidden only in groups they were created

I am working on automatization of exporting some data to xlsx-files with Perl, particularly with the module Excel::Writer::XLSX. 我正在使用Perl(特别是使用模块Excel :: Writer :: XLSX)将某些数据导出到xlsx文件的自动化中。 In case some already created columns are empty or irrelevant, I want them to be hidden. 如果某些已经创建的列为空或不相关,我希望将它们隐藏。 While in some cases that was easily done with common command: 尽管在某些情况下,使用通用命令很容易做到:

$worksheet->set_column( 'I:J', undef, undef, 1);

in some particular case they would not disappear as supposed to. 在某些特定情况下,它们不会像预期的那样消失。 After a lot of attempts, it turned out that the problem can be solved by changing the way they are originally set. 经过大量尝试,事实证明可以通过更改最初设置的方式来解决问题。

For example, if I've created them like this: 例如,如果我这样创建它们:

$worksheet->set_column( 'I:I', 40 );
$worksheet->set_column( 'J:M', 60 );
$worksheet->set_column( 'N:N', 40 );

Then command 然后命令

$worksheet->set_column( 'K:N', undef, undef, 1);

will only hide column 'N'. 将仅隐藏列“ N”。

The solution was to create them like that 解决方案是像这样创建它们

$worksheet->set_column( 'J:J', 60 );
$worksheet->set_column( 'K:M', 60 );
$worksheet->set_column( 'N:N', 40 );

So it works, but the code looks stupid and the whole situation just makes no sense to me. 这样就可以了,但是代码看起来很愚蠢,整个情况对我来说毫无意义。 Does anybody know why it happens, and if yes, then is there any other solution to the problem? 有人知道它为什么会发生吗?如果是,那么还有其他解决方法吗?

The reason for the strange behaviour is that set_column() doesn't handle the ranges like sets. 出现异常行为的原因是set_column()无法处理类似于set的范围。 For example if you set a column range for A:F and then another for C:D then you don't automagically set 3 ranges ( A:B , C:D , E:F ). 例如,如果您为A:F设置了一个列范围,然后为C:D设置了另一个范围,则您不会自动设置3个范围( A:BC:DE:F )。 So you need to do that split manually. 因此,您需要手动进行拆分。

In your case it would be better to use the numeric range to set_column() like: 在您的情况下,最好将数字范围用于set_column()例如:

$worksheet->set_column( 8, 8, 40 );

# Instead of:
$worksheet->set_column( 'I:I', 40 );

I'd suggest setting up an initial array of arrays (or hashes) with the column widths you will use for each column and then overwriting for the ones you want to hide, and finally looping over array and calling set_column() for each column. 我建议设置一个数组(或散列)的初始数组,该数组将使用每列的列宽,然后覆盖要隐藏的列宽,最后遍历数组并为每列调用set_column()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM