Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 超过最大执行时间,谷歌表单,如何改进呢?_Function_Loops_Google Apps Script_Optimization_Google Sheets - Fatal编程技术网

Function 超过最大执行时间,谷歌表单,如何改进呢?

Function 超过最大执行时间,谷歌表单,如何改进呢?,function,loops,google-apps-script,optimization,google-sheets,Function,Loops,Google Apps Script,Optimization,Google Sheets,各位早上好, 我来看你是因为在努力使我的谷歌脚本工作之后,在执行页面上我看到我的脚本工作,但是在我的谷歌工作表上我有一个错误:“超过了最大执行时间”。我在互联网上看到,定制的谷歌脚本函数只剩下30秒的执行时间,我不知道该怎么做?是否添加代码来调整此功能?我承认我不理解自定义函数和google应用程序脚本之间的区别,但我知道脚本的执行时间为6分钟。。。以下是我的代码摘录: // Standard functions to call the spreadsheet sheet and actives

各位早上好,

我来看你是因为在努力使我的谷歌脚本工作之后,在执行页面上我看到我的脚本工作,但是在我的谷歌工作表上我有一个错误:“超过了最大执行时间”。我在互联网上看到,定制的谷歌脚本函数只剩下30秒的执行时间,我不知道该怎么做?是否添加代码来调整此功能?我承认我不理解自定义函数和google应用程序脚本之间的区别,但我知道脚本的执行时间为6分钟。。。以下是我的代码摘录:

