英文:
api calls with user id's in a range
问题
我有这段代码,它进行了API调用并将其写入Google表格。我想要有一个范围,可以包含多个ID,并依次运行这些调用,这样我就不必一个个地进行调用并手动更改ID。
这里有一个包含范围和输出表的电子表格链接。
我迄今为止使用的代码如下:
function callNumbers() {
// 包括API密钥
const API_KEY = 'xyz';
// 设置终端点
const url = 'https://www.example.com/wp-json/armember/v1/member_details?arm_api_key=xyz&user_id=19&metakeys=country,checkbox_vgc5x';
// 调用API
const response = UrlFetchApp.fetch(url + '&api_key=' + API_KEY);
Logger.log(response.getContentText());
var fact = response.getContentText();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(sheet.getLastRow() + 1,1).setValue([fact]);
}
英文:
I have this code that does an api call and writes it into a google sheet. I would like to have a range where I can have several id's and run the calls after each other so I don't have to do the calls one by one and change the id manually.
There is a spreadsheet here which has the range and an output sheet.
And the code I have been using so far is this
function callNumbers() {
// include the API Key
const API_KEY = 'xyz';
// set the endpoint
const url = 'https://www.example.com/wp-json/armember/v1/member_details?arm_api_key=xyz&user_id=19&metakeys=country,checkbox_vgc5x';
// call the API
const response = UrlFetchApp.fetch(url + '&api_key=' + API_KEY);
Logger.log(response.getContentText());
var fact = response.getContentText();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(sheet.getLastRow() + 1,1).setValue([fact]);
}
答案1
得分: 1
修改后的脚本 1:
在这个修改中,所有请求都使用 fetchAll
方法运行。
function callNumbers1() {
const API_KEY = 'xyz';
const srcSheetName = "Id范围";
const dstSheetName = "用户数据输出";
const ss = SpreadsheetApp.getActiveSpreadsheet();
const srcSheet = ss.getSheetByName(srcSheetName);
const ids = srcSheet.getRange("A1:A" + srcSheet.getLastRow()).getDisplayValues();
const reqs = ids.map(([id]) => ({ url: `https://###/wp-json/armember/v1/member_details?arm_api_key=xyz&user_id=${id}19&metakeys=country,checkbox_vgc5x&api_key=${API_KEY}`, muteHttpExceptions: true }));
const res = UrlFetchApp.fetchAll(reqs);
const dstValues = res.map(r => [r.getResponseCode() == 200 ? r.getContentText() : null]);
const dstSheet = ss.getSheetByName(dstSheetName);
dstSheet.getRange(dstSheet.getLastRow() + 1, 1, dstValues.length).setValues(dstValues);
}
- 运行此脚本时,从源表格的 "A" 列检索到 ID。然后,检索到的值将放入目标表格的 "A" 列中。
修改后的脚本 2:
在这个修改中,每个请求都在循环中运行。
function callNumbers2() {
const API_KEY = 'xyz';
const srcSheetName = "Id范围";
const dstSheetName = "用户数据输出";
const ss = SpreadsheetApp.getActiveSpreadsheet();
const srcSheet = ss.getSheetByName(srcSheetName);
const ids = srcSheet.getRange("A1:A" + srcSheet.getLastRow()).getDisplayValues();
const dstValues = ids.map(([id]) => {
const url = `https://###/wp-json/armember/v1/member_details?arm_api_key=xyz&user_id=${id}19&metakeys=country,checkbox_vgc5x&api_key=${API_KEY}`;
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
return [res.getResponseCode() == 200 ? res.getContentText() : null];
});
const dstSheet = ss.getSheetByName(dstSheetName);
dstSheet.getRange(dstSheet.getLastRow() + 1, 1, dstValues.length).setValues(dstValues);
}
注意:
- 在此示例中,假定您的请求端点的脚本有效。请注意这一点。
参考资料:
英文:
Although I'm not sure whether I could correctly understand your expected result when your provided Spreadsheet is used, how about the following modified scripts?
Modified script 1:
In this modification, all requests are run with the fetchAll method.
function callNumbers1() {
const API_KEY = 'xyz';
const srcSheetName = "Range of Id's";
const dstSheetName = "user data output";
const ss = SpreadsheetApp.getActiveSpreadsheet();
const srcSheet = ss.getSheetByName(srcSheetName);
const ids = srcSheet.getRange("A1:A" + srcSheet.getLastRow()).getDisplayValues();
const reqs = ids.map(([id]) => ({ url: `https://###/wp-json/armember/v1/member_details?arm_api_key=xyz&user_id=${id}19&metakeys=country,checkbox_vgc5x&api_key=${API_KEY}`, muteHttpExceptions: true }));
const res = UrlFetchApp.fetchAll(reqs);
const dstValues = res.map(r => [r.getResponseCode() == 200 ? r.getContentText() : null]);
const dstSheet = ss.getSheetByName(dstSheetName);
dstSheet.getRange(dstSheet.getLastRow() + 1, 1, dstValues.length).setValues(dstValues);
}
- When this script is run, the IDs are retrieved from column "A" of the source sheet. And, the retrieved values are put into column "A" of the destination sheet.
Modified script 2:
In this modification, each request is run in a loop.
function callNumbers2() {
const API_KEY = 'xyz';
const srcSheetName = "Range of Id's";
const dstSheetName = "user data output";
const ss = SpreadsheetApp.getActiveSpreadsheet();
const srcSheet = ss.getSheetByName(srcSheetName);
const ids = srcSheet.getRange("A1:A" + srcSheet.getLastRow()).getDisplayValues();
const dstValues = ids.map(([id]) => {
const url = `https://###/wp-json/armember/v1/member_details?arm_api_key=xyz&user_id=${id}19&metakeys=country,checkbox_vgc5x&api_key=${API_KEY}`;
const res = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
return [res.getResponseCode() == 200 ? res.getContentText() : null];
});
const dstSheet = ss.getSheetByName(dstSheetName);
dstSheet.getRange(dstSheet.getLastRow() + 1, 1, dstValues.length).setValues(dstValues);
}
Note:
- In this sample, it supposes that your showing script for requesting the endpoint works. Please be careful about this.
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论