Perl script to merge cells in column1 which has same strings, for all sheets in a excel workbook


 
Thread Tools Search this Thread
Top Forums Programming Perl script to merge cells in column1 which has same strings, for all sheets in a excel workbook
# 8  
Old 08-21-2015
I also want the empty cells to be merged with the respective string.

---------- Post updated at 09:57 PM ---------- Previous update was at 09:23 PM ----------

As of now it is removing duplicates as expected but I also want the empty cells to be merged to their respective string.
# 9  
Old 08-21-2015
JB,

Can you provide an example of what you mean by "empty cells to be merged to their respective string".

- DL
# 10  
Old 08-22-2015
Code:
As of now the excel first column looks like this:
----------|
abs-pq      |
----------|
----------|
----------|
abs-pqrst |
----------|
----------|
tbs-pqrst |
----------|
----------|

I want output like this on both col1 and 2, what should i add to the code:
----------|
abs-pq      |
          |
          |
----------|
abs-pqrst |
          |
----------|
tbs-pqrst |
          |
----------|

---------- Post updated at 06:35 PM ---------- Previous update was at 08:12 AM ----------

have used below in the code, looks like it is working but when i open excel i am getting some data may have lost error. Am i doing it in right way?
Code:
my $format = $workbook->add_format(
                                    border  => 6,
                                    #bold    => 1,
                                    color   => 'black',
                                    bg_color => 0x32,
                                    align   => 'left',
                                    valign => 'vcenter',
                                    #center_across => 1,
                                    );
my $format1_merged = $workbook->add_format(
                                    border  => 6,
                                    );

Code:
while ($col < @currRow) {
            last if @prevRow < $col || $currRow[$col] ne $prevRow[$col];
           #### $worksheet->write($row, $col, '',$format1);
            $worksheet->merge_range($row, $col, $row-1, $col, '', $format1_merged);
            $col++;
        }
        while ($col < @currRow) {
            $worksheet->write($row, $col, $currRow[$col],$format);
            $col++;
        }


Last edited by Jack_Bruce; 08-22-2015 at 04:08 PM..
# 11  
Old 08-24-2015
Anyone have anyother better solution to merge cells?
# 12  
Old 08-25-2015
Code:
#! /usr/bin/perl

use strict;
use warnings;
use Spreadsheet::WriteExcel;

my $file = shift @ARGV;
my $workbook = Spreadsheet::WriteExcel->new($file) or die "can't create worksheet: $!";

my $dest = shift @ARGV;
chdir $dest or die "no such directory: $!";
opendir my $dh, '.' or die "can't open directory: $!";
my @files = sort grep { m{^[^.]} } readdir($dh);
close $dh; 

foreach my $file (@files) {
    open my $in, '<', $file or die "Could not open file: $!";

    $file =~ s{\..*$}{};

    my @prevRow = ();
    my $row     = 0;
    my @sheet   = undef;

    while(<$in>) {
        chomp;
        my @currRow  = split(',', $_);
        my $col = 0;

        while ($col < @currRow) {
            last if @prevRow <= $col || $currRow[$col] ne $prevRow[$col];
        $sheet[$col][$row] = undef;
            $col++;
        }

        while ($col < @currRow) {
        $sheet[$col][$row] = $currRow[$col];
            $col++;
        }

        @prevRow = @currRow;
        $row++;
    }

    close $in;

    my $worksheet = $workbook->add_worksheet($file);

    foreach my $col (0 .. $#sheet) {
        my $R     = $sheet[$col];
        my $nRows = scalar @$R;
        my $span  = 1;

        $row = 0;

        while ($row < $nRows) {
            unless (defined $R->[$row]) {
                $span++;
                next;
            }

            if (1 < $span) {
                $worksheet->merge_range($row - $span, $col, $row - 1, $col);
                $span = 1;
            }

            $worksheet->write($row, $col, $R->[$row]);
        } continue {
            $row++;
        }

        if (1 < $span) {
            $worksheet->merge_range($row - $span, $col, $row, $col);
        }
    }
}

Use the merge_range method to merge rows.

@sheet is populated with the worksheets data so that undefined cells can be counted for the merge_range method.

