[英]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.我正在尝试导入所有Pipedrive交易并在 Google 表格中写入我需要的信息。
What I am already able to do: Access the feed and parse the data to a variable.我已经能够做的:访问提要并将数据解析为变量。 And print this variable into 1 cell.并将此变量打印到 1 个单元格中。 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
.我在sheet.getRange
上sheet.getRange
的错误。
What I want to achieve is put the data in the columns id, value, pipeline_id我想要实现的是将数据放在列 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. de coördinaten of afmetingen van het bereik zijn ongeldig。 (regel 29, bestand 'dealpull5.0') (regel 29, bestand 'dealpull5.0')
Loosely translated:粗略翻译:
The coordinates of the size of the reach are invalid (line29,file "dealpull5.0")范围大小坐标无效(line29,file "dealpull5.0")
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.我还将 api 请求更改为仅拉取特定交易字段,拉回 500 笔交易,并使用我们设置的 PipeDriver 过滤器。
// 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;
}
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).您已从 Web API 检索数据,并将其转换为二维数组(行数组,其中每一行都是单元格数组)。 To confirm this, you can add a Logger call after the loop that grooms the data:要确认这一点,您可以在整理数据的循环之后添加一个 Logger 调用:
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:注意事项:
A2
, which is also expressed as R2C1
.由于有列标题,将包含来自 API 调用的数据的第一个单元格将是A2
,也表示为R2C1
。Range.setValues()
, all rows of the provided data must be the same length as the width of the range.使用Range.setValues()
,所提供数据的所有行的长度必须与范围的宽度相同。 If necessary, you may need to pad or trim the data before calling Range.setValues()
.如有必要,您可能需要在调用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.这样,您就可以轻松适应从 API 检索到的数据的变化。
var dataRange = sheet.getRange(2, 1, rows.length, rows[0].length); dataRange.setValues(rows);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.