Multiple worksheet with perl loop from array - excel

I was working with perl code to create a spreadsheet having multiple workshhets using Spreadsheet::WriteExcel. But the loop works only once and breaks. Please help to find what actually happening.
Code
use Spreadsheet::WriteExcel;
my $workbook = new_workbook($r, 'Workbook.xls');
my $worksheet;
my #list = ('bike', 'car', 'bus');
foreach my $name (#list){
$worksheet = $workbook->add_worksheet($name);
# writing excel data ...
}
$workbook->close();

You are declaring the $worksheet variable outside of the loop (1) and then overwrite it in each loop iteration (2). That might lead to complications inside Spreadsheet::WriteExcel if the reference to the first worksheet suddently gets replaced with a new worksheet. Probably the XLS comes out with only the last worksheet.
use Spreadsheet::WriteExcel;
my $workbook = new_workbook($r, 'Workbook.xls');
my $worksheet; # <--- 1
my #list = ('bike', 'car', 'bus');
foreach my $name (#list){
$worksheet = $workbook->add_worksheet($name); # <--- 2
# writing excel data ...
}
$workbook->close();
Instead you should just declare a lexical $worksheet inside the loop where you use it. In general, allways declare variables in the smallest lexical scope in Perl.
foreach my $name (#list) {
my $worksheet = $workbook->add_worksheet($name);
# writing excel data ...
}

Related

Finding if excel sheet contains array using powershell

I'm trying to find out if an excel sheet contains an array (in any cell in the fourth sheet). The variable is a user input as shown:
$j = Read-Host "Enter sensor serial number"
$Sens_name = #("$j")
And the act of it trying to find the input looks like this, where $EPRB2_loca is the location of the excel file including file extension:
#Checking in EPRB 2 file
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open("$EPRB2_loca")
$WorkSheet = $Workbook.Sheets.Item(4)
$WorkSheet.Name
$EPRB2_file = $WorkSheet.Cells.Find("$Sens_name")
if ($EPRB2_file.HasArray -eq $false)
{
$EPRB2_file = $null
}
Can someone please help me figure out why it won't show as it containing the array when I know it does?
I was using the wrong command.
If anyone comes across this, use .Count instead of .HasArray

Powershell Invalid Index Error When Using Excel Cells?

I am currently working on a script where I use several arrays to lookup inventory lists in Excel. After running a few tests I can get my script to read the contents of each cell (thanks to some research from this site!) but I can't use the contents of the cell in a variable. I receive an "Invalid Index" error when I try to switch to a worksheet using contents from one of the cells being read. I've also added an example of how the data is arranged.
#Test array
$array = "Dog", "Cat", "Mouse", "Tiger"
#Location of the Excel file to edit
$FileLoc = "Q:\Cutsheet.xlsx"
#Create Excel Com Object, and display it
$excel = new-object -com Excel.Application
$excel.visible = $true
#Open Workbook
$workbooks = $excel.workbooks.Open($FileLoc)
$worksheets = $workbooks.Worksheets
$worksheet = $worksheets.item("DATA")
#opens inventory workbook
$source = $excel.workbooks.Open("Q:\inventory.xlsx")
$sourceSheets = $source.Worksheets
<#
#This loop will search for match element of the array with a corresponding cell on an excel spreadsheet.
#That cell is grouped with several names of inventory worksheet names. The script will copy each inventory
#and append it to the existing cutsheet.
#>
foreach($element in $array) {
for ($i = 9; $i -lt 20; $i++) {
if ($worksheet.Cells.Item($i, 1).Text -eq $element) {
$j = 2
while ($worksheet.Cells.Item($i, $j).Text -ne "") {
Write-Host $worksheet.Cells.Item($i, $j).Value2
$name = $worksheet.Cells.Item($i, $j).Value2
$sourceSheet = $sourceSheets.Item($name)
$sourceSheet.Copy([system.type]::missing, $worksheets)
$j++
}
}
}
}
Example spreadsheet

In Perl, how can I copy a subset of columns from an XLSX work sheet to another?