// 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;
  }
}
//调用电子表格和activesheet的标准函数
函数GetPipedriveDeals2(){
设ss=SpreadsheetApp.getActiveSpreadsheet();
let sheets=ss.getSheets();
let sheet=ss.getActiveSheet();
//下一步构建url的方式是在端点之间进行迭代,因为api只允许固定数量的调用(100),这样我就可以慢慢填充工作表。
让url=”https://laptop.pipedrive.com/v1/products:(id)?start=“;
let limit=“&limit=500”;
//let filter=“&filter\u id=64”;
let pipeline=1;//放置特定于PipeDrive设置的管道id
让start=1;
//让结束=开始+50;
let token=“&api_token=XXXXXXXXXXXXXXXXXXXXXXXX”
let response=UrlFetchApp.fetch(url+start+limit+token)//
让dataAll=JSON.parse(response.getContentText());
让dataSet=dataAll;
//让价格=价格;
//创建应将数据放在其中的数组
让行=[],数据;
for(设i=0;i
提前谢谢大家

编辑:------------------------------------对于每个循环----------------------------


function getPipedriveDeals(start = 0,apiRequestLimit = 39) {
  console.log("start="+start);
  //Make the initial request to get the ids you need for the details.
  var idsListRequest = "https://laptop.pipedrive.com/v1/products:(id)?start=";
  var limit = "&limit=" + apiRequestLimit;
  var token = "&api_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  var response = UrlFetchApp.fetch(idsListRequest + start + limit + token);
  var data = JSON.parse(response.getContentText()).data;
  
  //For every id in the response, construct a url (the detail url) and push to a list of requests
  var requests = [];
  console.log("data="+data);
  data.forEach(function(product) {
    var productDetailUrl = "https://laptop.pipedrive.com/v1/products/" + product.id + "/deals:(id,d93b458adf4bf84fefb6dbce477fe77cdf9de675)?start=";
    requests.push(productDetailUrl + start + limit + token)
  });
  
  //With the list of detail request urls, make one call to UrlFetchApp.fetchAll(requests)
  var responses = UrlFetchApp.fetchAll(requests);
  return [responses,JSON.parse(responses[0].getContentText()).additional_data.pagination.more_items_in_collection];
  
}

function getAllDeals(){
  var allResponses = [];
  for(var i = 0; i<500; ){ 
    var deals = getPipedriveDeals(start=i);
    deals[0].forEach((response)=>{allResponses.push(response)});
  if(deals[1]){
    // If there are more items sleep for 1000 milliseconds
    Utilities.sleep(1000);
    i+=39;
  }
  else{
    console.log("No more items in collection.");
    break;
  }
}
console.log("allResponses="+allResponses);
return allResponses;
}


函数getPipedriveDeals(start=0,apirestLimit=39){
console.log(“start=“+start”);
//发出初始请求以获取详细信息所需的ID。
var idsListRequest=”https://laptop.pipedrive.com/v1/products:(id)?start=“;
var limit=“&limit=“+apirestlimit;
var token=“&api_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
var response=UrlFetchApp.fetch(idsListRequest+start+limit+token);
var data=JSON.parse(response.getContentText()).data;
//对于响应中的每个id,构造一个url(详细url)并推送到请求列表
var请求=[];
console.log(“data=“+data”);
data.forEach(功能(产品){
var productDetailUrl=”https://laptop.pipedrive.com/v1/products/“+product.id+”/deals:(id,d93b458adf4bf84fefb6dbce477fe77cdf9de675)?start=“;
请求.推送(productDetailUrl+start+limit+token)
});
//使用详细请求URL的列表,调用一次UrlFetchApp.fetchAll(请求)
var responses=UrlFetchApp.fetchAll(请求);
返回[responses,JSON.parse(responses[0].getContentText())。其他\u数据。分页。更多\u项\u在\u集合中];
}
函数getAllDeals(){
var-allResponses=[];
for(var i=0;i{allResponses.push(response)});
如果(交易[1]){
//如果有更多项目睡眠1000毫秒
睡眠(1000);
i+=39;
}
否则{
log(“集合中不再有项目”);
打破
}
}
console.log(“allResponses=“+allResponses”);
返回所有响应;
}
关于日志中的奇怪行为:

-------------------------------编辑编号2----------------------------------


function getPipedriveDeals(start = 0,apiRequestLimit = 39) {
  console.log("start="+start);
  //Make the initial request to get the ids you need for the details.
  var idsListRequest = "https://laptop.pipedrive.com/v1/products:(id)?start=";
  var limit = "&limit=" + apiRequestLimit;
  var token = "&api_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  var response = UrlFetchApp.fetch(idsListRequest + start + limit + token);
  var data = JSON.parse(response.getContentText()).data;
  
  //For every id in the response, construct a url (the detail url) and push to a list of requests
  var requests = [];
  console.log("data="+data);
  data.forEach(function(product) {
    var productDetailUrl = "https://laptop.pipedrive.com/v1/products/" + product.id + "/deals:(id,d93b458adf4bf84fefb6dbce477fe77cdf9de675)?start=";
    requests.push(productDetailUrl + start + limit + token)
  });
  
  //With the list of detail request urls, make one call to UrlFetchApp.fetchAll(requests)
  var responses = UrlFetchApp.fetchAll(requests);
  return [responses,JSON.parse(responses[0].getContentText()).additional_data.pagination.more_items_in_collection];
  
}

function getAllDeals(){
  var allResponses = [];
  for(var i = 0; i<500; ){ 
    var deals = getPipedriveDeals(start=i);
    deals[0].forEach((response)=>{allResponses.push(response)});
  if(deals[1]){
    // If there are more items sleep for 1000 milliseconds
    Utilities.sleep(1000);
    i+=39;
  }
  else{
    console.log("No more items in collection.");
    break;
  }
}
console.log("allResponses="+allResponses);
return allResponses;
}

直到现在,当我执行
=getPipedriveDeals()
时,它返回
False
(就像我在评论中说的那样),以及当我尝试删除
附加数据时。分页。集合中的更多项目(因为它对我来说不是很有用)我不知道为什么,但脚本不再工作,我在选择要返回的数据时遇到了一些困难


您可以使用来保存配额,并且可能会节省大量脚本执行时间

我已经删除了您代码中的一些冗余内容,以使下面的示例演示如何在您的案例中使用
fetchAll

例子: 函数getPipedriveDeals(start=0,apirestLimit=50){ console.log(“start=“+start”); //发出初始请求以获取详细信息所需的ID。 var idsListRequest=”https://laptop.pipedrive.com/v1/products:(id)?start=“; var limit=“&limit=“+apirestlimit; var token=“&api_token=xxxxxxxxxx”; var响应=UrlFet