voram has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,



I have just started using Spreadsheet::WriteExcel.



I am facing a problem where the text 801495E40 is automatically converted by excel into 8.01495E+45.



I tried to define a format for the cell and assigned it to the entire column like this:

my $textFormat = $workbook->add_format( num_format => 'Text' ); $detail->set_column('A:D', undef, $textFormat);

However, formatting the cell as above, formats it to FormatCell >> Number >> Custom >> Text type, not to the FormatCell >> Number >> Text type (which I feel would solve the problem).



Do let me know if there is any way that I can stop Excel from formatting tokens like 801495E40 (in regex terms [0123456789]+E[0123456789]+ ) to the scientific type like 8.01495E+45 considering E as an exponential factor



Thanks !

Megh

Replies are listed 'Best First'.
Re: WriteExcel : The text 801495E40 turns into 8.01495E+45
by runrig (Abbot) on Jan 16, 2014 at 23:23 UTC
    I don't see where 'Text' is a valid number format...and don't use a number format for a text column anyway...try 'write_string()'. write() will use write_number() if the value 'looks like' a number.
      Thanks runrig!

      My issue is that except for the four columns from A-D (which are string), all the other ones are numbers. And since I am populating these cells from a csv file, I have automated it to be printed with write() method for each cell.

      Hence I was trying to set a format for those 4 columns, but you pointed it right, 'Text' cannot be a number format indeed.
      <\br> For now, I have considered these as special cases and using regex i identify the tokens with such formats (like 801495E40) and print them using write_string() method.
      <\br> Thanks for the help!
Re: WriteExcel : The text 801495E40 turns into 8.01495E+45
by ww (Archbishop) on Jan 16, 2014 at 23:44 UTC
    Your code doesn't look much like the sample in John McNamarra's doc... where it says:
    add_format(%properties) The "add_format()" method can be used to create new Format objects w +hich are used to apply formatting to a cell. You can either define the properties at creation time via a hash of property values or later v +ia method calls. $format1 = $workbook->add_format(%props); # Set properties at cr +eation $format2 = $workbook->add_format(); # Set properties later See the "CELL FORMATTING" section for more details about Format properties and how to set them.
    And before you drill all the way down to "CELL FORMATTING" read this:
    One problem with the "write()" method is that occasionally data look +s like a number but you don't want it treated as a number. For example +, zip codes or ID numbers often start with a leading zero. If you writ +e this data as a number then the leading zero(s) will be stripped. You + can change this default behaviour by using the "keep_leading_zeros()" method. While this property is in place any integers with leading ze +ros will be treated as strings and the zeros will be preserved. See the "keep_leading_zeros()" section for a full discussion of this issue. More -- ...
    So, while I understand that your problem is not leading zeros but rather, the conversion of text to a number in scientific notation, the docs may help there...

    But here's where your my $textFormat = $workbook->add_format( num_format => 'Text' ); seems awry: you don't want to set a number format; you want simple text, if I've understood your dilemma correctly.

    See the note about "Cell notation". The $format parameter is optional In general it is sufficient to use the "write()" method. rite_string($row, $column, $string, $format) Write a string to the cell specified by $row and $column: $worksheet->write_string(0, 0, 'Your text here' ); $worksheet->write_string('A2', 'or here' );
    So unless the conversion is occuring as you READ the source data, it would appear that further reading in the doc (ie, perldoc Spreadsheet::WriteExcel will help you solve this issue.

    Alternately, you might want to prepend a single-quote to the $var ($var presumed here) you're trying to write to the new sheet.

    Come, let us reason together: Spirit of the Monastery
      Hi ww, Reading the docs further made things quite clear for me.
      Although an alternate solution to prepend the $var with a single quote did the work for me, I didnt feel it wise to use that way as the excel couldn't be used for further automation (which if I plan to do in future would provide me tokens like 801495E40 as '801495E40 which is not advisable).
      Thanks for your help though ! :)
Re: WriteExcel : The text 801495E40 turns into 8.01495E+45
by Kenosis (Priest) on Jan 17, 2014 at 00:42 UTC

    Use write_string():

    use strict; use warnings; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new('test.xls'); my $worksheet = $workbook->add_worksheet(); my $col = my $row = 0; $worksheet->write_string( $row, $col, '801495E40' );

    Edit: My apologies, but just now noticed that runrig already mentioned this.

Re: WriteExcel : The text 801495E40 turns into 8.01495E+45
by Laurent_R (Canon) on Jan 16, 2014 at 23:25 UTC
    Well, is "801495E40" a number or some string? If it is a number, most programming languages that I know will proceed with a similar conversion to the internal floating number format. Then, the rendering of the number may be changed by various functions (sprintf, (un)pack, etc.)