google-apps-scriptgoogle-api

for loop in Google Apps script to add specific values from a json response to a Google sheet


I've been trying to pull specific values from an API JSON response into a Google Sheet using the Cloud Identity API, specifically the policies part. The API response is working and im getting the values I want back, I just cant seem to get it to add all the responses to 228 rows on a Google Sheet, as im having trouble getting the rows to fill out. I can only seem to get it to fill out row A2 and nothing else. I think its an issue with the for loop, but I cant figure it out. I'll show code, sample JSON response and sheet below. Any help appreciated.

function get_settings(){

const ss = SpreadsheetApp.getActiveSpreadsheet();
const settingsSheet = ss.getSheetByName("07. settings api");

const api_key = PropertiesService.getScriptProperties().getProperty('apikey');
var url = "https://cloudidentity.googleapis.com/v1beta1/policies?key=" + api_key;

const params = {
  headers: {
    "Authorization": 'Bearer ' + ScriptApp.getOAuthToken()
  }
};

var response = UrlFetchApp.fetch(url, params);
var data = response.getContentText();
var json = JSON.parse(data);

for (let i = 0; i < 229; i++){
let settingsData = [];
settingsData.push(json.policies[0].name);
settingsData.push(json.policies[0].setting.type);
settingsData.push(json.policies[0].setting.value.serviceState);
settingsData.push(json.policies[0].setting.value.enableAccountRecovery);



//console.log(settingsData);

let ssdata = [];
ssdata.push(settingsData);

let targetRange = settingsSheet.getRange('A2:A229');
targetRange.setValues(ssdata);
}

}

JSON Response

{
  "policies": [
    {
      "name": "policies/axynr375bw46nm5dcsjnvrfpuefam",
      "customer": "customers/C03r7ld3j",
      "policyQuery": {
        "query": "entity.org_units.exists(org_unit, org_unit.org_unit_id == orgUnitId('03ph8a2z32dhpy9'))",
        "orgUnit": "orgUnits/03ph8a2z32dhpy9",
        "sortOrder": 101
      },
      "setting": {
        "type": "settings/security.super_admin_account_recovery",
        "value": {
          "enableAccountRecovery": true
        }
      },
      "type": "SYSTEM"
    },
    {
      "name": "policies/axynr375bw46nm5dctezjp4byyksa",
      "customer": "customers/C03r7ld3j",
      "policyQuery": {
        "query": "entity.org_units.exists(org_unit, org_unit.org_unit_id == orgUnitId('03ph8a2z32dhpy9')) && entity.licenses.exists(license, license in ['/product/101034/sku/1010340001','/product/Google-Apps/sku/1010020020','/product/Google-Apps/sku/1010020025','/product/Google-Apps/sku/1010020026','/product/Google-Apps/sku/1010020027','/product/Google-Apps/sku/1010020028','/product/Google-Apps/sku/1010020030','/product/Google-Apps/sku/1010020031'])",
        "orgUnit": "orgUnits/03ph8a2z32dhpy9",
        "sortOrder": 101
      },
      "setting": {
        "type": "settings/gmail.email_spam_filter_ip_allowlist",
        "value": {
          "allowedIpAddresses": [
            "172.217.25.142"
          ]
        }
      },
      "type": "SYSTEM"
    },
    {
      "name": "policies/axynr375bxlm5l5gc6cib7vrw4lsg",
      "customer": "customers/C03r7ld3j",
      "policyQuery": {
        "query": "entity.org_units.exists(org_unit, org_unit.org_unit_id == orgUnitId('03ph8a2z32dhpy9'))",
        "orgUnit": "orgUnits/03ph8a2z32dhpy9",
        "sortOrder": 101.00002
      },
      "setting": {
        "type": "settings/play_console.user_takeout",
        "value": {
          "takeoutStatus": "ENABLED"
        }
      },
      "type": "SYSTEM"
    },
    {
      "name": "policies/axynr375bwa3nh5uckijlmho3afcg",
      "customer": "customers/C03r7ld3j",
      "policyQuery": {
        "query": "entity.org_units.exists(org_unit, org_unit.org_unit_id == orgUnitId('03ph8a2z32dhpy9'))",
        "orgUnit": "orgUnits/03ph8a2z32dhpy9",
        "sortOrder": 101.00005
      },
      "setting": {
        "type": "settings/blogger.user_takeout",
        "value": {
          "takeoutStatus": "ENABLED"
        }
      },
      "type": "SYSTEM"
    },
    {
      "name": "policies/axynr375bx2oz6wsbpunrgxt7qbcg",
      "customer": "customers/C03r7ld3j",
      "policyQuery": {
        "query": "entity.org_units.exists(org_unit, org_unit.org_unit_id == orgUnitId('03ph8a2z32dhpy9'))",
        "orgUnit": "orgUnits/03ph8a2z32dhpy9",
        "sortOrder": 101.00016
      },
      "setting": {
        "type": "settings/books.user_takeout",
        "value": {
          "takeoutStatus": "ENABLED"
        }
      },
      "type": "SYSTEM"
    },
    {
      "name": "policies/axynr375bwi5l5wvb2t335wn7mlsg",
      "customer": "customers/C03r7ld3j",
      "policyQuery": {
        "query": "entity.org_units.exists(org_unit, org_unit.org_unit_id == orgUnitId('03ph8a2z32dhpy9'))",
        "orgUnit": "orgUnits/03ph8a2z32dhpy9",
        "sortOrder": 101.00021
      },
      "setting": {
        "type": "settings/location_history.user_takeout",
        "value": {
          "takeoutStatus": "ENABLED"
        }
      },
      "type": "SYSTEM"
    },
    {
      "name": "policies/axynr375bx25ndwad3wz7ehl3mkcg",
      "customer": "customers/C03r7ld3j",
      "policyQuery": {
        "query": "entity.org_units.exists(org_unit, org_unit.org_unit_id == orgUnitId('03ph8a2z32dhpy9'))",
        "orgUnit": "orgUnits/03ph8a2z32dhpy9",
        "sortOrder": 101.00025
      },
      "setting": {
        "type": "settings/play.user_takeout",
        "value": {
          "takeoutStatus": "ENABLED"
        }
      },
      "type": "SYSTEM"
    }

enter image description here


Solution

  • Modification points:

    When these points are reflected in your script, how about the following modification?

    Modified script:

    function get_settings() {
    
      const ss = SpreadsheetApp.getActiveSpreadsheet();
      const settingsSheet = ss.getSheetByName("07. settings api");
    
      const api_key = PropertiesService.getScriptProperties().getProperty('apikey');
      var url = "https://cloudidentity.googleapis.com/v1beta1/policies?key=" + api_key;
    
      const params = {
        headers: {
          "Authorization": 'Bearer ' + ScriptApp.getOAuthToken()
        }
      };
    
      var response = UrlFetchApp.fetch(url, params);
      var data = response.getContentText();
      var json = JSON.parse(data);
    
    
      // I modified the script below.
      var values = json.policies.map(o => [
        o.name || null,
        o.setting?.type || null,
        o.setting?.value?.serviceState || null,
        o.setting?.value?.enableAccountRecovery || null,
      ]);
      settingsSheet.getRange(2, 1, values.length, values[0].length).setValues(values);
    }
    

    Reference: