Export all organization units in Google Workspace (apps script)

Reference Script below to copy & paste

-----------------------------------------

function exportOrgUnits() {

   // get google sheet as we'll write data to it later, change sheet name to match yours
   const sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1")


   // you can add more fields based on your requirements
   const fileArray = [
       ["Org Unit Name", "Org Unit ID", "Parent OrgUnit Path", "Parent OrgUnit Id"]
   ]
   const orgUnits = AdminDirectory.Orgunits.list("my_customer", {
       type: "ALL"
   }).organizationUnits.forEach(orgUnit => {
       fileArray.push([orgUnit.name, orgUnit.orgUnitId.slice(3), orgUnit.parentOrgUnitPath, orgUnit.parentOrgUnitId.slice(3)]) // you can add more fields based on your requirements


   })

   // write data back to our sheets
   sheet.getRange(1, 1, fileArray.length, fileArray[0].length).setValues(fileArray)

}


-----------------------------------------

Related Posts

....

....