I have a .xlsx file (only one sheet) with 15 columns. I want to read some specific columns, let's say columns 3, 5, 11, 14 and write it to a new Excel sheet. In this case some cells of input files are empty means don't have any value.
Here what I am trying:
use warnings;
use strict;
use Spreadsheet::ParseXLSX;
use Excel::Writer::XLSX;
my $parser = Spreadsheet::ParseXLSX->new;
my $workbook = $parser->parse("test.xlsx");
if ( !defined $workbook ) {
die $parser->error(), ".\n";
}
my $worksheet = $workbook->worksheet('Sheet1');
# but from here I don't know how to define row and column range to get specific column data.
# I am trying to get all data in an array, so I can write it in new .xlsx file.
# function to write data in new file
sub writetoexcel
{
my #fields = #_;
my $workbook = Excel::Writer::XLSX->new( 'report.xlsx' );
$worksheet = $workbook->add_worksheet();
my $row = 0;
my $col = 0;
for my $token ( #fields )
{
$worksheet->write( $row, $col, $token );
$col++;
}
$row++;
}
I also followed this Question, but no luck.
How can I read specific columns from .xlsx file and write it into new .xlsx file?
Have you never copied a subset of columns from an array of arrays to another?
Here is the input sheet I used for this:
and, this is what I get in the output file after the code is run:
#!/usr/bin/env perl
use strict;
use warnings;
use Excel::Writer::XLSX;
use Spreadsheet::ParseXLSX;
my #cols = (1, 3);
my $reader = Spreadsheet::ParseXLSX->new;
my $bookin = $reader->parse($ARGV[0]);
my $sheetin = $bookin->worksheet('Sheet1');
my $writer = Excel::Writer::XLSX->new($ARGV[1]);
my $sheetout = $writer->add_worksheet('Extract');
my ($top, $bot) = $sheetin->row_range;
for my $r ($top .. $bot) {
$sheetout->write_row(
$r,
0,
# of course, you need to do more work if you want
# to preserve formulas, formats etc. That is left
# to you, as you left that part of the problem
# unspecified.
[ map $sheetin->get_cell($r, $_)->value, #cols ],
);
}
$writer->close;

Can I create a Excel workbook inside a foreach in perl?

I want to create multiple Excel files. The files will output basically the same format, the only difference the data will be for different years.
If I run the program in the following way it runs and create the excel file without problems:
use warnings;
use Excel::Writer::XLSX;
use Date::Parse;
... ###some validation of the data to work with
... ### put data on hashes
...
my $workbook = Excel::Writer::XLSX->new( "Monitoring_Report_2013.xlsx" );
$worksheet1 = $workbook->add_worksheet('Q1');
$worksheet2 = $workbook->add_worksheet('Q2');
$worksheet3 = $workbook->add_worksheet('Q3');
$worksheet4 = $workbook->add_worksheet('Q4');
... ### create the different tables on each worksheet
...
...
If I add the foreach part so it can creates automatically the differents files to each year it runs but when I tried to open the excel file it generate a corrupt error.
use warnings;
use Excel::Writer::XLSX;
use Date::Parse;
...
...
...
my #years_in_data = ("2012", "2013", "2014");
foreach my $year(#years_in_data)
{
chomp $year;
...
...
...
my $workbook = Excel::Writer::XLSX->new( "Monitoring_Report_$year.xlsx" );
$worksheet1 = $workbook->add_worksheet('Q1');
$worksheet2 = $workbook->add_worksheet('Q2');
$worksheet3 = $workbook->add_worksheet('Q3');
$worksheet4 = $workbook->add_worksheet('Q4');
...
...
...
}
Can I create the files automatically or I need to write each file manually??
Thanks for your help!
Are you calling the $worksheet->write() method for each of these ? Maybe you are saying you do with all the dots. If you don't then I can see none of them getting written.

Speed up reading an Excel File in Powershell

I wonder if there is any way to speed up reading an Excel file with powershell. Many would say I should stop using the do until, but the problem is I need it badly, because in my Excel sheet there can be 2 rows or 5000 rows. I understand that 5000 rows needs some time. But 2 rows shouldn't need 90sec+.
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$Excel.DisplayAlerts = $false
$Path = EXCELFILEPATH
$Workbook = $Excel.Workbooks.open($Path)
$Sheet1 = $Workbook.Worksheets.Item(test)
$URows = #()
Do {$URows += $Sheet1.Cells.Item($Row,1).Text; $row = $row + [int] 1} until (!$Sheet1.Cells.Item($Row,1).Text)
$URows | foreach {
$MyParms = #{};
$SetParms = #{};
And i got this 30 times in the script too:
If ($Sheet1.Cells.Item($Row,2).Text){$var1 = $Sheet1.Cells.Item($Row,2).Text
$MyParms.Add("PAR1",$var1)
$SetParms.Add("PAR1",$var1)}
}
I have the idea of running the $MyParms stuff contemporarily, but I have no idea how. Any suggestions?
Or
Increase the speed of reading, but I have no clue how to achieve that without destroying the "read until nothing is there".
Or
The speed is normal and I shouldn't complain.
Don't use Excel.Application in the first place if you need speed. You can use an Excel spreadsheet as an ODBC data source - the file is analogous to a database, and each worksheet a table. The speed difference is immense. Here's an intro on using Excel spreadsheets without Excel
Appending to an array with the += operator is terribly slow, because it will copy all elements from the existing array to a new array. Use something like this instead:
$URows = for ($row = 1; !$Sheet1.Cells.Item($row, 1).Text; $row++) {
if ($Sheet1.Cells.Item($Row,2).Text) {
$MyParms['PAR1'] = $Sheet1.Cells.Item($Row, 2).Text)
$SetParms['PAR1'] = $Sheet1.Cells.Item($Row, 2).Text)
}
$Sheet1.Cells.Item($Row,1).Text
}
Your Do loop is basically a counting loop. The canonical form for such loops is
for (init counter; condition; increment counter) {
...
}
so I changed the loop accordingly. Of course you'd achieve the same result like this:
$row = 1
$URows = Do {
...
$row += 1
}
but that would just mean more code without any benefits. This modification doesn't have any performance impact, though.
Relevant in terms of performance are the other two changes:
I moved the code filling the hashtables inside the first loop, so the code won't loop twice over the data. Using index and assignment operators instead of the Add method for assigning values to the hashtable prevents the code from raising an error when a key already exists in the hashtable.
Instead of appending to an array (which has the abovementioned performance impact) the code now simply echoes the cell text in the loop, which PowerShell automatically turns into a list. The list is then assigned to the variable $URows.

Resources