【问题标题】:Powershell msword table format cell textPowershell msword 表格格式单元格文本
【发布时间】:2015-03-28 13:40:03
【问题描述】:

使用 powershell 可以像这样在表格单元格中添加文本,现在我想格式化单元格文本。

table.Cell(1, 1).Range.Text = "ListvItem1vitem2"

例如。 我想加粗并对齐中心“列表”字符串。为 Item1 和 Iteam2 字符串应用有序项目样式。 如何使用 powershell 做到这一点?

编辑: 在 C# 中找到此代码,无法使其在 powershell 中运行。

https://social.msdn.microsoft.com/Forums/vstudio/en-US/4d7fa718-29d7-4b71-848e-11d7aafac5b7/multiple-format-into-one-cell-of-table-in-word-2007-using-c?forum=worddev

 //boldrange_1 for "green", boldrange_2 for "inch".
            Word.Range boldrange_1 = table.Cell(1, 2).Range;//Assign the whole cell range to boldrange, then adjust it with SetRange method.
            boldrange_1.SetRange(table.Cell(1, 2).Range.Start, table.Cell(1, 2).Range.Words[1].End);
            boldrange_1.Bold= 1;

            Word.Range boldrange_2 = table.Cell(2, 2).Range;
            boldrange_2.SetRange(table.Cell(2, 2).Range.Words[4].End, table.Cell(2, 2).Range.Words[5].End);
            boldrange_2.Font.Bold = 1;
            //I've found that boldrange_2.Font.Bold = 1; and boldrange_2.Bold = 1; have the same effect.

【问题讨论】:

    标签: powershell ms-word


    【解决方案1】:

    将字体设置为粗体或正常实际上并不难,是的,只需要使用 set-range。更改对齐方式或将单词设置为列表有点棘手,因为您必须在单独的段落中设置元素,并且典型方法(使用 TypeParagraph 和 TypeText)在表格单元格中不起作用。这种方法对我有用,但段落设置和表格设置需要根据您的具体需求进行更改。

    #This is just creating a simple table for testing
    $w = New-Object -ComObject "word.application"
    $w.Visible = $true
    $doc = $w.Documents.add()
    $r = $doc.Range()
    $table = $doc.Tables.add($r, 5, 5)
    $list = $table.Cell(1,1).Range #so sel doesn't have to be rewritten by hyperlinks
    $sel = $table.Cell(1,1).Range  #this is because the hyperlinks break indexof
    $hyper = $table.Cell(1,1).Range #doing assign this way for emphasis could do easier
    
    #Define full text for spliting up into ranges and setup paragraphs
    $sel.InsertParagraph()
    $w.Selection.TypeText("HyperLink`n`rList`r`nItem1`r`nItem2")
    
    #Set up the hyperlink (use -2 to keep `n`r for paragraph)
    $hyper.SetRange(0, $sel.Text.IndexOf("List") - 2)
    $list.SetRange($sel.Text.IndexOf("List"), $sel.Text.Length)
    $sel.SetRange($sel.Text.IndexOf("Item"), $sel.Text.Length)
    
    $doc.Hyperlinks.Add($hyper, 'www.google.com', $null, $null, "Link to Google")
    
    #set List range of cell to bold and alignment center
    $list.Bold = $true
    $list.paragraphFormat.Alignment = 'wdAlignParagraphCenter'
    
    #Now using sel range: add paragraph break (so list only applies to this)
    #Then set alignment and apply a number list
    $sel.InsertParagraphBefore()
    $sel.paragraphFormat.Alignment = 'wdAlignParagraphRight'
    $sel.ListFormat.ApplyNumberDefault()
    

    更新:清理代码并添加超链接构建方法

    【讨论】:

    • 这对我有用。我需要在列表项上方添加超链接。你能帮忙插入超链接吗?
    • @sfgroups 您可能会考虑在那时拆分单元格,但我已经更新了代码以提供此功能(注意 3 个范围集的原因是超文本由 indexof 和 .Text 读取作为“Google Link”,但实际上是一个 uri 对象并扩展了解释)
    猜你喜欢
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2012-07-09
    • 1970-01-01
    • 2018-01-20
    • 2020-06-14
    • 1970-01-01
    相关资源
    最近更新 更多