0

我正在尝试优化此代码以使其尽可能短我使用它来调用 API 以获取 Google 表格中的数据。有人告诉我,正是 fetch 使脚本如此之长,我可以尝试使用 afetchAll但它破坏了我的代码,我觉得将我的 url 放在一个数组中会破坏我的代码(对于fetchAll)。if我也对我提出的声明表示怀疑,以防数据是null(已经使我的函数崩溃)。

    // Standard functions to call the spreadsheet sheet and activesheet
function GetPipedriveDeals2() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheets = ss.getSheets();
  let 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.
  let url    = "https://laptop.pipedrive.com/v1/products:(id)?start=";
  let limit  = "&limit=500";
  //let filter = "&filter_id=64";
  let pipeline = 1; // put a pipeline id specific to your PipeDrive setup 
  let start  = 1;
  //let end  = start+50;
  let token  = "&api_token=XXXXXXXXXXXXXXXXXXXXXXXX"
  

  let response = UrlFetchApp.fetch(url+start+limit+token); //
  let dataAll = JSON.parse(response.getContentText()); 
  let dataSet = dataAll;
  //let prices = prices;
  //create array where the data should be put
  let rows = [], data;
  for (let i = 0; i < dataSet.data.length; i++) {
  data = dataSet.data[i];
    rows.push([data.id,
               GetPipedriveDeals4(data.id)
               ]);
  }

  Logger.log( 'function2' ,JSON.stringify(rows,null,8) );   // Log transformed data

  return rows;
}

// Standard functions to call the spreadsheet sheet and activesheet
function GetPipedriveDeals4(idNew) {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheets = ss.getSheets();
  let 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.
  let url    = "https://laptop.pipedrive.com/v1/products/"+idNew+"/deals:(id,d93b458adf4bf84fefb6dbce477fe77cdf9de675)?start=";
  let limit  = "&limit=500";
  //let filter = "&filter_id=64";
  let pipeline = 1; // put a pipeline id specific to your PipeDrive setup 
  let start  = 1;
  //let end  = start+50;
  let token  = "&api_token=XXXXXXXXXXXXXXXXXXXXXX"
  

  let response = UrlFetchApp.fetch(url+start+limit+token); //
  let dataAll = JSON.parse(response.getContentText()); 
  let dataSet = dataAll;
   //Logger.log(dataSet)
  //let prices = prices;
  //create array where the data should be put
  let rows = [], data;
  if(dataSet.data === null )return
  else {
    for (let i = 0; i < dataSet.data.length; i++) {
      data = dataSet.data[i];
      let idNew = data.id; 
      rows.push([data.id, data['d93b458adf4bf84fefb6dbce477fe77cdf9de675']]);
    }
  
  Logger.log( 'function4', JSON.stringify(rows,null,2) );   // Log transformed data
  return rows;
  }
}

尝试fetchAll

// Standard functions to call the spreadsheet sheet and activesheet
function GetPipedriveDeals2() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheets = ss.getSheets();
  let 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.
  let limit  = "&limit=500";
  //let filter = "&filter_id=64";
  let pipeline = 1; // put a pipeline id specific to your PipeDrive setup 
  let start  = 1;
  //let end  = start+50;
  let token  = "&api_token=XXXXXXXXXXXXXXXXXXXXXX"
  let url    = "https://laptop.pipedrive.com/v1/products:(id)?start="+start+limit+token;
  let request = [url];
  let response = UrlFetchApp.fetchAll(request); //
  let dataAll = response.map(function(e) {return e.getContentText()});
  let dataSet = dataAll;
  //let prices = prices;
  //create array where the data should be put
  let rows = [], data;
  for (let i = 0; i < dataSet.data.length; i++) {
  data = dataSet.data[i];
    rows.push([data.id,
               GetPipedriveDeals4(data.id)
               ]);
  }

  Logger.log( 'function2' ,JSON.stringify(rows,null,8) );   // Log transformed data

  return rows;
}

