如何在 powershell 中创建这样的数组?
How to create an array like this in powershell?
我需要为 TextToColumns Excel 自动化存储如下数据。
我需要实现 Code-2 或 Code-3 或 Code-4 有什么办法可以实现吗?
我有超过 350 条数据,所以我不能使用 Code-1,这对我来说不公平。
代码 1:工作正常
$var = (1,2),(2,2),(3,2),(4,2),(5,2),(6,2)........(300,2)
$ColumnA.texttocolumns($colrange,1,-412,$false,$false,$false,$false,$false,$true,"|",$var)
代码 2:不工作
$var = @((1,2)..(300,2))
$ColumnA.texttocolumns($colrange,1,-412,$false,$false,$false,$false,$false,$true,"|",$var)
代码 3:不工作
$var = @()
#forloop upto 300
{ $var += ($i,2) }
$ColumnA.texttocolumns($colrange,1,-412,$false,$false,$false,$false,$false,$true,"|",$var)
代码 4:不工作
[array]$var = 1..300 | foreach-object { ,@($_, 2) }
$ColumnA.texttocolumns($colrange,1,-412,$false,$false,$false,$false,$false,$true,"|",$var)
不是答案 - 只是记录一些研究以节省其他人一些时间...
我可以用下面的代码重现这个问题:
$xl = new-object -com excel.application;
$xl.Visible = $true;
$workbook = $xl.Workbooks.Add();
$worksheet = $workbook.Worksheets.Item(1);
$worksheet.Range("A1") = "aaa|111";
$worksheet.Range("A2") = "bbb|222";
$worksheet.Range("A3") = "ccc|333";
$worksheet.Range("A4") = "ddd|444";
$worksheet.Range("A5") = "eee|555";
$worksheet.Range("A6") = "fff|666";
它会像这样构建一个新的电子表格:
如果您然后 运行 以下内容,它将解析 A 列的内容并将结果放入 B 列和 C 列:
$range = $worksheet.Range("A:A");
$target = $worksheet.Range("B1");
# XlColumnDataType enumeration
# see https://docs.microsoft.com/en-us/office/vba/api/excel.xlcolumndatatype
$xlTextFormat = 2;
# XlTextParsingType enumeration
# see https://docs.microsoft.com/en-us/office/vba/api/excel.xltextparsingtype
$xlDelimited = 1;
# XlTextQualifier enumeration
# https://docs.microsoft.com/en-us/office/vba/api/excel.xltextqualifier
$xlTextQualifierNone = -4142;
$var = (1,$xlTextFormat),(2,$xlTextFormat),(3,$xlTextFormat),(4,$xlTextFormat),(5,$xlTextFormat),(6,$xlTextFormat);
# parse the values in A1:A6 and puts the values in a 2-dimensional array starting at B1
# see https://docs.microsoft.com/en-us/office/vba/api/excel.range.texttocolumns
$result = $range.TextToColumns(
$target, # Destination
$xlDelimited, # DataType
$xlTextQualifierNone, # TextQualifier
$false, # ConsecutiveDelimiter
$false, # Tab
$false, # Semicolon
$false, # Comma
$false, # Space
$true, # Other
"|", # OtherChar
$var # FieldInfo
);
然后看起来像这样:
但是,如果将 $var
的声明更改为
$var = 1..6 | % { ,@($_, $xlTextFormat) };
您收到以下错误:
OperationStopped: The remote procedure call failed. (0x800706BE)
并且 Excel 实例终止。
所以这两个声明有些地方不同:
$var = (1,$xlTextFormat),(2,$xlTextFormat),(3,$xlTextFormat),(4,$xlTextFormat),(5,$xlTextFormat),(6,$xlTextFormat);
$var = 1..6 | % { ,@($_, $xlTextFormat) };
但我不明白那是什么:-S
我无法完全解释这里发生了什么,但我想这与 texttocolumns
需要(延迟的)表达式 而不是(评估的)object.
意味着以下内容似乎适用于 Minimal, Reproducible Example from @mclayton:
$Var = Invoke-Expression ((1..6 |% { "($_, `$xlTextFormat)" }) -Join ',')
并希望以下内容能够解决最初问题中的问题:
$Var = Invoke-Expression ((1..300 |% { "($_, 2)" }) -Join ',')
我需要为 TextToColumns Excel 自动化存储如下数据。 我需要实现 Code-2 或 Code-3 或 Code-4 有什么办法可以实现吗? 我有超过 350 条数据,所以我不能使用 Code-1,这对我来说不公平。
代码 1:工作正常
$var = (1,2),(2,2),(3,2),(4,2),(5,2),(6,2)........(300,2)
$ColumnA.texttocolumns($colrange,1,-412,$false,$false,$false,$false,$false,$true,"|",$var)
代码 2:不工作
$var = @((1,2)..(300,2))
$ColumnA.texttocolumns($colrange,1,-412,$false,$false,$false,$false,$false,$true,"|",$var)
代码 3:不工作
$var = @()
#forloop upto 300
{ $var += ($i,2) }
$ColumnA.texttocolumns($colrange,1,-412,$false,$false,$false,$false,$false,$true,"|",$var)
代码 4:不工作
[array]$var = 1..300 | foreach-object { ,@($_, 2) }
$ColumnA.texttocolumns($colrange,1,-412,$false,$false,$false,$false,$false,$true,"|",$var)
不是答案 - 只是记录一些研究以节省其他人一些时间...
我可以用下面的代码重现这个问题:
$xl = new-object -com excel.application;
$xl.Visible = $true;
$workbook = $xl.Workbooks.Add();
$worksheet = $workbook.Worksheets.Item(1);
$worksheet.Range("A1") = "aaa|111";
$worksheet.Range("A2") = "bbb|222";
$worksheet.Range("A3") = "ccc|333";
$worksheet.Range("A4") = "ddd|444";
$worksheet.Range("A5") = "eee|555";
$worksheet.Range("A6") = "fff|666";
它会像这样构建一个新的电子表格:
如果您然后 运行 以下内容,它将解析 A 列的内容并将结果放入 B 列和 C 列:
$range = $worksheet.Range("A:A");
$target = $worksheet.Range("B1");
# XlColumnDataType enumeration
# see https://docs.microsoft.com/en-us/office/vba/api/excel.xlcolumndatatype
$xlTextFormat = 2;
# XlTextParsingType enumeration
# see https://docs.microsoft.com/en-us/office/vba/api/excel.xltextparsingtype
$xlDelimited = 1;
# XlTextQualifier enumeration
# https://docs.microsoft.com/en-us/office/vba/api/excel.xltextqualifier
$xlTextQualifierNone = -4142;
$var = (1,$xlTextFormat),(2,$xlTextFormat),(3,$xlTextFormat),(4,$xlTextFormat),(5,$xlTextFormat),(6,$xlTextFormat);
# parse the values in A1:A6 and puts the values in a 2-dimensional array starting at B1
# see https://docs.microsoft.com/en-us/office/vba/api/excel.range.texttocolumns
$result = $range.TextToColumns(
$target, # Destination
$xlDelimited, # DataType
$xlTextQualifierNone, # TextQualifier
$false, # ConsecutiveDelimiter
$false, # Tab
$false, # Semicolon
$false, # Comma
$false, # Space
$true, # Other
"|", # OtherChar
$var # FieldInfo
);
然后看起来像这样:
但是,如果将 $var
的声明更改为
$var = 1..6 | % { ,@($_, $xlTextFormat) };
您收到以下错误:
OperationStopped: The remote procedure call failed. (0x800706BE)
并且 Excel 实例终止。
所以这两个声明有些地方不同:
$var = (1,$xlTextFormat),(2,$xlTextFormat),(3,$xlTextFormat),(4,$xlTextFormat),(5,$xlTextFormat),(6,$xlTextFormat);
$var = 1..6 | % { ,@($_, $xlTextFormat) };
但我不明白那是什么:-S
我无法完全解释这里发生了什么,但我想这与 texttocolumns
需要(延迟的)表达式 而不是(评估的)object.
意味着以下内容似乎适用于 Minimal, Reproducible Example from @mclayton:
$Var = Invoke-Expression ((1..6 |% { "($_, `$xlTextFormat)" }) -Join ',')
并希望以下内容能够解决最初问题中的问题:
$Var = Invoke-Expression ((1..300 |% { "($_, 2)" }) -Join ',')