Is there a way to pick a random color from the list of possible options in labels?

3 views
Skip to first unread message

Pedro Lima

unread,
Jul 2, 2024, 9:54:34 PM (11 hours ago) Jul 2
to GAM for Google Workspace
Hi, i'm trying to iteratively generate one label for each member of a given group. i'd like to generate a random color for each of them, but since gam's cli scripts abstract the looping away, i can't find a way to do it.

This is how i'd do it high level if i was programming it in python or something

for group in groups.csv
 for group,email in group_elements.csv
  random_color =  generate_random_color()
  for person in group
    add label "email" backgroundcolor random_color


currently i'm doing something like
gam redirect csv ./group_elements.csv multiprocess csv ./groups.csv gam print group-members group "~group"
gam csv ./group_elements.csv gam group "~group" add label "~email" labellistvisibility showifunread backgroundcolor "#ffad46 textcolor "#ffffff"

but i'd like to select backgroundcolor random, and it would fetch an option from the list  of possible options:
 <#16a765|#2da2bb|#42d692|#4986e7|#98d7e4|#a2dcc1|#b3efd3|#b6cff5|#b99aff|#c2c2c2|#cca6ac|#e3d7ff|#e7e7e7|#ebdbde|#f2b2a8|#f691b2|#fb4c2f|#fbd3e0|#fbe983|#fdedc1|#ff7537|#ffad46|#ffc8af|#ffdeb5>
  

Maj Marshall Giguere

unread,
12:02 AM (9 hours ago) 12:02 AM
to google-ap...@googlegroups.com
Pedro;

GAM, of either flavor, has no mechanism to do embedded loops, other than the ability to iterate over a csv file.  There are two potential ways to tackle this, they are variations on a theme.

First, you can use whatever shell you are using to loop over the groups and members and execute a single gam command for each group/member pair.  Second, you can install advanced gam as a python library and essentially do as suggested in the first suggestion.  You would still be building up each gam command individually, then using exec to invoke each command, or you could send each constructed command to a file which could be executed using the gam batch capability, my personal preference in most scenarios.

Here is an example using the bash shell.  It's a pretty ugly almost one liner.  This sample sends each constructed gam command to stdout, you can collect all the commands into a gam batch file.   I'm sure you can do something similar with powershell.  Sometimes you just have to wrap what you want to do in a script.

# Array of colors
Colors=(
'#16a765'

'#2da2bb'
'#42d692'
'#4986e7'
'#98d7e4'
'#a2dcc1'
'#b3efd3'
'#b6cff5'
'#b99aff'
'#c2c2c2'
'#cca6ac'
'#e3d7ff'
'#e7e7e7'
'#ebdbde'
'#f2b2a8'
'#f691b2'
'#fb4c2f'
'#fbd3e0'
'#fbe983'
'#fdedc1'
'#ff7537'
'#ffad46'
'#ffc8af'
'#ffdeb5'
)
# get all members of all groups, then add the labels with random color selection

gam redirect csv - print group-members fields group,email|tail -n +2|(IFS=\,;while read group member;do echo gam group \"$group\" add label \"$member\" labellistvisibility showifunread backgroundcolor \"$(echo ${Colors[$(shuf -i 0-23 -n 1)]})\" textcolor \"$(echo ${Colors[$(shuf -i 0-23 -n 1)]})\";done)


Hopefully you will find this useful, or at least entertaining.  It was an interesting diversion anyway.

--
You received this message because you are subscribed to the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/a2c7c8cd-88a7-4f7f-924e-42c74cf66271n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages