在Excel字段中存储动态数据

在Excel字段中存储动态数据,excel,Excel,我有一个命令,返回用户在AWS中所属的组数。用户可以属于的组数可能会有所不同 我想将这些数据存储在excel中,每个用户的名字旁边都有一个组列表。我希望每个小组都有自己的领域。但群体的数量可能会有所不同 例如,以下用户都有不同数量的组: aws iam list-groups-for-user --user-name tdunphy --profile=company-prod | jq -r '.Groups[].GroupName' grp-quicksight agility-admi

我有一个命令,返回用户在AWS中所属的组数。用户可以属于的组数可能会有所不同

我想将这些数据存储在excel中,每个用户的名字旁边都有一个组列表。我希望每个小组都有自己的领域。但群体的数量可能会有所不同

例如,以下用户都有不同数量的组:

aws iam list-groups-for-user --user-name tdunphy --profile=company-prod  | jq -r  '.Groups[].GroupName'
grp-quicksight
agility-admin
grp-account-bill
grp-sag
grp-flow-log-user
company_SAG
grp-cloud-formation
grp-cloudops


aws iam list-groups-for-user --user-name broberts  --profile=company-prod  | jq -r  '.Groups[].GroupName'
agility-admin
grp-account-bill
grp-sag
grp-cloud-formation
grp-cloudops


aws iam list-groups-for-user --user-name ejimenez  --profile=company-prod  | jq -r  '.Groups[].GroupName'
agility-admin
grp-cloudops
理想情况下,我希望以如下方式水平显示此数据:

User Name  Groups
tdunphy    grp-quicksight agility-admin    grp-account-bill grp-sag (etc)
broberts   agility-admin  grp-account-bill grp-sag          grp-cloud-formation
每个组名都应位于其自己的列中。但我不确定这样看起来是否整洁。我愿意横向列出组名,如果这样看起来更好的话

我将把AWS命令中的组名转储到文本文件中。然后以编程方式将该文本文件转储到CSV中


如何存储每个用户的每个组列表,每个组都有自己的字段?我无法预测一个用户将属于多少组。是否有一种干净的方法来实现这一点?

此代码生成以下数据。 有几个假设

  • AWS原始数据位于名为“AWS_输入”的表格中(尽管这是一个变量)
  • 该数据在A列中,从第1行开始
  • 输出在名为“aws_输出”的工作表中创建(这也是一个变量)
  • 输出从A列的第2行开始。这允许单元格A1有一个标题,例如“用户名”

子构建awsgroups()
Dim AWSPrefix为长,FindName为长,NextFree为长,LastFree为长,EndRow为长,runningtotal为长
将用户名设置为字符串
变暗AWS变长,输出变长
将wsi、wso设置为工作表
“说出床单的名字
设置wsi=图纸(“aws\U输入”)
设置wso=图纸(“aws\U输出”)
'AWS前缀的长度->用户的AWS iam列表组--user name
AWSPrefix=41
'为所有变量设置值
FindName=0
NextFree=0
LastFree=0
EndRow=0
runningtotal=0
awsrow=1
outputrow=2
'在列A(1)中查找最后一个非空单元格
EndRow=wsi.Cells(Rows.Count,1).End(xlUp).Row
“只要不超过最后一行数据,就可以做一些事情

运行Total时您想垂直还是水平显示结果?您的数据是在一个单独的文件中,还是在现有工作簿中的一列数据中。我应该要求您根据上面显示的数据向我们展示首选布局。这将使解决方案的结构变得容易。嗨@Tedinoz!我刚刚更新了OP,尝试回答您的问题。如果我需要进一步澄清,请告诉我。谢谢我认为这只是一个意见问题。就我个人而言,我会将其存储为“user | group”,用户会在他们所属的每个组旁边的行中重复。这就像是一个合适的表,可以通过这种方式进行数据透视,或者最终迁移到合适的数据库中。@bluethundr谢谢。你对格式-用户|组与组|用户的结论是什么?你真的更喜欢后者吗?
Sub BuildAWSGroups()    
Dim AWSPrefix As Long, FindName As Long, NextFree As Long, LastFree As Long, EndRow As Long, runningtotal As Long    
Dim UserName As String    
Dim awsrow As Long, outputrow As Long    
Dim wsi, wso As Worksheet    

' name the sheets    
Set wsi = Sheets("aws_input")    
Set wso = Sheets("aws_output")    


' length of the AWS prefix -> aws iam list-groups-for-user --user-name    
AWSPrefix = 41    

'set values to all the variables    
FindName = 0    
NextFree = 0    
LastFree = 0    
EndRow = 0    
runningtotal = 0    
awsrow = 1    
outputrow = 2    

'Find the last non-blank cell in column A(1)    
EndRow = wsi.Cells(Rows.Count, 1).End(xlUp).Row    

' do stuff so long as you don't go past the last row of data    
Do While runningtotal <= EndRow    

' the last test for NextFree throws an error, so we'll look for the error    
On Error Resume Next    
' get the next non-blank row number    
NextFree = wsi.Range("a" & awsrow & ":a" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row    
' is there was an error then we need to calculate the next non-blank cell differently
If NextFree = LastFree Then
    NextFree = Cells.Find(What:="*", _
    After:=Range("a" & (LastFree + 2)), _
    LookAt:=xlPart, _
    LookIn:=xlFormulas, _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, _
    MatchCase:=False).Row
NextFree = awsrow + NextFree

End If

' find location of the user name in the aws data row
wsi.Activate
FindName = InStr((AWSPrefix + 1), wsi.Cells(awsrow, 1).Value, " ")
'extract the user name
UserName = Trim(Mid(wsi.Cells(awsrow, 1).Value, (AWSPrefix + 1), (FindName - AWSPrefix)))
'copy user name to output
wso.Cells(outputrow, 1).Value = UserName

' copy the aws groups for this user
wsi.Activate
wsi.Range(Cells((awsrow + 1), 1), Cells((NextFree - 1), 1)).Copy
'paste and transpose the groups to the user row on the output sheet
wso.Activate
wso.Cells(outputrow, 2).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True

' update variables for the loop
LastFree = NextFree
awsrow = LastFree + 2
outputrow = outputrow + 1
runningtotal = awsrow

Loop
End Sub