Search code examples
bashshellperlunixperl-module

write_rich_string with dynamic values (Perl)


I am pretty much new to perl and i am put into a new perl project . Me being a C developer had a few hiccups initially, but somehow managed to fix the code expect for the below logic.

I am using write_rich_string in order to do different formatting within the same cell. Here is the snippet of the code where i am getting the error.

$worksheet->write_rich_string(0,0,$format1,$text1,$format2,$text2,$wrap);

the code is working great until one of the text1 or text2 comes as null. Is there any way i can handle the null inside the write_rich_string()?

Please note i have 7 texts with its corresponding formats to be inserted. I have just shown 2 texts.

Request your assistance guys. Please help.

Update 2 : I also tried the below approach such that i can concatenate the Worksheet format(Red colour font cell) with the string in a variable and put it inside the write_rich_string() function . But that gives the below message and the excel row has the below message instead.

if ($mdr_count == 1) { $var.="$lav_cell,$mrd"; }
if ($lwd_count == 1) { $var.="$red_cell,$lwd"; }
print "The value is ${var}";

$worksheet->write_rich_string($row,$pos,$var);

Output :

**Excel::Writer::XLSX::Format=HASH(0x1427aac)**,Provide Missing Required **DataExcel::Writer::XLSX::Format=HASH(0x1426e7c)**,Loan is late by more than 30 days

It just converted the format into a string and pasted that in the excel row.


Solution

  • On top of my mind you can have an array with the string (text) that you want to enter in the spreadsheet. Search the array for undef elements and change them to NULL strings.

    Sample of working code based on the code that you provide us.

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Excel::Writer::XLSX;
    
    # Create a new Excel workbook
    my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );
    
    # Add a worksheet
    my $worksheet = $workbook->add_worksheet();
    
    #  Add and define a format
    my $format1 = $workbook->add_format();
    $format1->set_bold();
    $format1->set_color( 'red' );
    $format1->set_align( 'center' );
    
    #  Add and define a format
    my $format2 = $workbook->add_format();
    $format2->set_bold();
    $format2->set_color( 'blue' );
    $format2->set_align( 'center' );
    
    #  Add and define a format
    my $format3 = $workbook->add_format();
    $format3->set_bold();
    $format3->set_color( 'orange' );
    $format3->set_align( 'center' );
    
    # Search array and replace undef element(s)
    my @text = ('string1', undef, 'string3');
    defined or $_ = "NULL" for @text;
    # Or simply empty string
    # defined or $_ = "" for @text;
    # Or simply white space
    # defined or $_ = " " for @text;
    
    # Write some strings with multiple formats.
    my ($row, $col) = 0;
    $worksheet->write_rich_string( $row, $col,
                       $format1, $text[0],
                       $format2, $text[1],
                       $format3, $text[2] );
    

    Does this work for you?

    Looking forward to your reply, BR.