Home > OS >  Filter the output of Logger.log to only show specific parts
Filter the output of Logger.log to only show specific parts

Time:11-30

I have a simple function (shown below) that lists all the existing schema values for a Google Workspace domain. I would like to only retrieve specific items.

This is my function:

function listSchema() {

const ss = SpreadsheetApp.getActive();
const sheet = ss.getSheetByName("Domain Schema");
const schemaSafeName = sheet.getRange(4,2).getValue();

try{
    Logger.log(AdminDirectory.Schemas.get('my_customer',schemaSafeName));
  }
  catch(error){
    const {code, message} = error.details;

    if(code === 400 || code === 404 || code === 409 || code === 412){
      console.log("Error 400 or 404 or 409 or 412");
    }
    else {
      console.log(`${code} - ${message}`);
    }
  }  
}

The Logger.log (prettified and commented) output is as follows:

{
 etag="eObIY8zBQ9kCV0dcsWouNipdJvA0", // DON'T WANT THIS
  
  displayName=Test Schema Group,
      
   fields=
     [
      {
       fieldId=87mf1ADjQwuyvPaKPIa2uw==, // DON'T WANT THIS
       readAccessType=ADMINS_AND_SELF,
       displayName=Test Field1,
       fieldType=BOOL,
       fieldName=Test_Field1,
       etag="eObIY8zBQ9kCV03CoXVqpKE7PI", // DON'T WANT THIS
       multiValued=false,
       kind=admin#directory#schema#fieldspec // DON'T WANT THIS
      },
      {
       multiValued=false,
       fieldName=Test_Field2,
       displayName=Test Field2,
       etag="eObIY8zBQ9kCSggLJvV_R8EVdhq8S0O3A", // DON'T WANT THIS
       fieldType=BOOL,
       fieldId=7pRb-wPQQE2cyVvniaJA==, // DON'T WANT THIS
       readAccessType=ADMINS_AND_SELF,
       kind=admin#directory#schema#fieldspec // DON'T WANT THIS
      }
      ],
          
  schemaId=tAq6fq92Qn-6egbHjFFkug==, // DON'T WANT THIS
  kind=admin#directory#schema, // DON'T WANT THIS
  schemaName=Test_Schema_Group133
}

`

Note: every time I run it the order is different, so I guess using something to retrieve parts of the output using some sort of index would not work. But I may be wrong.

I'd like to get a "filtered" Logger.log output as follows: `

{
  displayName=Test Schema Group,
   fields=
     [
      {
       readAccessType=ADMINS_AND_SELF,
       displayName=Test Field1,
       fieldType=BOOL,
       fieldName=Test_Field1,
       multiValued=false,
      },
      {
       multiValued=false,
       fieldName=Test_Field2,
       displayName=Test Field2,
       fieldType=BOOL,
       readAccessType=ADMINS_AND_SELF,
      }
      ],
          
  schemaName=Test_Schema_Group133
}

`

So, basically, I'd like the output to ignore the values of "fieldId", "kind", and "etag".

Additionally - and ideally -, I'd also like to have the option to only the value, without the name, such as: "Test Schema Group" instead of "displayName=Test Schema Group". This way I can then push those values to a Google Sheet, with a pre-defined title for each row, without the need to filter it (using the Index function).

If I understand how to do this, I can then adapt it to get only any given value. I hope...

Thanks in advance for any help.

CodePudding user response:

Since you need to handpick what you want anyway, you might as well do it like this:

var data = {
  etag: "eObIY8zBQ9kCV0dcsWouNipdJvA0",
  displayName: "Test Schema Group",
  fields: [{
      fieldId: "87mf1ADjQwuyvPaKPIa2uw==",
      readAccessType: "ADMINS_AND_SELF",
      displayName: "Test Field1",
      fieldType: "BOOL",
      fieldName: "Test_Field1",
      etag: "eObIY8zBQ9kCV03CoXVqpKE7PI",
      multiValued: false,
      kind: "admin#directory#schema#fieldspec"
    },
    {
      multiValued: false,
      fieldName: "Test_Field2",
      displayName: "Test Field2",
      etag: "eObIY8zBQ9kCSggLJvV_R8EVdhq8S0O3A",
      fieldType: "BOOL",
      fieldId: "7pRb-wPQQE2cyVvniaJA==",
      readAccessType: "ADMINS_AND_SELF",
      kind: "admin#directory#schema#fieldspec"
    }
  ],
  schemaId: "tAq6fq92Qn-6egbHjFFkug==",
  kind: "admin#directory#schema",
  schemaName: "Test_Schema_Group133"
}
var ret = {};
if(data.schemaName == "Test_Schema_Group133"){
ret.displayName = data.displayName;
ret.schemaName = data.schemaName;
var fields = [];
for (var f of data.fields) {
  var obj = {};
  obj.readAccessType = f.readAccessType;
  obj.displayName = f.displayName;
  obj.fieldType = f.fieldType;
  obj.fieldName = f.fieldName;
  obj.multiValued = f.multiValued;
  fields.push(obj);
}
ret.fields = fields;
}
console.log(ret);

There are alternatives with Object.keys() or Object.getOwnPropertyNames(). But they will do essentially the same...

  • Related