.NET - 此脚本的每个单独部分都有效,但 运行 它作为一个整体导致它替换我的 SVG 文件中的所有文本
.NET - Every individual part of this script works, but running it as a whole causes it to replace all the text in my SVG file
我已经创建了一个脚本来自动创建电子邮件签名图像。
我要实现的步骤是:
- 用用户信息替换 SVG 文件中的标记文本。
- 使用 Inkscape CLI 生成该 SVG 文件的 PNG 版本。
- 用下一个用户的信息替换 SVG 文件中上一个用户的信息,然后重复步骤 2。
- 重复第 2 步和第 3 步,直到所有用户都拥有 PNG 电子邮件签名文件。
- 将 SVG 文件中最后一个用户的信息替换为原始标签。
脚本的目标:
- 允许将新标签动态添加到 SVG 而无需更改脚本。
- 接受要使用的联系人和 SVG 模板的输入。
- 列表项
这是我的脚本:
param(
[string]$targetDirectory=$(Convert-Path .),
[string]$width = 980,
[string]$height = 242,
[string]$PNGname = "$Name - Signature.png"
)
Write-Host ""
$scriptPath = Split-Path $MyInvocation.MyCommand.Path
$CSVPrompt = 'Input the name of the CSV file you wish to use. Remember the file extension. It must be .txt or .csv and the file must be located in the same directory as this script.'
$SVGPrompt = 'Input the name of the SVG file you wish to use. Remember the file extension. It must be located in the same directory as this script.'
$targetCSV = Read-Host -Prompt $CSVPrompt
$targetSVG = Read-Host -Prompt $SVGPrompt
$contactList = import-csv $scriptPath$targetCSV
$tags = $contactList[0] | Get-Member -Membertype 'NoteProperty' | Select-Object -ExpandProperty 'Name'
Foreach ($tag in $tags) {New-Variable -Name "$tag" -Value "[$tag]";
New-Variable -Name "prior$tag" -Value "[$tag]";}
function ReplaceInformation
{ # Gets the contents of the target SVG file; replaces the tag with the value; Sets the content of the file.
(Get-Content $scriptPath\signature-template.svg).replace
(
(Get-Variable -Name "prior$tag" -ValueOnly),
(Get-Variable -Name $tag -ValueOnly)
) | Set-Content $scriptPath\signature-template.svg;
}
function ProducePNG
{ # Simple command to use Inkscape to convert SVG to PNG of a specified size.
& "C:\Program Files\Inkscape\inkscape.exe" $targetSVG -z -e $PNGname -w $width -h $height;
}
function ResetInformation
{ # This takes the last user's information and resets the tags back to their generic [<tag>].
(Get-Content $scriptPath\signature-template.svg).replace((Get-Variable -Name "prior$tag" -ValueOnly), "[$tag]") | Set-Content $scriptPath\signature-template.svg;
}
Foreach ($contact in $contactList)
{
Foreach ($tag in $tags)
{ # This produces variables set to the tag value in the contact. i.e. the $Name variable with the value of $contact.Name
Set-Variable -Name "$tag" -Value $contact.$tag;
ReplaceInformation
Set-Variable -Name "prior$tag" -Value $contact.$tag;
} # The "prior" variables are for chaining replace after the initial replacement. i.e. going from [Name] to Bob to Julie without having to reset back to [Name] until the last user is done.
ProducePNG
}
ResetInformation
当我 运行 在 Powershell 中手动将其用于预期目的时,每个单独的功能都可以正常工作。当我 运行 整个脚本时,我看到了很多这样的东西,但研究并没有告诉我它是什么意思:
OverloadDefinitions
-------------------
string Replace(char oldChar, char newChar)
string Replace(string oldValue, string newValue)
string Replace(char oldChar, char newChar)
string Replace(string oldValue, string newValue)
string Replace(char oldChar, char newChar)
string Replace(string oldValue, string newValue)
... etc etc for 100's of lines before the script completes.
当我在输入 CSV 和 4 个标记字段中只有 2 个联系人时,我得到了这个,所以我希望看到这些行对这么多用户最多重复 12-14 次。 100 次对我来说没有意义,除非每个字符一次。
我拥有的 SVG 文件大小为 250KB,但是当我 运行 脚本时,我最终只得到列表中的最后一个标签和最后一个用户对该标签的值。所以 250KB 的 SVG 变成了这个:
Receptionist
[Title]
这是一个示例标签列表,它们将在 SVG 文件中,但存储在 Powershell 的 $tags
变量中。它按字母顺序排列,但我认为这不是我的问题。这只是它为每个联系人进行替换的顺序:
[Email]
[Name]
[Number]
[Title]
与我在测试中使用的 CSV 示例相当的示例:
Name,Email,Title,Number
Bob Saggett,bob@company.com,Janitor,9878
Julie Grey,julie2@company.com,Receptionist,9001
我觉得我的问题的核心在于我如何使用 .NET 替换方法,但从文档来看,我认为我使用它是正确的。
https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx
.NET 版本为 4.5.1 & PowerShell 版本为 4.0.
在 Powershell 脚本中,您不应像这样将调用参数携带到新行:
(Get-Content $scriptPath\signature-template.svg).replace
(
(Get-Variable -Name "prior$tag" -ValueOnly),
(Get-Variable -Name $tag -ValueOnly)
) | Set-Content $scriptPath\signature-template.svg;
上面的代码导致replace
在没有参数的情况下实际执行。结果,Powershell 仅打印 replace
的可能重载定义。如果您在 Powershell window:
中键入 "".replace
,您可能会重复此行为
PS C:> "".replace
OverloadDefinitions
-------------------
string Replace(char oldChar, char newChar)
string Replace(string oldValue, string newValue)
因此,要解决此问题,请更改 replace
调用,使传递的参数位于同一行:
(Get-Content $scriptPath\signature-template.svg).replace((Get-Variable -Name "prior$tag" -ValueOnly), (Get-Variable -Name $tag -ValueOnly)) | Set-Content $scriptPath\signature-template.svg;
对此:
I'm getting that when I only have 2 contacts in the input CSV and 4
tagged fields, so I would expect to see those line repeated, at most,
12-14 times for that many users. 100's of times doesn't make sense to
me unless it's one per character.
您观察到如此多的 replace
错误调用的原因很简单。 Get-Content
returns 文件内容不是单个字符串,而是文件行的集合。这就是为什么 replace
被调用的次数与 signature-template.svg
文件中包含的行数一样多。
我已经创建了一个脚本来自动创建电子邮件签名图像。
我要实现的步骤是:
- 用用户信息替换 SVG 文件中的标记文本。
- 使用 Inkscape CLI 生成该 SVG 文件的 PNG 版本。
- 用下一个用户的信息替换 SVG 文件中上一个用户的信息,然后重复步骤 2。
- 重复第 2 步和第 3 步,直到所有用户都拥有 PNG 电子邮件签名文件。
- 将 SVG 文件中最后一个用户的信息替换为原始标签。
脚本的目标:
- 允许将新标签动态添加到 SVG 而无需更改脚本。
- 接受要使用的联系人和 SVG 模板的输入。
- 列表项
这是我的脚本:
param(
[string]$targetDirectory=$(Convert-Path .),
[string]$width = 980,
[string]$height = 242,
[string]$PNGname = "$Name - Signature.png"
)
Write-Host ""
$scriptPath = Split-Path $MyInvocation.MyCommand.Path
$CSVPrompt = 'Input the name of the CSV file you wish to use. Remember the file extension. It must be .txt or .csv and the file must be located in the same directory as this script.'
$SVGPrompt = 'Input the name of the SVG file you wish to use. Remember the file extension. It must be located in the same directory as this script.'
$targetCSV = Read-Host -Prompt $CSVPrompt
$targetSVG = Read-Host -Prompt $SVGPrompt
$contactList = import-csv $scriptPath$targetCSV
$tags = $contactList[0] | Get-Member -Membertype 'NoteProperty' | Select-Object -ExpandProperty 'Name'
Foreach ($tag in $tags) {New-Variable -Name "$tag" -Value "[$tag]";
New-Variable -Name "prior$tag" -Value "[$tag]";}
function ReplaceInformation
{ # Gets the contents of the target SVG file; replaces the tag with the value; Sets the content of the file.
(Get-Content $scriptPath\signature-template.svg).replace
(
(Get-Variable -Name "prior$tag" -ValueOnly),
(Get-Variable -Name $tag -ValueOnly)
) | Set-Content $scriptPath\signature-template.svg;
}
function ProducePNG
{ # Simple command to use Inkscape to convert SVG to PNG of a specified size.
& "C:\Program Files\Inkscape\inkscape.exe" $targetSVG -z -e $PNGname -w $width -h $height;
}
function ResetInformation
{ # This takes the last user's information and resets the tags back to their generic [<tag>].
(Get-Content $scriptPath\signature-template.svg).replace((Get-Variable -Name "prior$tag" -ValueOnly), "[$tag]") | Set-Content $scriptPath\signature-template.svg;
}
Foreach ($contact in $contactList)
{
Foreach ($tag in $tags)
{ # This produces variables set to the tag value in the contact. i.e. the $Name variable with the value of $contact.Name
Set-Variable -Name "$tag" -Value $contact.$tag;
ReplaceInformation
Set-Variable -Name "prior$tag" -Value $contact.$tag;
} # The "prior" variables are for chaining replace after the initial replacement. i.e. going from [Name] to Bob to Julie without having to reset back to [Name] until the last user is done.
ProducePNG
}
ResetInformation
当我 运行 在 Powershell 中手动将其用于预期目的时,每个单独的功能都可以正常工作。当我 运行 整个脚本时,我看到了很多这样的东西,但研究并没有告诉我它是什么意思:
OverloadDefinitions
-------------------
string Replace(char oldChar, char newChar)
string Replace(string oldValue, string newValue)
string Replace(char oldChar, char newChar)
string Replace(string oldValue, string newValue)
string Replace(char oldChar, char newChar)
string Replace(string oldValue, string newValue)
... etc etc for 100's of lines before the script completes.
当我在输入 CSV 和 4 个标记字段中只有 2 个联系人时,我得到了这个,所以我希望看到这些行对这么多用户最多重复 12-14 次。 100 次对我来说没有意义,除非每个字符一次。
我拥有的 SVG 文件大小为 250KB,但是当我 运行 脚本时,我最终只得到列表中的最后一个标签和最后一个用户对该标签的值。所以 250KB 的 SVG 变成了这个:
Receptionist
[Title]
这是一个示例标签列表,它们将在 SVG 文件中,但存储在 Powershell 的 $tags
变量中。它按字母顺序排列,但我认为这不是我的问题。这只是它为每个联系人进行替换的顺序:
[Email]
[Name]
[Number]
[Title]
与我在测试中使用的 CSV 示例相当的示例:
Name,Email,Title,Number
Bob Saggett,bob@company.com,Janitor,9878
Julie Grey,julie2@company.com,Receptionist,9001
我觉得我的问题的核心在于我如何使用 .NET 替换方法,但从文档来看,我认为我使用它是正确的。
https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx
.NET 版本为 4.5.1 & PowerShell 版本为 4.0.
在 Powershell 脚本中,您不应像这样将调用参数携带到新行:
(Get-Content $scriptPath\signature-template.svg).replace
(
(Get-Variable -Name "prior$tag" -ValueOnly),
(Get-Variable -Name $tag -ValueOnly)
) | Set-Content $scriptPath\signature-template.svg;
上面的代码导致replace
在没有参数的情况下实际执行。结果,Powershell 仅打印 replace
的可能重载定义。如果您在 Powershell window:
"".replace
,您可能会重复此行为
PS C:> "".replace
OverloadDefinitions
-------------------
string Replace(char oldChar, char newChar)
string Replace(string oldValue, string newValue)
因此,要解决此问题,请更改 replace
调用,使传递的参数位于同一行:
(Get-Content $scriptPath\signature-template.svg).replace((Get-Variable -Name "prior$tag" -ValueOnly), (Get-Variable -Name $tag -ValueOnly)) | Set-Content $scriptPath\signature-template.svg;
对此:
I'm getting that when I only have 2 contacts in the input CSV and 4 tagged fields, so I would expect to see those line repeated, at most, 12-14 times for that many users. 100's of times doesn't make sense to me unless it's one per character.
您观察到如此多的 replace
错误调用的原因很简单。 Get-Content
returns 文件内容不是单个字符串,而是文件行的集合。这就是为什么 replace
被调用的次数与 signature-template.svg
文件中包含的行数一样多。