Note that $worksheet->write($row, $col, undef) writes a blank cell while $worksheet->write($row, $col, '') writes a cell with an zero length string - ISBLANK() would return FALSE.
This User Gave Thanks to derekludwig For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Perl script for identifying dupes in column1

Dear all, I have a large dictionary database which has the following structure source word=target word e.g. book=livre Since the database is very large in spite of all the care taken, it so happens that at times the source word is repeated e.g. book=livre book=tome Since I want to... (7 Replies)
Discussion started by: gimley
7 Replies

2. Shell Programming and Scripting

Write two csv files into one excel with multiple sheets

I have requirement to write two CSV files to one single excel with multiple sheets. Data present in the two files should sit in excel as different sheets. How can we achieve this using shell script? 1.csv 2. csv 1,2,3,4 5,6,7,8 XXXXX YYYYY Res.excel 1.csv data... (1 Reply)
Discussion started by: duplicate
1 Replies

3. Shell Programming and Scripting

Perl script to Merge contents of 2 different excel files in a single excel file

All, I have an excel sheet Excel1.xls that has some entries. I have one more excel sheet Excel2.xls that has entries only in those cells which are blank in Excel1.xls These may be in different workbooks. They are totally independent made by 2 different users. I have placed them in a... (1 Reply)
Discussion started by: Anamika08
1 Replies

4. Shell Programming and Scripting

Merge two cells in excel via UNIX?

Hi UNIX Gods! Is it possible to merge two cells in .csv file using unix commands? Imagine that this is my present csv file opened via excel: Gate Reports| | fatal alerts | 200 | is is possible to make it look like this using unix? Gate Reports | fatal... (1 Reply)
Discussion started by: 4dirk1
1 Replies

5. Shell Programming and Scripting

modify Existing MS excel workbook in perl

Hi I need to modify an excel file in perl and for which I installed perl in Linux 1. Open a existing excel file 2. delete an unwanted Sheet called "summary" 3. and i want to insert some data into range of cells ( B1:B11) 4. Remove unwanted value called "Sum" repeated in the... (1 Reply)
Discussion started by: luke_devon
1 Replies

6. Shell Programming and Scripting

Sending SQL Queries output to different Excel sheets

Hi, I need your help in sedning sql queries output to different excel sheets. My requirement is like this: Query1: Select name from table1 where status = 'Complete' Query2: Select name from table1 where status = 'Failed' Query3: Select name from table1 where status = 'Ignored' ... (4 Replies)
Discussion started by: parvathi_rd
4 Replies

7. Shell Programming and Scripting

PERL: Split Excel Workbook to Indiv Excel files

Hi, I am trying to find a way to read an excel work book with multiple worksheets. And write each worksheet into a new excel file using perl. My environment is Unix. For example: I have an excel workbook TEST.xls and it has Sheet1, Sheet2, Sheet3 worksheets. I would like to create... (2 Replies)
Discussion started by: sandeep78
2 Replies

8. Shell Programming and Scripting

How to format excel sheets in UNIX??

Hi, I have generated an excel sheet using a shell script. i have converted the output text file to an excel and got the desired output. However, in a particular column in the excel the values of the numbers start with 0. e.g. 078393343, 00342442, etc. But, in the resulting excel I get as... (2 Replies)
Discussion started by: Vijay06
2 Replies

9. Shell Programming and Scripting

Multiple excel work sheets through UNIX

Hi, There is this requirement to create multiple work sheets in an MS Excel file through UNIX. We normally can create one work sheet in unix by either tab or comma delimiting and appending .xls or .csv to the file name, but can we create multiple work sheets. Regards, Puspendu (1 Reply)
Discussion started by: puspendu
1 Replies

10. Programming

creating more than 2 excel sheets in C

Hi, I have a question about using C to write excel file. There is a way to write into a sheet into a file excel, like this: FILE * File; pMyFile = fopen("test.xls", "w+"); fprintf(File, "\n\n"); /* go to the row 2 of the column A*/ fprintf(File, "%s", "Hi pippo!"); /* write... (7 Replies)
Discussion started by: manceryder
7 Replies
Login or Register to Ask a Question