function GetPipedriveDeals4(idNew) {
  let start  = 1;
  let limit  = "&limit=500";
  let token  = "&api_token=XXXXXXXXXXXXXXXXXXXX"
  let urli    = "https://laptop.pipedrive.com/v1/products/"+idNew+"/deals:(id,d93b458adf4bf84fefb6dbce477fe77cdf9de675)?start="+start+limit+token;
  let request1 = [urli]
  let response1 = UrlFetchApp.fetchAll(request1); //
  var dataAll1 = response1.map(function(e) {return e.getContentText()});
  let dataSet1 = dataAll1;
   //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.
  let urli    = "https://laptop.pipedrive.com/v1/products/"+idNew+"/deals:(id,d93b458adf4bf84fefb6dbce477fe77cdf9de675)?start="+start+limit+token;
  let request1 = [urli]
  let response1 = UrlFetchApp.fetchAll(request1); //
  var dataAll1 = response1.map(function(e) {return e.getContentText()});
  let dataSet1 = dataAll1;
   //Logger.log(dataSet1)
  //let prices = prices;
  //create array where the data should be put
  let rows1 = [], data1;
  if(dataSet1.data1 === null )return
  else {
    for (let i = 0; i < dataSet1.data1.length; i++) {
      data1 = dataSet1.data1[i];
      let idNew = data1.id; 
      rows1.push([data1.id, data1['d93b458adf4bf84fefb6dbce477fe77cdf9de675']]);
    }
  
  Logger.log( 'function4', JSON.stringify(rows1,null,2) );   // Log transformed data
  return rows1;
  }
}

所以我在文档中看到我必须将我的 URL 放在 Tab [] 中才能发出请求,但我知道我有以下错误:

8 juil. 2020 à 16:06:18 Erreur  TypeError: Cannot read property 'length' of undefined
    at GetPipedriveDeals2(Copie de importNamesTypes:22:36)

我想我做错了什么,但看不到。谢谢

4

1 回答 1

0

所以是的,我没有把修改放到网上,但事实上我设法做我想做的,所以我会放上我的代码和一些解释。

首先,我没有设法执行上面的脚本,或者我放在网上的任何其他脚本,原因有两个:

- 1:我写的第一个对于google sheet来说太长了,执行时间在50s以上(最多30s)

- 2:我制作的第二个脚本对于 API pipedrive 来说太快了,当我设法解决这个问题时,我收到一条错误消息“无法读取 null 的属性”,这是因为 pipedrive 在返回“null” string 和 null 作为空值,所以这破坏了代码。当我设法解决这个问题时,脚本又太长了。

所以我再次修改了脚本,并在参数中设置了开始和限制。所以现在我通过这样做等等来调用我的=getPipeDriveDeals(0, 50)函数=getPipeDriveDeals(51, 90)......

所以这是我写的代码:


function getPipedriveDeals(start , limit) {
  var allResponsesDeals = [];
  
  options = {muteHttpExceptions: true}; 
  var idsListRequest = "https://xxxx.pipedrive.com/v1/products:(id)?start=";
  var token = "&api_token=hiddenforobviousreasons";
  var response = UrlFetchApp.fetch(idsListRequest + start +"&limit="+limit + token, options);
  let dataAll = JSON.parse(response.getContentText()).data;
  
  var requests = [];
  let rows = [], data;
  //Logger.log("data="+ JSON.stringify(dataAll, null, 2));
  
 
  if(dataAll === null) {
    //Logger.log(" dataAll issss nnnnuulll" );
  }else{ 
    dataAll.forEach(function(product) {
      var productDetailUrl = "https://xxxx.pipedrive.com/v1/products/" + product.id + "/deals:(title,7d321c7454a4b44a09f32bdd7702a2b17fd7654e)?start=0";
      requests = productDetailUrl + token
      var responses = UrlFetchApp.fetch(requests);
      var dataArray = JSON.parse(responses.getContentText());
      
      
      if(dataArray.data === null) {
        //Logger.log(" newData issss nnnnuulll" );
      }else 
      {
        for (let i = 0; i < dataArray.data.length; i++) {
          data = dataArray.data[i];
          rows.push([data.title, data['7d321c7454a4b44a09f32bdd7702a2b17fd7654e']])
        }
      }
    });
  }
  
  Logger.log("allResponsesDeals ="+ JSON.stringify(rows, null, 2));
  return rows;
  
}

function getAllDeals(){
  var allResponses = [];
    var deals = getPipedriveDeals();
    Logger.log("deals="+ JSON.stringify(deals, null, 2));
    
    deals.forEach((response)=>{allResponses.push(response)});
  
  allResponses.push(deals)
  
  Logger.log("allResponses="+ allResponses);
  return allResponses;
}

如果您想将特定数据添加到您的工作表中,只需在rows.push您第二次调用的 URL 中更改它即可。

我建议您不要在第一次请求时修改要获取的数据(该 ID 用于发出所有其他请求)。

我希望这将帮助您和所有想要调整 Pipedrive 功能的人。

于 2020-07-15T11:29:06.917 回答