当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 周五乐趣:将 PowerShell ISE 内容发送到 Word

作者:精品下载站 日期:2024-12-14 07:45:13 浏览:13 分类:玩电脑

周五乐趣:将 PowerShell ISE 内容发送到 Word


[玩转系统] 周五乐趣:将 PowerShell ISE 内容发送到 Word

因此,我为 PowerShell ISE 整合了一个快速功能。

Function Send-ToWord {
[cmdletbinding()]
Param(
[ValidatePattern("\S+")]
[string[]]$Text = $psise.CurrentFile.Editor.SelectedText)

If (($global:word.Application -eq $Null) -OR -NOT (Get-Process WinWord)) {
    #remove any variables that might be left over just to be safe
    Remove-Variable -Name doc,selection -Force -ErrorAction SilentlyContinue

    #create a Word instance if the object doesn't already exist
    $global:word = New-Object -ComObject word.application

    #create a new document
    $global:doc = $global:word.Documents.add()

    #create a selection
    $global:selection = $global:word.Selection

    #set font and paragraph for fixed width content
    $global:selection.Font.Name = "Consolas"
    $global:selection.font.Size = 10
    $global:selection.paragraphFormat.SpaceBefore = 0
    $global:selection.paragraphFormat.SpaceAfter = 0

    #show the Word document
    $global:word.Visible = $True    
}

#insert the text
$global:selection.TypeText($text) 
#insert a new paragraph (ENTER)  
$global:selection.TypeParagraph()

} #end Function

此功能会将 ISE 中的任何选定文本粘贴到 Word 文档中。第一次运行该函数时,PowerShell 将创建一个 Word 文档并将其格式化为固定宽度文本。然后它将插入您的文本和新的段落标记。下次运行该函数时,它应该检测到您打开了文档并重新使用现有变量。 Word 文档将可见,因此您可以进一步编辑并保存它。如果您在文档中移动光标,您插入的任何新内容都会移至那里。

为了使其易于使用,请将此函数插入您的 PowerShell ISE 配置文件脚本中,并添加带有键盘快捷键的菜单项。

$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Send to Word",{Send-ToWord},"Ctrl+Alt+W") | Out-Null

[玩转系统] 周五乐趣:将 PowerShell ISE 内容发送到 Word

现在,我可以从 ISE 脚本窗格中选择代码,并使用快速组合键将其发送到 Word。玩得开心,享受你的周末。

更新:我发布了另一个版本,其中包含复制和粘贴为彩色代码的选项。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