Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^17: How to read the value of a variable (read from a Excel file converted Into text file) and put then back into Excel column

by poj (Abbot)
on Feb 26, 2017 at 17:28 UTC ( [id://1182891]=note: print w/replies, xml ) Need Help??


in reply to Re^16: How to read the value of a variable (read from a Excel file converted Into text file) and put then back into Excel column
in thread How to read the value of a variable (read from a Excel file converted Into text file) and put then back into Excel column

As I said, you probably need to add cleaning to specific keywords in the cleanup sub

# specific cleaning for isim usim sim_invalid if ( $kw =~ /^(isim|usim|sim_invalid)/ ){ $value =~ s/(\d+).*/$1/; # remove trailing text }
poj
  • Comment on Re^17: How to read the value of a variable (read from a Excel file converted Into text file) and put then back into Excel column
  • Select or Download Code

Replies are listed 'Best First'.
Re^18: How to read the value of a variable (read from a Excel file converted Into text file) and put then back into Excel column
by rockyurock (Acolyte) on Feb 27, 2017 at 07:35 UTC
    Hi.. Is It possible to keep my search restricted only to the keywords entered in the keywords list as there are some of the keywords exists with duplicate and hence I don;t want to print them in the excel sheet output current e.g.
    signaling_flag_weight 2 # It's appearing as _weight 2 however the key +word Is only signaling_flag=# ran_type_req # It's appearing as _req however the keyword Is only ran +_type=# emergency_indication = 1 #It's appearing as ication however the keywor +d Is emergency_ind=# ipv4_notify_req #It's appearing as _notify_req however the keyword Is + ipv4=# Ipv6FeatureEnable #It's appearing as FeatureEnable however the keyword + only ipv6= #
    (2) Iam Not able to search
    private_uid = rat type = profile.home_uri = profile.imei = profile.private_uid = profile.public_uid = ck =
      Second Part Is ok now as I have modified the keywords ...i..e from private_uid = to private_uid In the excel Input sheet.
        Hi.. I was trying to read the excel file Independent of the Path i.e. from the current directory and I was able to do with help of abs_path () but I am not able to use SaveAs function to save the updated file Into the same current directory In the below program .. can you please help ?
        # get already active Excel application or open new my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); my $Excel_path = 'Parameters.xlsx'; print $Excel_path; #Need to use absolute path for Excel files my $excelfile = abs_path("$Excel_path") or die "Error: the file $Excel +_path has not been found\n"; print $excelfile; # open Excel file #my $dir = "E:\\Automation Related\\Perl Scripts - ALL\\IMS_debugging\ +\Excel\\"; #my $Book = $Excel->Workbooks->Open($dir.$excelfile) my $Book = $Excel->Workbooks->Open($excelfile) or die Win32::OLE->LastError(); # get keywords my $kwhashref = get_keywords($Book); # create regex my $match = join '|',map{quotemeta} keys %$kwhashref; my $re = qr/$match/; #print $re; # search files and update workbook my @files = grep { -f } (<main_log*>); for my $file (@files){ my $line_no = 0; print "Reading $file\n"; open my $fh, '<', $file or die $!; while (<$fh>){ chomp; ++$line_no; s/ +/ /g; # remove multiple spaces # Original # # special pcscf_v4 extract addr # if (/($re).*addr = ([.\d]+)/){ # print "Line $line_no kw=[$1] value=[$2] special\n"; # update_sheet($1,$2); # next; # } # # match more than 1 per line e.g. usim and isim # while (/($re)[ =]*([.\d]+)/g){ # print "Line $line_no kw=[$1] value=[$2]\n"; # update_sheet($1,$2); # } if (/($re).*addr = ([.\da-fA-F:]+)/){ my $msg = update_sheet($1,$2,$_); print "Line $line_no $msg special\n"; next; } # match more than 1 per line e.g. usim and isim while (/($re)[ =]*([^,]+)/g){ my $msg = update_sheet($1,$2,$_); print "Line $line_no $msg\n"; } } close $fh; } #pp $kwhashref; # debug #my$var = 'updated'; # save as new spreadsheet my $temp = cwd(); #$Excel->{DisplayAlerts}=0; # This turns off the "This file already ex +ists" message. #$Book->Save; #Or $Book->SaveAs("C:\\file_name.xls"); #$Book->Close; #or $Excel->Quit; #$Book->SaveAs($dir.'updated_'.$excelfile); # working saveas if path is provided #$Book->SaveAs("E:\\Automation Related\\Perl Scripts - ALL\\IMS_debugg +ing\\Final_Scripts\\Final_Script_Working\\temporary.xlsx"); # working if given directly as file name. under User/recent/ #$Book->SaveAs("temporary.xlsx"); $Book->SaveAs($excelfile.'.xlsx'); $Book->Close; # update sheet # original # sub update_sheet { # my ($kw,$value) = @_; # my ($sht,$r,$c) = @{$kwhashref->{$kw}}[0..2]; # $Book->Worksheets($sht)->Cells($r,$c+1)->{Value} = $value; # $kwhashref->{$kw}[3] = $value; # for debugging # } sub update_sheet { my ($kw,$value,$comment) = @_; my $clean = cleanup($kw,$value); my ($sht,$r,$c) = @{$kwhashref->{$kw}}[0..2]; my $sel = $Book->Worksheets($sht)->Cells($r,$c+1); $sel->Insert({ Shift => -4161}); # xlToRight $sel->{Value} = $clean; $sel->AddComment( { Text=> $comment } ); $sel->Comment->Shape->TextFrame->{ AutoSize } = 1; $kwhashref->{$kw}[3] = $value; # for debugging $kwhashref->{$kw}[4] = $clean; # for debugging return "kw=[$kw] value=[$value] clean=[$clean]"; } # get keywords sub get_keywords { my ($book) = @_;; my %word = (); for my $sht (1..$book->Worksheets->Count){ my $Sheet = $book->Worksheets($sht); # determine position of keywords # $Sheet->UsedRange->Value; my $rng = $Sheet->UsedRange->Address( { ReferenceStyle => -4150 } ); # R1C1:R2C2 my ($r1,$c1,$r2,$c2) = ($rng =~ m/[RC](\d+)/g); $r2 = $r2 || $r1; $c2 = $c2 || $c1; print "Sheet $sht Range $rng r1=$r1 c1=$c1 r2=$r2 c2=$c2\n"; # build keyword hash storing cell position for my $c ($c1..$c2){ for my $r ($r1..$r2){ my $val = $Sheet->Cells($r,$c)->Value || ''; if ($val){ $word{$val} = [$sht,$r,$c]; # sheet row col } } } } return \%word; } # remove unwanted chars from value sub cleanup { my ($kw,$value) = @_; # generic cleansing $value =~ s/[\[\]]//g; # remove all [] brackets $value =~ s/^\s+//g; # remove leading whitespace $value =~ s/\s+$//g; # remove trailing whitespace # specific cleansing #if ($kw =~ //){ # $value =~ s///g #} # specific cleaning for isim usim sim_invalid if ( $kw =~ /^(isim|usim|sim_invalid|pcscf_discovery_flag|signalin +g_flag|EVS payload type|evs_mode_switch|check_nw_im_cn_signaling_flag +|is_ems_support|emergency_ind|ran_type|plmn|VT_SUPPORT|WFC_SUPPORT|ra +t_type:|access RAT|port|integrity_algo|profile.home_uri|profile.imei| +profile.private_uid|profile.public_uid|ua rat)/ ){ $value =~ s/(\d+).*/$1/; # remove trailing text } return $value; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1182891]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2024-04-23 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found