Skip to content

Instantly share code, notes, and snippets.

@gustavoreis
Created July 29, 2019 13:59
Show Gist options
  • Save gustavoreis/4cb1f24ad3f9de8f8b867c2c02c7ae9a to your computer and use it in GitHub Desktop.
Save gustavoreis/4cb1f24ad3f9de8f8b867c2c02c7ae9a to your computer and use it in GitHub Desktop.
Planilha de Tags
/** * Utiliza os dados contidos na planilha com nome 'Config' para listar os produtos. * * O que a planilha 'Config' deve conter (todos na coluna B): * Célula B1: Host; * Célula B2: Api User; * Célula B3: Api key; * * Retorna as colunas com as informações do pedido nessa ordem: Referência, Nome, Tags **/ function listProducts() { var spreadsheet = SpreadsheetApp.getActive(); var sheet = spreadsheet.getSheetByName('Configs'); var host = sheet.getRange(1, 2).getValue(); var username = sheet.getRange(2, 2).getValue(); var password = sheet.getRange(3, 2).getValue(); var page = 1; var data = []; do { var resources = getProducts(host, username, password, page); page++; insertInfo(data, resources); } while (resources.length > 0); return data; }
function getProducts(host, username, password, page) { var baseUrl = 'https://' + host + '/api/v2/products'; var query = '?page=' + page; var url = baseUrl + query; return callApi(url, username, password); }
function insertInfo(data, resources) { for (var index in resources) { var resource = resources[index]; data.push([ resource.reference, resource.name, resource.tag_names.toString() ]); } }
function callApi(url, username, password) { var auth_token = Utilities.base64Encode(username + ':' + password); var response = UrlFetchApp.fetch( url, { headers: { 'Authorization': 'Basic ' + auth_token }, muteHttpExceptions: true } ); return JSON.parse(response); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment