Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Пример работы с подсказками DaData в Google Taблицах
// Замените на свой API-ключ из личного кабинета (https://dadata.ru/profile/#info)
var API_KEY = "ВАШ_API_КЛЮЧ";
/***
/* Не меняйте код ниже этой строчки
***/
var FORMATTERS = {
address: formatAddress,
fio: formatFio,
party: formatParty,
bank: formatBank
}
function formatAddress(suggestion) {
var postalCode = suggestion.data.postal_code;
var address = suggestion.value;
if (postalCode) {
address = postalCode + ", " + address;
}
return [
["Адрес", address]
];
}
function formatGender(gender) {
genders = {
MALE: "мужской",
FEMALE: "женский",
UNKNOWN: "неизвестный"
}
return genders[gender];
}
function formatFio(suggestion) {
var fio = suggestion.data;
return [
["Фамилия", fio.surname],
["Имя", fio.name],
["Отчество", fio.patronymic],
["Пол", formatGender(fio.gender)]
];
}
function formatParty(suggestion) {
var party = suggestion.data;
return [
["Название", party.name.short_with_opf],
["ОГРН", party.ogrn],
["ИНН", party.inn],
["КПП", party.kpp],
["ОКВЭД", party.okved],
["Дата регистрации", party.state.registration_date],
["Адрес", party.address.value]
];
}
function formatBank(suggestion) {
var bank = suggestion.data;
return [
["Название", bank.name.payment],
["БИК", bank.bic],
["SWIFT", bank.swift],
["Адрес", bank.address.value]
];
}
function guessAddress(query) {
return guess("address", query);
}
function guessFio(query) {
return guess("fio", query);
}
function guessParty(query) {
return guess("party", query);
}
function guessBank(query) {
return guess("bank", query);
}
function guess(type, query) {
if (API_KEY === "ВАШ_API_КЛЮЧ") {
return "Укажите ваш API-ключ в переменной API_KEY";
}
var response = suggest(type, query);
if (response.suggestions.length > 0) {
return FORMATTERS[type](response.suggestions[0]);
} else {
return "Ничего не найдено";
}
}
function suggest(type, query) {
var url = "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/" + type;
var payload = '{"query": "' + query + '"}'
var headers = {
"Authorization": "Token " + API_KEY
};
var fetchArgs = {
method: "POST",
contentType: "application/json",
payload: payload,
headers: headers,
muteHttpExceptions: false
};
return JSON.parse(UrlFetchApp.fetch(url, fetchArgs));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment