繁体   English   中英

从 Google 表格中的 pipedrive API 导入数据。 如何将数据以正确的列和行复制到工作表中

[英]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.getRangesheet.getRange的错误。

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

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

我得到的错误如下:

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

粗略翻译:

范围大小坐标无效(line29,file "dealpull5.0")

感谢这篇文章! 我重新编写了您的代码,以满足我直接返回匹配交易数组的一些需要。

我还将 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;
}

您已从 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 |      |       |             |

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

注意事项:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM