我正在尝试优化此代码以使其尽可能短我使用它来调用 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)
我想我做错了什么,但看不到。谢谢