Home > Back-end >  (JS) Google Script App Search / Filter for keyword
(JS) Google Script App Search / Filter for keyword

Time:06-12

I have been stumped on this for a while. I am fairly new to Google script app and wanted to see if there is a way to make this happen. So far, I've used a few methods within Google Sheet but seem to not get it working.

The code below does give me an output of all the data, however, the data that is nested in the data.custom_fields[x] has multiple objects that is separated by ",". I would like to be able to filter out the other key words and just use whatever is inside "display_value=". The display_value= is not always in the same area so have to run a search for them.

I am assuming some kind of If statement would be used here..

An example of the object is:

{type=x, resource_subtype=x, created_by={name=x, gid=x, resource_type=x}, display_value=Cool Value, description=x, enabled=x, resource_type=custom_field, gid=x, enum_options=[x.lang.Object;x, enum_value={x}, name=x}

I've tried to split function as well but not sure how to filter out the words I need.

function Users() {
  var options = {
   "headers" : {
     "Authorization": "API Key here"
   }
  }
  var response = UrlFetchApp.fetch("URL here", options);

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var sheet = ss.getSheetByName("Tab Name here"); // specific sheet name getSheetByName(""); alternatively use ss.getActiveSheet()

  var dataAll = JSON.parse(response.getContentText()); //
  var dataSet = dataAll.data; // "data" is the key containing the relevant objects
  var rows = [],
    data;
  
  for (i = 0; i < dataSet.length; i  ) {
  data = dataSet[i];
  rows.push([
    data.gid,
    data.name, 
    data.permalink_url,
    data.due_on,
    data.custom_fields[1],
    data.custom_fields[2],
    data.custom_fields[4],
    data.custom_fields[5],
    data.custom_fields[6],
    data.custom_fields[7],
    data.custom_fields[8],
    data.custom_fields[9],
    
    ]); //your JSON entities here
  }

  // [row to start on], [column to start on], [number of rows], [number of entities]
  dataRange = sheet.getRange(2, 1, rows.length, 12);
  dataRange.setValues(rows);

Thank you in advance!

Example Image of JSON imported data

CodePudding user response:

Although they appear separated by ,'s, that is only how they're displayed in the log. Because you're using JSON.parse, you're receiving/converting to an Object, not a string.

Because data.custom_fields is an array of objects, you can access the property/key values as : data.custom_fields[x].display_value.

Learn More:

CodePudding user response:

If you want to extract display_value, try

let myVal = myData.match(/(?<=display_value=)[^,] /g)[0]

I guess that myData could be data.custom_fields[5], so replace it by

data.custom_fields[5].match(/(?<=display_value=)[^,] /g)[0]
  • Related