在 Google sheet 秒内从 pipedrive API 导入数据。如何将数据复制到 sheet 中正确的列和行

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

我正在尝试导入所有 Pipedrive 交易并在 Google 表格中写入我需要的信息。

我已经能够做到的:访问提要并将数据解析为变量。 并将此变量打印到 1 个单元格中。当然这还行不通。

代码有点乱(边学边建)

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

}

我在 sheet.getRange 上遇到的错误。

我想要实现的是将数据放在 id、value、pipeline_id

列中

任何指向我需要去哪个方向解决这个问题的指示都会很棒!修复会很好,但一些指示对我的理解更有用。

我收到的错误如下:

afmetingen van het bereik zijn ongeldig 的协调员。 (regel 29, bestand 'dealpull5.0')

粗略翻译:

reach的大小坐标无效(第29行,文件"dealpull5.0")

您已从 Web API 检索数据并将其转换为二维数组(行数组,其中每一行都是单元格数组)。为了确认这一点,您可以在整理数据的循环之后添加一个 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

如果您 运行 这样做,您应该会在日志中看到类似这样的内容:

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

接下来,您想将其写入电子表格,如下所示:

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

错误消息抱怨范围大小与数据大小不匹配。

注意事项:

  • 因为有列 headers,第一个包含来自 API 调用的数据的单元格将是 A2,也表示为 R2C1
  • 使用Range.setValues()时,所提供数据的所有行的长度必须与范围的宽度相同。如有必要,您可能需要在调用 Range.setValues().
  • 之前填充或 trim 数据
  • 方便将范围大小与数据大小相匹配;这样,您就可以轻松地适应从 API.

    检索到的数据的变化
    var dataRange = sheet.getRange(2, 1, rows.length, rows[0].length);
    dataRange.setValues(rows);
    

感谢 post! 我根据我的一些需要修改了您的代码,直接 return 匹配交易数组。

我还更改了 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;
}