首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将动态数据存储在Excel字段中

将动态数据存储在Excel字段中
EN

Stack Overflow用户
提问于 2018-09-13 21:40:56
回答 1查看 65关注 0票数 1

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

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

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

代码语言:javascript
复制
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

理想情况下,我希望像这样水平地显示这些数据:

代码语言:javascript
复制
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中。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-15 08:35:52

此代码生成以下数据。

有几个假设。

  • AWS原始数据位于一个名为"aws_input“的表中(尽管这是一个变量)。
  • 此数据位于A列中,并开始于第1行
  • 输出是在一个名为"aws_output“的表中创建的(这也是一个变量)
  • 输出以A列的row2开头。这允许单元格A1有一个标题,比如“用户名”。
代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52322169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档