我有一個 .xlsm 檔案,我必須在其中在特定單元格中插入公式。'=LEFT(AG6,12)&RIGHT(1000 AG$1, 3)&RIGHT(AG6,5)'然而,我必須插入的公式是在 Excel 中打開檔案后,我看到我輸入的公式顯示在單元格中,而不是公式的結果。
我如何讓這個作業?
$excelPath = '\a\bc\my.xlsm'
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
$book = $excel.workbooks.Open($excelPath)
$savePath = '\x\y\z\my.xlsm'
$ws = $book.workSheets('MySheet')
$ws.Cells(7, 33).Formula = '=LEFT(AG6,12)&RIGHT(1000 AG$1, 3)&RIGHT(AG6,5)'
$book.saveAs($savePath)
$book.close($false)
# Close all object references
$excel.Quit()
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
uj5u.com熱心網友回復:
單元格被格式化為文本。您可以General在撰寫公式之前應用格式。
$ws.Cells(7, 33).NumberFormat = 'General'
$ws.Cells(7, 33).Formula = '=LEFT(AG6,12)&RIGHT(1000 AG$1, 3)&RIGHT(AG6,5)'
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/351188.html
