我有一个Excel文件,如下所示:
Visted Domains Comments
yahoo.com
google.com
hotmail.com
所有列都已填充。
我正在尝试从Excel文件中读取域,在IE中打开。 一旦访问过,写下“是”'在'访问'列。
到目前为止,此当前脚本从Excel文件中读取,并在IE中打开。关闭当前的IE窗口后,它会打开下一个URL。
$ExcelObject = New-Object -comobject Excel.Application
$ExcelObject.Visible = $true
$ExcelObject.DisplayAlerts = $False
$excelFile = "C:\Users\muafzal\Documents\Files\EMIE\Analyzing\list.xlsx"
$Workbook = $ExcelObject.workbooks.open($excelFile)
$Sheet = $Workbook.Worksheets.Item(1)
$row = [int]2
$domain = @() # beginnt bei 2,1... 3,1... 4,1
Do {
$domain += $Sheet.Cells.Item($row,2).Text ; $row = $row + [int]1
} until (!$Sheet.Cells.Item($row,1).Text)
ForEach($url in $domain){
#Start IE and make it visible
$ie = new-object -com "InternetExplorer.Application"
$ie.Visible = $true
#Navigate to the URL
$ie.Navigate($url)
# Output URL that have been visited to a text file.
$url | Out-File $done -Append
#Sleep while IE is running
while($ie.visible){
start-sleep -s 1
}
}
我希望Excel文档可写,因此我可以输入有关该网站的评论。
答案 0 :(得分:0)
我想“我希望excel文档可写”你的意思是,PS脚本应该为你完成这项工作。
为此,我们必须解决2个问题:
我们如何写入excel单元格:
首先,您可能不希望使用Text
返回的Range
对象的$Sheet.Cells.Item($row, 1)
属性,因为它始终返回打开excel表时显示的内容(并且包括如果文本不适合单元格则可以获得的哈希值。有关详细信息,请参阅this问题
我的猜测是,Value
将是正确的,而不是Text
- 因为它是一个属性,你也可以用它来写信息。
我对你的脚本的建议是将逻辑从ForEach循环移动到你的Do循环,因为你可以使用$ row索引来解决Visited和Comment列。
要将列设置为已访问,您可以编写例如:
Do {
$domain += $Sheet.Cells.Item($row,2).Text
# (browser magic here!)
# edit exel sheet:
$Sheet.Cells.Item($row, 1).Value = 'yes'
$row = $row + [int]1
} until (!$Sheet.Cells.Item($row,1).Text)
我们如何询问用户评论?
您可以使用InputBox
类中的Microsoft.VisualBasic.Interaction
方法:
# this will import the Microsoft.VisualBasic assembly and make
# the Interaction class available in $vbi
# add those two lines to the top of your script
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$vbi = [Microsoft.VisualBasic.Interaction] # convenience variable
# ask the user for a comment
$comment = $vbi::InputBox("Write a comment about that website:", "Comment")
if ($comment -eq "") {
echo "input box was cancelled!"
}
else {
echo "comment:`t$comment"
}
最后,您的代码可能如下所示:
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$vbi = [Microsoft.VisualBasic.Interaction] # convenience variable
$ExcelObject = New-Object -comobject Excel.Application
$ExcelObject.Visible = $true
$ExcelObject.DisplayAlerts = $False
$excelFile = "C:\Users\muafzal\Documents\Files\EMIE\Analyzing\list.xlsx"
$Workbook = $ExcelObject.workbooks.open($excelFile)
$Sheet = $Workbook.Worksheets.Item(1)
$row = [int]2
$domain = @() # beginnt bei 2,1... 3,1... 4,1
Do {
$domain += $Sheet.Cells.Item($row,2).Text
# browser magic here:
#Start IE and make it visible
$ie = new-object -com "InternetExplorer.Application"
$ie.Visible = $true
#Navigate to the URL
$ie.Navigate($url)
# Output URL that have been visited to a text file.
$url | Out-File $done -Append
#Sleep while IE is running
while($ie.visible){
start-sleep -s 1
}
# ask the user for a comment
$comment = $vbi::InputBox("Write a comment about that website:", "Comment")
if ($comment -eq "") {
# cancel was pressed, so maybe revisit later?
$Sheet.Cells.Item($row, 1).Value = 'no'
}
else {
# edit exel sheet:
$Sheet.Cells.Item($row, 1).Value = 'yes'
$sheet.Cells.Item($row, 3).Value = $comment
}
# next row...
$row = $row + [int]1
} until (!$Sheet.Cells.Item($row,1).Text)
PS:我没有安装excel来测试代码,但我认为它应该可以正常运行。我希望这是你真正想知道的;)