【问题标题】:Return a reference to an Excel Worksheet from a PowerShell Function从 PowerShell 函数返回对 Excel 工作表的引用
【发布时间】:2020-04-30 16:41:57
【问题描述】:

为了代码的可读性,我想将我的 Excel 内容移动到一个函数中,并让工作表对象在我的程序处理内容时可用于写入单元格值。如何调用创建 Excel 电子表格并返回工作表引用的函数,以便我可以继续访问我创建的打开/活动 Excel 应用程序对象?

Function Create-Excel-Spreadsheet() {
    # open excel
    $excel = New-Object -ComObject excel.application
    $excel.visible = $True

    # add a worksheet
    $workbook = $excel.Workbooks.Add()
    $xl_wksht= $workbook.Worksheets.Item(1)
    $xl_wksht.Name = 'Cut-off'   #give the worksheet a name

    #Create a Title for the first worksheet and adjust the font
    $title_row = 1
    $xl_wksht.Cells.Item($title_row, 1)= 'Cut-off Processing Ran ' + $startday + ' at ' + $starttime_str

    #merging a few cells on the top row to make the title look nicer
    $MergeCells = $xl_wksht.Range("A1:Q1")
    $MergeCells.Select() 
    $MergeCells.MergeCells = $true

    #formatting the title and giving it a font & color
    $xl_wksht.cells($title_row, 1).HorizontalAlignment = -4108   # center the title
    $xl_wksht.cells.Item($title_row,1).Font.Size = 18
    $xl_wksht.cells.Item($title_row,1).Font.Bold = $True
    $xl_wksht.cells.Item($title_row,1).Font.Name = "Cambria"
    $xl_wksht.cells.Item($title_row,1).Font.ThemeFont = 1
    $xl_wksht.cells.Item($title_row,1).Font.ThemeColor = 4
    $xl_wksht.cells.Item($title_row,1).Font.ColorIndex = 55
    $xl_wksht.cells.Item($title_row,1).Font.Color = 8210719
    $xl_wksht.cells.Item($title_row,1).Font.Color = 8210719
    $xl_wksht.Rows[$title_row].RowHeight = 39
    $xl_wksht.Rows[$title_row].VerticalAlignment = 2

    #create the column headers
    $header_row = 2
    $data_row_start = 3

    $xl_wksht.Rows[$header_row].WrapText = $True
    $xl_wksht.Rows[$header_row].Font.Bold = $True
    $xl_wksht.Rows[$header_row].columnWidth = 12.57
    $xl_wksht.Rows[$header_row].HorizontalAlignment = -4108

    $xl_wksht.cells.Item($header_row, 1).value2 = 'Current Load Date'
    $xl_wksht.Columns[1].HorizontalAlignment = -4108
    $xl_wksht.Columns[1].NumberFormat = "@"

    $xl_wksht.cells.Item($header_row, 2).value2 = 'Export File Type'
    $xl_wksht.Columns[2].columnWidth = 26

    $xl_wksht.cells.Item($header_row,3).value2 = 'File Name to Downloaded'
    $xl_wksht.Columns[3].columnWidth = 37

    $xl_wksht.cells.Item($header_row,4).value2 = 'Source Path'
    $xl_wksht.Columns[4].columnWidth = 23

    return $xl_wksht
}

【问题讨论】:

    标签: excel function powershell reference


    【解决方案1】:

    如果您希望获得对工作表的引用,您可以更新您的代码以确保除了您要查找的内容之外没有其他任何内容被打印/返回。就目前而言,您正在返回两个变量..

    $wksht = Create-Excel-Spreadsheet
    
    1. $wksht[0] -> 布尔值(真)
    2. $wksht[1] -> 工作表参考。

    返回变量的第二个索引将包含您的电子表格引用,其中 first 只是一个布尔值。

    This is a good read 来自 powershell 的创始人关于为什么会发生这种情况。

    如果你拿你的代码并执行它,你会发现在对象$xl_wksht本身被打印之前,有一个True被打印出来。您需要做的是确保屏幕上除了您需要的变量/引用之外没有其他内容。

    在您的代码中更改此行,您将能够获取引用。

        $MergeCells.Select() | Out-Null # line 17 of your code. Add | Out-Null. Or, Simply remove this line as it doesnt do anything.
    

    更新代码后,您可以使用变量从方法中获取值

        $wksht = Create-Excel-Spreadsheet
        $wksht.Cells(5, 1) = "Test"       // Writes Test on Row 5, Col 1.
    

    【讨论】:

    • 成功了!感谢您的快速帮助。我可能会问一个关于将其拉入模块并提供参考的单独问题。尽管尝试了带有清单的典型建议,但我遇到了一些挑战(大量试验和错误)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    相关资源
    最近更新 更多