gpt4 book ai didi

html - 如何格式化 InternetExplorer 对象中的表格?

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:45 24 4
gpt4 key购买 nike

我正在寻找有关我编写的这个脚本的输出格式的帮助。该脚本有效,但我的问题是关于输出结果。

使用此脚本,我可以从 AD 中获取数据并以 objIE 形式输出结果。我这样做是为了让信息看起来整洁。我的问题是如何将 HTML 编码到 VBScript 中(而不是将 VBS 编码到 HTML 中)。下面是结果的图片,但我不确定如何进行样式编辑(下图)。

这是我的代码:

Call FindPCsThatUserLoggedInto

Sub FindPCsThatUserLoggedInto()
'Get name to search for
strUser = InputBox("Please Enter User's First Name")

If strUser <> "" Then
strLast = InputBox("Please Enter User's Last Name")

If strLast <> "" Then
'Set location parameter
strLocation = ("Location")

'Set AD Constant
Const ADS_SCOPE_SUBTREE = 2

'Create objects
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")

'Open Active Directory
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

'Set Active Directory connection object
Set objCommand.ActiveConnection = objConnection

' Set AD Command properties
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

'Begin building HTML string headers
s = "<table style = ""width:100%"" border = ""1""><tr><th>Name:</th><th>Username:</th><th>Location:</th><th>Employee ID:</th><th>Contractor ID:</th><th>Badge ID:</th></tr>"

'Issue AD command
objCommand.CommandText = "SELECT ADSPath FROM 'LDAP://dc=,dc=,dc=com' WHERE givenName = '" & strUser & "*' AND sn = '" & strLast & "*' And physicalDeliveryOfficeName = '" & strLocation & "'"

'Set RecordSet object to results
Set objRecordSet = objCommand.Execute

'Make sure there are records returned
If objRecordSet.Recordcount > 0 Then
'Point to first record
objRecordSet.MoveFirst

'Loop through all records
Do Until objRecordSet.EOF
'Set user object
Set objUser = GetObject(objRecordSet.Fields("ADSPath").Value)

'Create temporary user string of the user name reversed twice
strUser = strReverse(objUser.samaccountname) & strReverse(objUser.samaccountname)

'Create temporary badge # reversed twice
strBadge = strReverse(objUser.BadgeID) & strReverse(objUser.BadgeID)

'Create TEMPID interlacing the first 5 characters of the two temp strings
strTEMPID = MID(strUser,1,1)&MID(strBadge,1,1)&MID(strUser,2,1)&MID(strBadge,2,1)&MID(strUser,3,1)&MID(strBadge,3,1)&MID(strUser,4,1)&MID(strBadge,4,1)&MID(strUser,5,1)&MID(strBadge,5,1)

'Populate HTML table with results
s = s & "<tr><td>" & objUser.DisplayName & "</td><td>" & objUser.samaccountname & "</td><td>" & objUser.physicalDeliveryOfficeName & "</td><td>" & objUser.EmployeeNumber & "</td><td>" & objUser.ContractorID & "</td><td>" & objUser.BadgeID & "</td><td>"

'Move to next record
objRecordSet.MoveNext
Loop

'Finish HTML string
s = s & "</table>"

'Create IE object and assign our HTML string to the body
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
objIE.ToolBar = 0
objIE.StatusBar = 0
Set objDoc = objIE.Document.Body
objDoc.InnerHTML = s
objIE.Visible = True
Else
'Inform user of no records
MsgBox "No users matching that criteria exist in AD."
End If
End If
End If
End Sub

结果如下:

actual format

这是我希望输出的样子:

desired format

我知道创建它的 HTML 代码 - 我只是不确定将 HTML 代码放在 VBScript 的什么位置以使其输出它。

最佳答案

您通常通过 CSS 进行这种格式化在 HTML 中。使用 InternetExplorer.Application 对象,您可以像这样定义样式表:

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate "about:blank"
...

Set css = objIE.Document.CreateStyleSheet
css.AddRule "table", "border: 1px solid lightgray; border-collapse: collapse;"
css.AddRule "th", "color: black; background-color: white; font-weight: bold;"
css.AddRule "th, td", "border: 1px solid lightgray;"
css.AddRule "tr:nth-child(odd)", "background-color: lightgray;"
css.AddRule "tr:nth-child(even)", "background-color: white;"

...

:nth-child 伪类允许您在偶数行和奇数行之间交替行颜色。

关于html - 如何格式化 InternetExplorer 对象中的表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26760137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com