【发布时间】:2020-07-08 08:03:47
【问题描述】:
大家早上好
我来看你是因为在努力使我的谷歌脚本工作之后,在执行页面上我看到我的脚本工作,但是在我的谷歌工作表上我有一个错误:“超过最大执行时间”。我在互联网上看到自定义 google 脚本函数只留下 30 秒的执行时间,我不知道该怎么做?添加代码来调整此功能?我承认我不明白自定义函数和谷歌应用脚本之间的区别,但我知道脚本有 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;
}
}
提前谢谢大家。
编辑:--------------FOR EACH LOOP--------- -------------------
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;
}
-------------------编辑编号2------- --------------------------
直到现在我执行=getPipedriveDeals()它返回False(就像我在评论中所说的那样)以及当我尝试删除additional_data.pagination.more_items_in_collection(因为它对我来说不是很有用)我不知道为什么但是脚本不再起作用了,我在选择要返回的数据时遇到了一些困难。
【问题讨论】:
-
如果在循环中使用
fetch是您问题的主要原因,那么使用fetchAll怎么样? Ref 但我不确定你的实际情况。例如,在您的脚本中,ss、sheets和sheet的值是多少?这些不是用的吗? -
您好@Tanaike,感谢您的快速回复!值“ss, sheet, sheet”用于选择谷歌表,我还注意到在我的第二个函数中它们可能不是必需的,它为我节省了大约 8 秒的时间来删除它们。我认为您对 fetch 的看法是正确的,只是我真的不知道如何在当前状态下实现 FetchAll ...根据文档,它必须附加到一个或多个请求,而我不知道真的不知道如何修改代码来做到这一点而不破坏一切我花了很长时间编写它并且我得到了一点帮助......再次感谢
标签: function loops google-apps-script optimization google-sheets