One of the tools I write at $work is a larger application with a front-end in Excel. As that front end does some SQL queries against some views, I want to maintain a list of all views that are actually used by the front-end. The following snippet extracts all portions of the code that somewhat look like SQL (owing to the fact that I write the SQL in a very predictable way).
use Win32::OLE; # ... my $excel_source = 'C:\Path\To\Some_workbook.xls'; my $excel = Win32::OLE->CreateObject('Excel.Application'); my $wb = $excel->Workbooks->Add($excel_source); for my $source (in ($wb->{VBProject}->{VBComponents}) ){ print $source->Name, "\n"; my $vb = $source->{CodeModule}; my $linecount = $vb->{CountOfLines}; if( $linecount) { my $body = join "", $vb->Lines(1,$linecount); while ($body =~ /^([^\n]+=\s*"(?:SELECT|INSERT|UPDATE|DELE +TE) (?:[^\n]*_\r?\n)*[^\n]*?)"\r?\n/imsg) { my $matched= $1; $matched =~ s/^\s*'.*$//mg; # remove all VB comments next unless ($matched =~ /\S/); $matched =~ s/.*?\s*=\s*"//im; $matched =~ s/_\r?\n\s*//g; # rewrap continued lines $matched =~ s/"\s*&\s*"//g; # reconcatenate all string +s #print $matched; #print "\n---\n"; if ($matched =~ /(?=from\s+(\w+(\s*,\s*\w+)*))/i) { my $tables = $1; $excel_used{uc $_} = $matched for ($tables =~ /(\w ++)/g); } if ($matched =~ /(?=join\s+(\w+)*)/i) { $excel_used{uc $1} = $matched; } push @used_sql, $matched; } }; }
In reply to Extract the code from a Microsoft Excel file by Corion
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |