最近20天从Pipedrive导入到Google工作表的脚本

时间:2017-07-25 10:33:44

标签: google-apps-script google-sheets pipedrive-api

我有这个脚本用于从Pipedrive导入Go​​ogle表格,效果很好。我希望它只从最近20天而不是特定日期拉出比赛。我还希望每次清除行并将它们分成新鲜,而不是将它们添加到下一个空行。

function GetPipedriveDeals() {
  var ss = SpreadsheetApp.openById('sheet name');
  var sheet = ss.getSheetByName("Sheet1");

  var url    = "https://api.pipedrive.com/v1/activities?user_id=0&start=0&limit=500&start_date=2017-06-01&api_token=xxxxxxxxxxxxxxxxxxxx";

  var response = UrlFetchApp.fetch(url); 
  var dataSet = JSON.parse(response.getContentText()); 
  var data;

  for (var i = 0; i < dataSet.data.length; i++) {
    data = dataSet.data[i];
    sheet.appendRow([data.user_id, data.type, data.add_time, data.note, data.org_name]);
  }
}

非常感谢任何帮助,谢谢你。

1 个答案:

答案 0 :(得分:0)

你可以使用start_date&amp; end_date参数用于获取最近20天的记录,而sheet.clear()用于清除工作表的内容。请参阅以下代码。希望这有帮助!

   function GetPipedriveDeals() {
      var ss = SpreadsheetApp.openById('sheet name');
      var sheet = ss.getSheetByName("Sheet1");

      sheet.clear(); //clear the contents of the sheet

      var startDate = getStartDate();
      var endDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
      var url    = "https://api.pipedrive.com/v1/activities?user_id=0&start=0&start_date="+startDate+"&end_date="+endDate+"api_token=xxxxxxxxxxxxxxxxxxxx";
      var response = UrlFetchApp.fetch(url); 
      var dataSet = JSON.parse(response.getContentText()); 
      var data;

      for (var i = 0; i < dataSet.data.length; i++) {
        data = dataSet.data[i];
        sheet.appendRow([data.user_id, data.type, data.add_time, data.note, data.org_name]);
      }
    }


    function getStartDate() {
      var result = new Date();
      result.setDate(result.getDate() - 20);
      return result.convertDate();
    }

    Date.prototype.convertDate = function() {
      var mm = this.getMonth() + 1; 
      var dd = this.getDate();

      return [this.getFullYear(),
         (mm>9 ? '' : '0') + mm,
         (dd>9 ? '' : '0') + dd
      ].join('-');
    };