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

本小妞迷上赌 提交于 2019-12-06 06:18:27

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);
    

Thanks for this post! I reworked your code for some of my needs to return the array of matching deals directly.

I also changed the api request to only pull specific deal fields, pull back 500 deals, and use a PipeDriver filter we've setup.

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

   //the way the url is build next step is to iterate 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:(org_name,title,owner_name,status,pipeline_id)?start=";
  var limit  = "&limit=500";
  var filter = "&filter_id=1";
  var pipeline = 7; // put a pipeline id specific to your PipeDrive setup 
  var start  = 0;
//  var end  = start+50;
  var token  = "&api_token=your-api-token-goes-here"


  //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+limit+filter+token); 
  var dataAll = JSON.parse(response.getContentText()); 
  var dataSet = dataAll;

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

  for (var i = 0; i < dataSet.data.length; i++) {
    data = dataSet.data[i];

    if(data.pipeline_id === pipeline){ 
      rows.push([data.org_name, data.title, data.owner_name, data.status, data.pipeline_id]);//your JSON entities here
    } 
  }
  Logger.log( JSON.stringify(rows,null,2) );   // Log transformed data

  return rows;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!