I want to call one API over each row of google sheet. API will return the JSON object which I want to push in the array after all rows completed. I want to this array for further process.
for e.g resultArr = [ {first Api result}, {second api result} .... so on]
below one is my code which I have a try
const axios = require("axios");
const assert = require("assert");
const path = require("path");
const fs = require("fs");
const { GoogleSpreadsheet } = require("google-spreadsheet");
const doc = new GoogleSpreadsheet("ABCDEFGHIJKLMOPQRSTUVWxyz");
doc.useApiKey("ABCDEFGHIJKLMOPQRSTUVWxyz");
let jsonRowArr = ["initial"];
async function test1() {
await doc.loadInfo();
const sheet = doc.sheetsByIndex[0]; /* or use doc.sheetsById[id] */
/* read all rows */
const rows = await sheet.getRows(); // can pass in { limit, offset }
rows.forEach(async (row, index) => {
let heading = row["_sheet"]["headerValues"];
fieldValue = {};
let rowValue = row["_rawData"];
heading.forEach((key, i) => {
fieldValue[key] = rowValue[i];
});
//start here
let APIParam = {
test_name: `test ${index}`,
shipping_method: {
before_decimal_point: fieldValue["Before Decimal"],
after_decimal_point: fieldValue["After Decimal"],
closest_round: "100",
},
shipping_price: fieldValue["3 Digit"],
method_name: "get_shipping_after_round",
expected_rate_up: ["Expected Down 3"],
expected_rate_down: ["Expected Up"],
};
APIAfterApiData = await apiCallNewFn(APIParam);
await jsonRowArr.push("dsv");
await jsonRowArr.push(APIAfterApiData);
});
return await jsonRowArr;
}
apiCallNewFn = async (params) => {
let { data } = await axios.post(
"http://api.example.com/call_function.php",
null,
{
params,
}
);
// console.log('data...', data);
data["test_name"] = await params.test_name;
data["expected_rate_up"] = await params.expected_rate_up;
data["expected_rate_down"] = await params.expected_rate_down;
return await data;
};
(async () => {
let customTestObj = await test1();
console.log("I want fianl result here");
console.log(customTestObj);
console.log("for further process");
})();