我正在嘗試提高日常作業中的自動化水平,其中一部分涉及在包含基本相同資訊的報告中的表格末尾添加一行,其中一些單元格已更改(新日期) .
我對 VB 和 C 有一點經驗,但在 PowerShell 方面我非常業余,這似乎是任務自動化的首選。
我有幾個 PowerShell 腳本可以搜索報告正文并更改文本,但報告的最后一部分是記錄,需要追加而不是修改。
我該怎么辦?
我嘗試修改我在網上找到的一些 PowerShell 代碼,但無濟于事。我已經在正確的表格中選擇了一行,但我不知道如何選擇最后一行,復制它并將其作為表格底部的新行插入下方:
$objWord = New-Object -ComObject word.application
$objWord.Visible = $True
$objWord.Documents.Open("FILEPATH")
$FindText = "KEYTEXT"
$objWord.Selection.Find.Execute($FindText)
$objWord.Selection.SelectRow()
uj5u.com熱心網友回復:
這是一個冗長的示例,其中包含您正在尋找的解釋:
# open document
$objWord = New-Object -ComObject word.application
$objWord.Visible = $True
$doc = $objWord.Documents.Open("C:\temp\temp.docx")
# search for row
$FindText = "Value2"
$result = $objWord.Selection.Find.Execute($FindText)
$objWord.Selection.SelectRow()
# copy the ranges from searched row
$table = $objWord.Selection.Tables[1]
$copiedCells = $objWord.Selection.Cells | select columnindex,rowindex
# add row at the end of table
$table.Rows[$table.Rows.Count].Select()
$objWord.Selection.InsertRowsBelow(1)
# insert copied text into each column of last row
foreach ($cell in $copiedCells) {
# copy value from cell in copied row
$copiedText = $table.Cell($cell.rowindex, $cell.columnindex).Range.Text
# remove last 2 characters (paragraph and end-of-cell)
$TrimmedText = $copiedText.Remove($copiedText.Length - 2)
# set value for cell in last row
$table.Cell($table.Rows.Count,$cell.columnindex).Range.Text = $TrimmedText
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/454142.html
下一篇:如何從字串中提取原始哈希?
