Arrange columns in output of commands

89 views
Skip to first unread message

Bobby Clowers

unread,
Oct 16, 2024, 2:56:17 PM10/16/24
to GAM for Google Workspace
Is there a way to arrange columns in a specific order when it gets sent to drive.
Ex. gam print users allfields todrive
Instead of primaryEmail being the first column I would like name.fullName to be the first column. 
More than just that one but just as example.
I did try to use csv_output_header_filter and put the column names in the order I would like but it still didn't do it.

Any Ideas or what am I doing wrong if this is possible?

Thanks for any input.

Ross Scroggs

unread,
Oct 16, 2024, 3:10:30 PM10/16/24
to google-ap...@googlegroups.com
Bobby,

There is no code for ordering headers; some commands do have a sort option.
I'll see if I can think of something straightforward.

Ross
----
Ross Scroggs



--
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/429403b2-cefb-4705-be3e-17404c496d99n%40googlegroups.com.

✉ Kevin Melillo

unread,
Oct 16, 2024, 3:23:15 PM10/16/24
to google-ap...@googlegroups.com
You can easily re-order them once it is in a Google Sheet... just drag head column letter left or right.

✉ Kevin Melillo

unread,
Oct 16, 2024, 3:28:30 PM10/16/24
to google-ap...@googlegroups.com
If you do this on a regular basis for the same columns, you could always use Apps Script to re-order them.  Just include the order needed in the constfinalOrder list.

function myFunction() {
  // Please set the final order of headers.
  const finalOrder = ["header2", "header5", "header1", "header4", "header3"];

  const sheet = SpreadsheetApp.getActiveSheet();
  const obj = sheet
    .getRange(1, 1, 1, sheet.getLastColumn())
    .getValues()[0]
    .reduce(
      (ar, h, i) => [...ar, { from: i + 1, to: finalOrder.indexOf(h) + 1 }],
      []
    )
    .sort((a, b) => (a.to > b.to ? 1 : -1));

  for (let i = 0; i < obj.length; i++) {
    if (obj[i].from != obj[i].to) {
      sheet.moveColumns(sheet.getRange(1, obj[i].from), obj[i].to);
      obj.forEach((e, j) => {
        if (e.from < obj[i].from) obj[j].from += 1;
      });
    }
  }
}

Ian Crew

unread,
Oct 16, 2024, 3:35:53 PM10/16/24
to google-ap...@googlegroups.com
For me, I’m often feeding the results in CSV form to another process, not via Sheets*, so having a way to ensure that the columns are in a consistent order would be really useful.

*-For example, importing the results into BigQuery using the “bq” command line tool.

Cheers,

Ian


--
Ian Crew

Architect, Communication and Collaboration Services
Productivity & Collaboration Services
Berkeley IT
University of California, Berkeley


Ross Scroggs

unread,
Oct 16, 2024, 3:51:10 PM10/16/24
to google-ap...@googlegroups.com
I was thinking along these lines:
csv_output_header_order "header1,header2,...."
header1 and header 2 will appear first; all other headers will follow in whatever order they were originally in.

Ross
----
Ross Scroggs


Ross Scroggs

unread,
Oct 16, 2024, 4:13:27 PM10/16/24
to google-ap...@googlegroups.com
There is csv_output_header_force "header1,header2,header2,..." in which you specity the exact headers and order that you want.

Ross
----
Ross Scroggs


Bobby Clowers

unread,
Oct 16, 2024, 9:03:04 PM10/16/24
to google-ap...@googlegroups.com

Thanks for all the replies.  Kevin the app script idea kinda neat could be useful for other things also.I've used app script very little so not sure how all the commands work.  Ian  I haven't really used BQ so not super familiar with it how how to use it.  Wouldn't mind knowing more on how you're doing it.  Ross are those new?  I didn't see them in the csv output section but sounds like it would work 


You received this message because you are subscribed to a topic in the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-manager/J21a_PnNDKI/unsubscribe.
To unsubscribe from this group and all its topics, 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/A8BC3D54-1617-4A17-B59B-57652A9446AB%40gmail.com.

Ross Scroggs

unread,
Oct 16, 2024, 9:10:57 PM10/16/24
to google-ap...@googlegroups.com
Bobby,

Wiki updated:

* `csv_output_header_force` - A list of <Strings> used to specify the exact column headers to include


Ross

----
Ross Scroggs


Bobby Clowers

unread,
Oct 17, 2024, 9:43:38 AM10/17/24
to GAM for Google Workspace
Ross,
It seems to still not work here is what I'm trying to get
gam config csv_output_header_force name.fullName,primaryEmail,externalIds.0.value,aliases.0,aliases.1,orgUnitPath,lastLoginTime,creationTime,isEnrolledIn2Sv,includeInGlobalAddressList,isAdmin,suspended,isEnforcedIn2Sv,isEnrolledIn2Sv,Licenses,LicensesDisplay

Here is what comes out

primaryEmail
aliases.0aliases.1creationTimeexternalIds.0.valueincludeInGlobalAddressListisAdminisEnforcedIn2SvisEnrolledIn2SvlastLoginTimename.fullNameorgUnitPathsuspendedLicensesLicensesDisplay

Reply all
Reply to author
Forward
0 new messages