excel - 粘贴\返回到打开的 Excel 工作表的单元格 A1
问题描述
我希望获取在 PowerShell 中生成的字符串并将该字符串粘贴\返回到 OPEN 电子表格的单元格 A:1 中。我对此进行了研究,但我不了解 excel 特定的语法。我是否需要识别和声明正在运行的特定 Excel 工作簿的 PID,如果需要,如何?
我知道如何创建一个新工作簿并将文本添加到单元格 A:1 但不是已经打开的单元格。粘贴后,它还必须“按回车键”执行电子表格功能。
我不知道从哪里开始: ((new-object -com "excel.application").Workbooks.Add()).application.Visible=$True
我在网上搜索过,但没有找到任何有意义的例子。请帮忙
解决方案
您可以使用以下脚本来实现它
#Specify the path of the excel file
$FilePath = "path\test.xlsx"
#Specify the Sheet name
$SheetName = "Sheet1"
# Create an Object Excel.Application using Com interface
$objExcel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')
# Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $false
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet 'BuildSpecs'
$WorkSheet = $WorkBook.sheets.item($SheetName)
$WorkSheet.Cells.Item(1,1) = "Link" #Updates the first cell (A1)
$WorkBook.Save()
$WorkBook.Close()
确保该文件具有 PowerShell 脚本的写入权限。
如果要编辑已经打开的工作表,请更改文件的路径并正确提及工作表名称,它将按预期工作。
推荐阅读
- excel - 将一些列附加到另一列,并创建一个单独的列以包含附加的列标题标题
- react-jsonschema-forms - 在 React-JsonSchema-Form 中从表单字段的开头和结尾修剪空格
- python - 如何将用户输入与列表中的大写字母字符串进行比较?
- r - 在一个条形图上绘制多个变量?
- android - 有没有办法在 android 编辑文本中创建新的输入类型?
- reactjs - 仅在第一次以惯用方式和优雅方式呈现组件时执行 react-apollo-hooks useQuery
- javascript - 为什么我的地图无法在手机上正确显示
- android - 如何在返回值之前等待函数执行完毕?
- php - 如何将数组数据发布到端点 - Laravel
- javascript - 为什么 new Date(ms) == (new Date()).setMilliseconds(ms)