简体   繁体   English

Select 使用 ComOject Excel 的活动工作表中的所有单元格。在 Powershell 中的应用

[英]Select All Cells in Active Worksheet using ComOject Excel.Application in Powershell

Very new to Powershell. Powershell 非常新。

Situation:情况:

My script below is meant to do the following:我下面的脚本旨在执行以下操作:

  1. Open New Workbook打开新工作簿
  2. Put "This is A1" in the cell 'A1'在单元格“A1”中输入“这是 A1”
  3. Select all cells in the Active Worksheet Select 活动工作表中的所有单元格
  4. Set the selection to TEXT data format将选择设置为 TEXT 数据格式

Here is my Script for Powershell:这是我的 Powershell 脚本:

 $excel = New-Object -Com Excel.Application $excel.visible = $true $book = $excel.Workbooks.Add() $book.ActiveSheet.Range("A1").Value = "This is A1" $worksheet = $book.ActiveSheet.UsedRange.Cells $colcount = $worksheet.columns.count $copyrange = $worksheet.range("1:$colcount").numberformat = "@"

Issue:问题:

I don't know how to write Select All in the command because when I run my script, it only makes 'A1 & B1' TEXT cells and everything else is left as GENERAL format.我不知道如何在命令中编写Select All ,因为当我运行我的脚本时,它只会生成'A1 & B1'文本单元格,其他所有内容都保留为GENERAL格式。 I would like to make the Range Dynamic.我想使范围动态。

This is my script in VBA:这是我在 VBA 中的脚本:

 Sub TEXT_FORMAT() ActiveCell.FormulaR1C1 = "This is A1" Cells.Select Selection.NumberFormat = "@" End Sub

The Script I tried to follow:我试图遵循的脚本:

How to select all in excel using Powershell 如何使用 Powershell 在 excel 中的 select

Can anyone please help me out with how I can make this more dynamic with my SELECT ALL statement?任何人都可以帮我解决如何使用我的 SELECT ALL 语句使这更加动态吗?

Much appreciated.非常感激。

Try this out, it worked for me.试试这个,它对我有用。 You can apply the number formatting by either selecting ALL rows (or) ALL columns您可以通过选择所有行(或)所有列来应用数字格式

$excel = New-Object -Com Excel.Application
$excel.visible = $true
$book = $excel.Workbooks.Add()
$worksheet = $book.ActiveSheet
$worksheet.Range("A1").Value2 = "This is A1" # Use the property 'Value2'
$worksheet.Rows.NumberFormat = "@"
# $worksheet.Columns.NumberFormat = "@" # this also sets number format of all cells to 'Text'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM