【发布时间】:2020-05-16 22:25:13
【问题描述】:
伙计们,我是 Poweshell 脚本的新手,我一直在谷歌上搜索很多代码以获取 sn-ps 代码并了解它的作用并根据我的需要对其进行自定义。我正在做的一个小项目需要帮助。我有一个 EXCEL 电子表格,里面只有一个工作表。我从第 3 方 URL 下载了 excel,在 excel 工作表中我有数据。在 Excel 的顶部有几个单元格,我想忽略它们并从实际上有一个带有标题的表格的行开始。有了我所拥有的,我硬编码了起始位置的值,然后我通过使用 for 循环逐行读取将这些值写入 PIPE 分隔的文本文件。现在我想根据该特定行的列中的字符串动态获取该标题字段,然后开始从该位置读取直到文件末尾。
源数据看起来像这样。
源数据:
我有这样的代码。如您所见,我对列进行了硬编码,然后将数据写入我在顶部创建的文本文件中。我想根据标题字符串使起始位置动态,它可能出现在任何位置(在这种情况下它恰好是 11)。
#Download the Medicare Fee Scheule for the state website for the respective year
$url "https://med.noridianmedicare.com/documents/10525/23843395/California%2C%20Area+05%2C%202020+Medicare+Part+B+Fee+Schedule+Excel+File"
$path = "\\srqsctfs01\sftp\BA\MedicareFeeSchedule"
$archivepath = "\\srqsctfs01\sftp\BA\MedicareFeeSchedule\Archive" # Setting the Archive File path variable to moving the excel file to archive location
$file = "MedicareFeeSchedule2020_SFCounty" # Set a file name variable
$filename = $file + ".xlsx"
$outputfilepath = $path + "\" + $filename #Declare the Output filepath variable
$archivefilepath = $archivepath + "\" + $filename #"# dummy comment to fix syntax highlighting in SO
$start_time = Get-Date
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $outputfilepath)
#OR
(New-Object System.Net.WebClient).DownloadFile($url, $outputfilepath)
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
#Declare the file path and sheet name
$sheetName = "California, Area 05"
# CREATE AN EMPTY TEXT FILE ONLY IF IT DOES NOT ALREADY EXIST
$FileName = "MedicareFeeSchedule2020_SFCounty" + "_" + (Get-Date).tostring("MM-dd-yyyy")
$TextFilePath = $path + "\" + $FileName + ".txt" #"# dummy comment to fix syntax highlighting in SO
if (!(Test-Path $TextFilePath))
{
New-Item -itemType File -Path $path -Name ($FileName + ".txt")
}
else
{
Remove-Item $TextFilePath
}
#Create an instance of Excel.Application and Open Excel file
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open($outputfilepath)
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible=$false
#Count max row
$rowMax = ($sheet.UsedRange.Rows).count
#Declare the starting positions
$rowNote,$colNote = 11,1
$rowProcedureCode,$colProcedureCode = 11,2
$rowModifier,$colModifier = 11,3
$rowParAmount,$colParAmount = 11,4
$rowNonParAmount,$colNonParAmount = 11,5
$rowLimitingChargeAmount,$colLimitingChargeAmount = 11,6
#loop to get values and store it
for ($i=1; $i -le $rowMax-1; $i++)
{
$Note = $sheet.Cells.Item($rowNote+$i,$colNote).text
$ProcedureCode = $sheet.Cells.Item($rowProcedureCode+$i,$colProcedureCode).text
$Modifier = $sheet.Cells.Item($rowModifier+$i,$colModifier).text
$ParAmount = $sheet.Cells.Item($rowParAmount+$i,$colParAmount).text
$NonParAmount = $sheet.Cells.Item($rowNonParAmount+$i,$colNonParAmount).text
$LimitingChargeAmount = $sheet.Cells.Item($rowLimitingChargeAmount+$i,$colLimitingChargeAmount).text
$string = ($Note + "|" + $ProcedureCode + "|" + $Modifier + "|" + $ParAmount + "|" + $NonParAmount + "|" + $LimitingChargeAmount)
$string | Out-File -FilePath $TextFilePath -Append
}
#close excel file
$objExcel.quit()
非常感谢您为实现这一目标提供的任何帮助。
谢谢! 帕布
【问题讨论】:
标签: regex excel powershell foreach