2014-10-09 18 views
5

Ho creato un foglio di calcolo Google con cinque colonne;È possibile chiamare l'API esterna in Google Spreedsheet?

Una volta che l'utente ha riempito i valori nelle prime tre colonne, deve chiamare un'API di terze parti e riempire il valore (risposta) nella quarta e quinta colonna.

È possibile scrivere un codice in Google Spreadsheet per la chiamata API? È possibile chiamare e ottenere risposta da API esterne in Google Spreadsheet?

+0

Questa è una buona domanda - hai trovato una risposta ancora? – user531065

+1

Non ho trovato la risposta. Spero non sia possibile (non sono sicuro). L'ho fatto come applicazione indipendente. –

risposta

6

C'è un modo per effettuare chiamate API e avere i risultati in un foglio di calcolo - l'unico modo per farlo è creare/aprire il foglio di calcolo di destinazione, andare a strumenti e quindi a Editor di script, e usarlo come limite script:

function Maestro() { 
var ss = SpreadsheetApp.getActiveSpreadsheet(); //get active spreadsheet (bound to this script) 
var sheet = ss.getSheetByName('mae'); //The name of the sheet tab where you are sending the info 

var apiCall = 'getUpcomingConference'; 
var apiKey = '_____key here______'; 
var apiToken = '______security token______'; 
var url = 'http://myaccount.maestroconference.com/_access/' + apiCall +"?customer=" + apiKey + "&key=" + apiToken; //api endpoint as a string 

var response = UrlFetchApp.fetch(url); // get api endpoint 
var json = response.getContentText(); // get the response content as text 
var mae = JSON.parse(json); //parse text into json 

Logger.log(mae); //log data to logger 

var stats=[]; //create empty array to hold data points 

var date = new Date(); //create new date for timestamp 

//The number in brackets refers to which instance we are looking at - soonest upcoming call is [0], next after that is [1], etc. 
stats.push(date); //timestamp 
stats.push(mae.value.conference[0].name); 
stats.push(mae.value.conference[0].scheduledStartTime); 
stats.push(mae.value.conference[0].UID); 

//append the stats array to the active sheet 
sheet.appendRow(stats); 
} 

Ha bisogno di una piccola interfaccia di lavoro ma funzioni! Prende informazioni da una chiamata API e le inserisce in un foglio di calcolo.

0

Ultimamente mi sono imbattuto nello stesso requisito per leggere le righe del foglio e inviare i dati in richiesta e registrare la risposta. Ho pensato di condividere quello che ho lavorato fuori dopo un po 'googling ...

function testing_this() { 
    var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues(); 
    for (row in data) { 
     Logger.log(data[row]); 

     var row = data[row] 
     var options = { 
      'method': 'post', 
      'payload': { email:row[1]} 
     }; 
     // sending to API. for example: 
     UrlFetchApp.fetch('https://your-rest-api-url/v1/customers/', options); 
    } 
} 

Se si desidera ottenere i dati nel foglio si dovrebbe usare la funzione:

var response = UrlFetchApp.getRequest("http://your-api-url/"); 
    for(data in response) { 
     var respData = response[data]; 
     // do whatever u want to do with this data... 
    } 

spero che sia utile a tutti coloro che si trovano ad affrontare requisiti simili come sopra.

Ho postato questo script in github se si vuole forcella/pull ...

https://github.com/joshiparthin/gsheetScriptExperiments/blob/master/readAndSendToApi.js

Cheers,

Parth

Problemi correlati