由于未定义的值,无法调用Range()方法

时间:2015-03-27 15:53:29

标签: excel perl win32ole

我现在已经开始和关闭这个项目两天了。我决定学习从Perl开始编程,因为它提供了所有的数据操作功能。

这是我的代码,然后是我的错误。我尽力评论它。对不起,这很难读。

我认为在do直接循环中声明我的变量会允许它们存储用户输入它没有工作的值。

此外,有关清理代码和帮助削减处理所需系统资源的任何建议吗?

另外,我尝试格式化单元格,但无济于事。我已经对这些内容进行了评论。如果你能帮助我让他们工作那将是非常棒的,或至少解释他们为什么不是。

#!C:\Perl\bin
#manage IR/SRN/Parts Used
#Written for Zebra
#Author Dalton Brady
#Location Bentonville AR

#use strict;
use warnings;
use POSIX qw(strftime);
use Win32::OLE;
use Win32::OLE qw( in with);
use Win32::OLE::Const 'Microsoft Excel';
use Win32::OLE::Variant;

$Win32::OLE::Warn = 3;    #Die on errors

my @uut = ('VC5090', 'MK2046', 'MK2250', 'MK4900', '@pos'); #different types of units worked on, trying to name the worksheet after one depending on user input have yet to add that func.
my $ref   = strftime '%Y-%m-%d', localtime();    #create the datestamp
my $i     = 0;                                   #increment counter for why loop
my $n     = 0;                                   #increment for do until loop
my $xfile = 'X:\dataTest\data.xls';              #location of excel file
my $book;     #place for the workbook to exist
my $sheet;    #place for the worksheet to exist

#my $ir;        #store IR of uut
#my $sn;        #store serial number of uut
my $ex;         #a place to hold the excel application
my $pu;         #store the number of parts used, to close the while loop
my $row = 2;    #track row in which data will be placed with the do until loop line 56
my $col = 1;
my $uc;         #units completed to be entered monitors how long to keep the loop open
#my @parts;     #store the different parts as a list within an area (to be written to the spreadsheet)
my @talk = ('IR:', 'SN:', '#of Parts Used: ', 'PN:', 'Units Completed: ');

# start an instance of excel

$ex = Win32::OLE->new('Excel.Application');
$ex->{DisplayAlerts} = 0;    #turn off alerts/textboxes/saveboxes

#check to see if excel file exists if yet open it, if not create it
if (-e $xfile) {
    $book = $ex->Workbooks->Open($xfile);
}
else {
    $book = $ex->Workbooks->Add();    #create new workbook to be used because we couldn't find one

    #########SETUP YOUR EXCEL FILE#############
    $sheet = $book->Worksheets("Sheet1");
    $sheet->Activate();
    $sheet->{Name} = "Test";

    #$sheet->Cells("a1")->HorizontalAlignment = x1HAlignCenter;
    #$sheet->Cells("b1")->HorizontalAlignment = x1HAlignCenter;
    #$sheet->Cells("c1")->HorizontalAlignment = x1HAlignCenter;
    $sheet->Columns("a")->{ColumnWidth} = 20;
    $sheet->Columns("b")->{ColumnWidth} = 20;
    $sheet->Columns("c")->{ColumnWidth} = 30;
    $sheet->Range("a1")->{Value}        = "IR Number";
    $sheet->Range("b1")->{Value}        = "Serial Number";
    $sheet->Range("c1")->{Value}        = "Parts Used";
    $sheet->Range("d1")->{Value}        = "Date";

    $book->SaveAs($xfile);    #Save the file we just created
}

do {
    my @parts = "";
    my $ir    = "";
    my $sn    = "";

    print $talk[4];   #ask for how many units user will be scanning or has completed to be scanned
    $uc = <>;

    print $talk[0];   #ask for the IR number
    $ir = <>;

    print $talk[1];   #ask for uut Serial Number
    $sn = <>;

    print $talk[2];   #ask for the number of parts used, to regulate the parts list storage into the @parts array
    $pu = <>;

    while ($i < $pu) {
        print $talk[3];
        my $scan = <>;
        push @parts, $scan;
        $i++;
    }

    #prints a col 2 row
    $sheet->Range($row, $col)->{Value} = $ir;
    $col++;
    $sheet->Range($row, $col)->{Value} = $sn;
    $col++;
    $sheet->Range($row, $col)->{Value} = (join(", ", @parts));
    $col++;
    $sheet->Range($row, $col)->{Value} = $ref;
    $row++;
    $col = 1;
    $n++;

} until ($n == $uc);

# save and exit
$book->SaveAs($xfile);
$book = $ex->WorkBooks->Close();

undef $book;
undef $ex;

这是我在运行脚本时收到的错误。

Units Completed: 3
IR:94844545
SN:56489149816
#of Parts Used: 1
PN:694198419
Can't call method "Range" on an undefined value at gen1.pl line 86, <> line 5.
Press any key to continue . . .

0 个答案:

没有答案