Example 1: Extract Data by One Cell
Use one candidate ID from a sheet tab to retrieve name and party, and office information through an API call, and output results to another sheet tab.
function CandidateData() {
var key = "YOUR API KEY" //API Key
var ss = SpreadsheetApp.getActiveSpreadsheet() //Assign Google Sheets (including all tabs) to varialbe ss
var wsName = ss.getSheetByName("person") //Assign tab "person" to varialbe wsName
var name = wsName.getRange("A2").getValue() ////Extract candidate id from tab "person"
let apiURL = 'https://api-stage.open.fec.gov/v1/candidate/'+ name + '/?api_key=' + key
var resText = UrlFetchApp.fetch(apiURL).getContentText() //Make the API call to extract data in JSON file format
var resJSON = JSON.parse(resText) //Parse JSON data
var record = resJSON["results"][0]
var can_name = resJSON["results"][0]["name"]
var party = resJSON["results"][0]["party_full"]
var office_f = resJSON["results"][0]["office_full"]
//*****Output Results**********//
var wsLiveData = ss.getSheetByName("live") //**SETUP OUTPUT SHEET
var candidate_name = wsLiveData.getRange("A2") //SETUP A2 for Candidate Name
var party_full = wsLiveData.getRange("B2") //SETUP B2 for Party Name
var office_full = wsLiveData.getRange("C2") //SETUP C2 for Office
candidate_name.setValue(can_name) //SEND extracted value to var candidate_name
party_full.setValue(party) //SEND extracted value to var party_full
office_full.setValue(office_f) //SEND extracted value to var office_full
}Last updated
Was this helpful?
