Sellers often work with detailed sales reports. Manually collecting data increases the risk of errors and slows down the analysis process, which can negatively impact decision-making speed.
Use the WB API to automatically export data to Google Sheets, reducing the risk of errors and saving time.
Set up regular delivery of an up-to-date and accurate sales report in the form of a Google Sheet.
In this article, we will explain how to set up this integration.
YOUR_API_TOKEN
with your own token.var baseUrl
parameter, set the report date range, for example, dateFrom=2024-01-29&dateTo=2025-01-01
.Note: Data is available starting from January 29, 2024.
function getDataFromAPI() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var token = 'ВАШ_API_ТОКЕН';
var limit = 100000;
var baseUrl = 'https://statistics-api.wildberries.ru/api/v5/supplier/reportDetailByPeriod?dateFrom=2024-01-29&dateTo=2025-01-01&limit=' + limit;
var lastRow = sheet.getLastRow();
var headers = [];
var rrdid = 0;
if (lastRow > 1) {
headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var rrdidIndex = headers.indexOf('rrd_id');
if (rrdidIndex !== -1) {
rrdid = sheet.getRange(lastRow, rrdidIndex + 1).getValue();
} else {
throw new Error("Колонка 'rrd_id' не найдена в заголовках.");
}
} else if (lastRow === 1) {
headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
}
var data, response, url;
var allData = [];
var headersSet = (headers.length > 0);
while (true) {
url = baseUrl + '&rrdid=' + rrdid;
var encodedUrl = encodeURI(url);
var options = {
'method': 'get',
'headers': {
'Authorization': token
},
'muteHttpExceptions': true
};
try {
response = UrlFetchApp.fetch(encodedUrl, options);
if (response.getResponseCode() !== 200) {
throw new Error('Ошибка API: ' + response.getContentText());
}
data = JSON.parse(response.getContentText());
if (data === null || data.length === 0) {
Logger.log('Нет больше данных для выгрузки.');
break;
}
if (!headersSet) {
headers = Object.keys(data[0]);
sheet.appendRow(headers);
headersSet = true;
}
data.forEach(function(row) {
var values = headers.map(function(header) {
return row[header];
});
allData.push(values);
});
rrdid = data[data.length - 1].rrd_id;
Logger.log('Следующий rrdid: ' + rrdid);
if (data.length < limit) {
Logger.log('Выгружены все доступные данные.');
break;
}
} catch (error) {
Logger.log('Ошибка при запросе данных: ' + error.message);
break;
}
}
if (allData.length > 0) {
sheet.getRange(sheet.getLastRow() + 1, 1, allData.length, allData[0].length).setValues(allData);
}
}
getDataFromAPI
Automatic data collection provides sellers with a significant advantage and is becoming increasingly indispensable for successful business management.