简体   繁体   English

如何将此代码从Visual Basic转换为Perl?

[英]How do I translate this code from Visual Basic to Perl?

I am trying to export an Excel spreadsheet to SharePoint. 我正在尝试将Excel电子表格导出到SharePoint。 I recorded the Visual Basic code, and now I want to translate it to Perl. 我录制了Visual Basic代码,现在我想把它翻译成Perl。 I tried like this but it didn't work. 我试过这样但是没用。

I don't get any error, but I also don't see the list in the Sharepoint. 我没有收到任何错误,但我也没有看到Sharepoint中的列表。 When I did it using the macro in Excel it worked 当我在Excel中使用宏时,它工作了

use Win32::OLE::Const 'Microsoft Excel';

my $excel = Win32::OLE->new('Excel.Application');
$excel->{'Visible'} = 1;
$excel->{DisplayAlerts} = 1;

my $book = $excel->Workbooks->Open("C:\\Book1.xlsx")
    || die("Unable to open document ", Win32::OLE->LastError());

my $list = $book->ActiveSheet->ListObjects("Table1")->Publish Array("https:\/\/sponsor\/sites\/dev_test_site", "myname"), False;  

The original Visual Basic code 原始的Visual Basic代码

Sub Macro1()

  ActiveSheet.ListObjects("Table1").Publish Array( _
    "https://sponsor/sites/dev_test_site", "myname"), False
  Range("C2").Select
End Sub

Eventually I came up with this code 最终我想出了这段代码

my $excel = Win32::OLE->new('Excel.Application');
$excel->{'Visible'} = 1;
$excel->{DisplayAlerts} = 1;

my $book = $excel->Workbooks->Open("C:\\Book1.xlsm")
    || die("Unable to open document ", Win32::OLE->LastError());

my @array=("https:\/\/sponsor\/sites\/dev_test_site", "aaaa");

my $list= $book->ActiveSheet->ListObjects("hhhh")->Publish(@array, 0);

And this image shows the result 这张图片显示了结果

在此输入图像描述

You should use strict and use warnings . 您应该use strictuse warnings It will proceed to tell you a number of error messages then. 然后它将继续告诉您一些错误消息。

What I can make out from the Perl code you posted without running it is: 我可以从你发布的Perl代码中得到的结果是:

my $list= $book->ActiveSheet->ListObjects("Table1")->Publish Array("https:\/\/sponsor\/sites\/dev_test_site", "myname"), False;

Note that there is a space between Publish and Array( . That has to be a problem. The only way to have a function cal followed by something other than ( or ; or , is if it has prototypes. But method calls in object oriented Perl cannot have prototypes. So that is definitely wrong. 请注意, PublishArray(之间有一个空格Array( 。这必须是一个问题。除了(;或者,如果它有原型)之外,让函数cal后跟其他东西的唯一方法。但是面向对象的Perl中的方法调用不能有原型。所以这绝对是错误的。

Then there's Array(...) . 那就是Array(...) There is no built-in function called Array and I do not think that Win32::OLE::Const exports that, though I did not look. 没有名为Array内置函数,我不认为Win32 :: OLE :: Const导出了,虽然我没看。 Even if it did, you told it to only export 'Microsoft Excel' . 即使它确实如此,你告诉它只导出'Microsoft Excel' The same goes for False . 对于False

I suggest you read the documentation of Win32::OLE::Const and add use strict and use warnings . 我建议你阅读Win32 :: OLE :: Const文档并添加use strictuse warnings There are also some resources of how to work with Win32 modules on Sinan Ünürs blog . SinanÜnürs博客上还有一些如何使用Win32模块的资源。

You could take a look at this: Convert perl script to vba This already has some answers. 您可以看看这个: 将perl脚本转换为vba这已经有了一些答案。

You may need to follow this script. 您可能需要遵循此脚本。 % pp -o hello hello.pl or something like that. % pp -o hello hello.pl或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM