I am using Apps Script in Google Sheets to import JSON from Google Analytics. I have previously used the ImportJSON function to easily get the JSON from an open API just using =ImportJSON(url)
within a cell. Because I am using Apps Script to authorise the Analytics API stuff (this all works fine), I am also using it to insert the JSON data into my sheet. ImportJSON returns a two-dimensional array containing the data, with the first row containing headers. However, I am having difficulty getting the range of the data so that I can add the values to the sheet.
This is what I have:
function makeRequest() {
var analyticsService = getAnalyticsService();
var apiUrl = 'https://www.googleapis.com/analytics/v3/data/ga?ids=removed&start-date=30daysAgo&end-date=yesterday&metrics=ga%3AuniquePageviews&dimensions=ga%3ApagePath&sort=-ga%3AuniquePageviews&filters=ga%3ApagePath%3D%40%2FKnowledgeBank%2FFactsheetForFarmers.aspx&max-results=10&access_token=' + analyticsService.getAccessToken()
var sheet = SpreadsheetApp.getActiveSheet();
var jsonData = ImportJSON(apiUrl);
var cell = sheet.getRange(jsonData.length,jsonData[0].length);
cell.setValues(jsonData);
}
The error I get is Incorrect range height, was 3 but should be 1
ImportJSON
at GitHub for theImportJSON()
function.