I would like to pull / import API data from theGraph into Google Sheets ( GS ) Then feed the imported data to columns / rows in a GS spreadsheet.
The solution by @Theza to the question ( Google Sheets: json into columns ) does this nicely and I have tried to use this as a starting point.
Editing the URL & query results in the data I want being pulled correctly and is displayed in the log
I cannot work out how I then get the logged info to feed to my spreadsheet ?!
Some help would be much appreciated 🙂
Below is the Apps Script I am working from:
function graphData(query,url){
var options = {"headers": {"Content-Type": "application/json"
},
"payload": JSON.stringify({query}),
"method": "POST"
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
return response;
};
function Pool () {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
sheet.appendRow(["pool-id", "token0Name", "token1Name", "token0Price", "token1Price"]);
// Original url
//var url = 'https://api.thegraph.com/subgraphs/name/openpredict/chainlink-prices-subgraph' ;
// MY API
var url = 'https://api.thegraph.com/subgraphs/name/stellaswap/pulsar' ;
// Original query
//var query = 'query{prices(first:2 where:{assetPair_in:["USDC/ETH","DAI/ETH "]} orderBy:timestamp orderDirection:desc) {assetPair{id} timestamp price}}';
// MY QUERY
var query = 'query { pool(id: "0x1b11d991f32fb59ec4ee744de68ad65d9e85b2d2") { id token0 { name } token1 { name } token0Price token1Price } }';
var a=graphData(query,url);
var code= a.getContent();
var json = a.getContentText(); // get the response content as text
var mae = JSON.parse(json); //parse text into json
// How do I edit the following to feed the log (response) data to columns / rows in the GS spreadsheet
var rows = [],dat,lastR=sheet.getLastRow()+1;
mae.data.prices.forEach(x => {rows[rows.length]=[x.assetPair.id,x.timestamp,x.price];});
sheet.getRange('A'+lastR+':C'+(lastR+rows.length-1)).setValues(rows);
}