'I'm using the Post function in google script to fill data in the spreadsheet through an ERP callback, but I'm having problems
I created a script to fill in data of orders in the spreadsheet received via callback by a JSON file from my ERP to the URL of the google script of the spreadsheet (when the order changes status, the ERP sends the callback) . But it's not working, could someone help me to check where the error is? I'll leave my script below and an example of the JSON file that will be received
my script => => =>
//this is a function that fires when the webapp receives a GET request
function doGet(e) {
return HtmlService.createHtmlOutput("request received");
}
//this is a function that fires when the webapp receives a POST request
function doPost(e) {
var params = JSON.stringify(e.postData.contents);
params = JSON.parse(params);
var myData = JSON.parse(e.postData.contents);
var data = myData["data"]["pedido"]["data"];
var numero = myData["data"]["pedido"]["numero"];
var numeroPedidoLoja = myData["data"]["pedido"]["numeroPedidoLoja"];
var situacao = myData["data"]["pedido"]["situacao"];
var quantidade = myData["data"]["pedido"]["itens"]["item"]["quantidade"];
var codigo = myData["data"]["pedido"]["itens"]["item"]["codigo"];
var descricao = myData["data"]["pedido"]["itens"]["item"]["descricao"];
var totalvenda = myData["data"]["pedido"]["totalvenda"];
var loja = myData["data"]["pedido"]["loja"];
var tipoIntegracao = myData["data"]["pedido"]["tipoIntegracao"];
var uf = myData["data"]["pedido"]["cliente"]["uf"];
var precocusto = myData["data"]["pedido"]["itens"]["item"]["precocusto"];
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = Math.max(sheet.getLastRow(),1);
sheet.insertRowAfter(lastRow);
sheet.getRange(lastRow + 1, 1).setValue(data);
sheet.getRange(lastRow + 1, 2).setValue(numero);
sheet.getRange(lastRow + 1, 3).setValue(numeroPedidoLoja);
sheet.getRange(lastRow + 1, 4).setValue(situacao);
sheet.getRange(lastRow + 1, 5).setValue(quantidade);
sheet.getRange(lastRow + 1, 6).setValue(codigo);
sheet.getRange(lastRow + 1, 7).setValue(descricao);
sheet.getRange(lastRow + 1, 8).setValue(totalvenda);
sheet.getRange(lastRow + 1, 9).setValue(loja);
sheet.getRange(lastRow + 1, 10).setValue(tipoIntegracao);
sheet.getRange(lastRow + 1, 11).setValue(uf);
sheet.getRange(lastRow + 1, 12).setValue(precocusto);
sheet.getRange(lastRow + 1, 13).setValue(params);
SpreadsheetApp.flush();
return HtmlService.createHtmlOutput("post request received");
}
`
ex JSON => => =>
{
"data":{
"pedidos":[
{
"pedido":{
"desconto":"15,00",
"observacoes":"Testando o campo observações do pedido",
"observacaointerna":"Testando o campo observações internas do pedido",
"data":"2017-07-28",
"numero":"1",
"numeroPedidoLoja":"100000001",
"vendedor":"João da Silva",
"valorfrete":"22.50",
"totalprodutos":"139.98",
"totalvenda":"147.48",
"situacao":"Em Aberto",
"loja": "123456789",
"dataPrevista":"2017-07-28",
"tipoIntegracao":"Magento",
"cliente":{
"nome":"Organisys Software",
"cnpj":"00.000.000/0000-1",
"ie":"0000000000",
"rg":"162788484",
"endereco":"Rua Visconde de São Gabriel",
"numero":"000",
"complemento":"Sala 000",
"cidade":"Bento Gonçalves",
"bairro":"Cidade Alta",
"cep":"95.700-000",
"uf":"RS",
"email":"[email protected]",
"celular":"",
"fone":"(54) 2222-22222"
},
"itens":[
{
"item":{
"codigo":1234,
"descricao":"Mochila",
"quantidade":"2.0000",
"valorunidade":"69.990000000",
"precocusto":null,
"descontoItem":"0.00",
"un":"Pç",
"pesoBruto": "0.520",
"largura": "20",
"altura": "18",
"profundidade": "15",
"unidadeMedida": "cm",
"descricaoDetalhada": "Ecobag esportiva"
}
}
],
"parcelas":[
{
"parcela":{
"idLancamento": "123456",
"valor":"73.74",
"dataVencimento":"2017-07-28 00:00:00",
"obs":"Teste obs 1",
"destino": "3",
"forma_pagamento":{
"id":"1",
"descricao":"Dinheiro",
"codigoFiscal":"1"
}
}
},
{
"parcela":{
"idLancamento": "123457",
"valor":"73.74",
"dataVencimento":"2017-08-28 00:00:00",
"obs":"Teste obs 2",
"destino": "3",
"forma_pagamento":{
"id":"1",
"descricao":"Dinheiro",
"codigoFiscal":"1"
}
}
}
],
"nota":{
"serie":"1",
"numero":"012346",
"dataEmissao":"2017-08-28 00:00:00",
"situacao":"1",
"chaveAcesso":"43140401056417000139550010000123461496923524",
"valorNota": "147.48"
},
"transporte":{
"transportadora":"Transportadora",
"cnpj":"00.000.000/0000-1",
"tipo_frete":"R",
"volumes":[
{
"volume":{
"id": "6423805580",
"idServico":"1231293",
"servico":"SEDEX 10",
"codigoServico":"03158",
"codigoRastreamento":"SX052413651BR",
"dataSaida":"2017-07-28",
"prazoEntregaPrevisto":"1",
"valorFretePrevisto":"11.25",
"valorDeclarado": "0.00",
"remessa":{
"numero":"12345AAA",
"dataCriacao":"2017-10-17"
},
"dimensoes":{
"peso":"1.553",
"altura":"20",
"largura":"15",
"comprimento":"20",
"diametro":"0"
},
"urlRastreamento":"https://www.exemplo.com.br/rastreio?q=EC272330554BR"
}
},
{
"volume":{
"id": "6423805635",
"idServico":"1231293",
"servico":"SEDEX 10",
"codigoServico":"03158",
"codigoRastreamento":"SX052413651BR",
"dataSaida":"2017-07-28",
"prazoEntregaPrevisto":"1",
"valorFretePrevisto":"11.25",
"remessa":null,
"dimensoes":{
"peso":"1.553",
"altura":"20",
"largura":"15",
"comprimento":"20",
"diametro":"0"
}
}
}
],
"enderecoEntrega":{
"nome":"Organisys Software Entrega",
"endereco":"Rua Assis Brasil",
"numero":"222",
"complemento":"",
"cidade":"Bento Gonçalves",
"bairro":"Centro",
"cep":"95.700-000",
"uf":"RS"
}
}
}
}
]
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
