Search code examples
google-apps-scriptgoogle-sheetsgoogle-appspipedrive-api

Importing data from pipedrive API in Google sheets. How to copy the data into the sheet in the correct columns and rows


I am trying to import all Pipedrive deals and writing the information I need in Google sheets.

What I am already able to do: Access the feed and parse the data to a variable. And print this variable into 1 cell. Of course this isn't workable yet.

The code is somewhat scavenged (building while I'm learning)

// Standard functions to call the spreadsheet sheet and activesheet
function alldeals() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var sheet = ss.getActiveSheet();

   //the way the url is build next step is to itterate between the end because api only allows a fixed number of calls (100) this way i can slowly fill the sheet.
  var url = "https://api.pipedrive.com/v1/deals?start=";
  var url2 = "&limit="
  var start = 0;
  var end = start+50;
  var token  = "&api_token=hiddenforobviousreasons"


  //call the api and fill dataAll with the jsonparse. 
//then the information is set into the 
//dataSet so that i can refill datall with new data.

  var response = UrlFetchApp.fetch(url+start+url2+end+token); 
  var dataAll = JSON.parse(response.getContentText()); 
  var dataSet = dataAll;


  //create array where the data should be put
  var rows = [], data;

  for (i = 0; i < dataSet.length; i++) {
    data = dataSet[i];
    rows.push([data.id, data.value,data.pipeline_id]); //your JSON entities here
  }

  dataRange = sheet.getRange(1, 1, rows.length, 3); // 3 Denotes total number of entites
  dataRange.setValues(rows);

}

The error I am getting on the sheet.getRange.

What I want to achieve is put the data in the columns id, value, pipeline_id

Any pointers to what direction I need to go to solve this problem would be awesome! A fix would be nice to but some pointers would be more useful for my understanding.

The error i am getting is the following:

De coördinaten of afmetingen van het bereik zijn ongeldig. (regel 29, bestand 'dealpull5.0')

Loosely translated:

The coordinates of the size of the reach are invalid (line29,file "dealpull5.0")


Solution

  • You've retrieved data from a web API, and transformed it into a 2-dimensional array (an array of rows, where each row is an array of cells). To confirm this, you can add a Logger call after the loop that grooms the data:

      var rows = [], data;
    
      for (i = 0; i < dataSet.length; i++) {
        data = dataSet[i];
        rows.push([data.id, data.value,data.pipeline_id]); //your JSON entities here
      }
    
      Logger.log( JSON.stringify(rows,null,2) );   // Log transformed data
    

    If you run that, you should see something like this in your logs:

    [--timestamp--] [ 
       [ id1, value1, pipeline_id1 ],
       [ id2, value2, pipeline_id2 ],
       ...
    ]
    

    Next, you want to write it into a spreadsheet, which looks like this:

         A       B         C
    1 | ID   | Value | Pipeline_Id |
      +------+-------+-------------+
    2 |      |       |             |
    3 |      |       |             |
    

    The error message is complaining that the size of the range and size of your data do not match.

    Things to note:

    • Since there are column headers, the first cell that will contain data from the API call will be A2, which is also expressed as R2C1.
    • When using Range.setValues(), all rows of the provided data must be the same length as the width of the range. If necessary, you may need to pad or trim the data before calling Range.setValues().
    • It is convenient to match the range size to the size of the data; that way, you can easily adapt to changes in the data retrieved from the API.

      var dataRange = sheet.getRange(2, 1, rows.length, rows[0].length);
      dataRange.setValues(rows);