Is there a way to modify json received callback? - json

I'm receiving a callback from a server in my Google app script via doPost.
The problem is, my Json format is with a this word in front of the Json "Data=", because of that I'm not able to work with the Json callback.
The code:
Function doPost(e){
var r = e.postdata.contents
Logger.log(r)
}
I'm receiving the bellow format.
data={""retorno"":{""estoques"":[{""estoque"":{""codigo"":""001a"",""nome"":""M\u00e1scara 100% Algod\u00e3o Lav\u00e1vel Dupla Prote\u00e7\u00e3o - 10 Unidades"",""estoqueAtual"":50,""depositos"":[{""deposito"":{""id"":7939278964,""nome"":""Geral"",""saldo"":""50.0000000000"",""desconsiderar"":""N"",""saldoVirtual"":""50.0000000000""}}]}}]}}
Anyway to remove this "Data="?
Thanks

If you just want to remove the substring data= - the easiest would be to use the method silce()
Sample:
var r = e.postdata.contents;
var sliced = r.toString().slice(5);
Logger.log(slide);

Related

Why is this Importxml formula not working?

The following formula does work for some, but not for others:
=IFNA(VALUE(IMPORTXML("https://finance.yahoo.com/quote/C2PU.SI", "//*[#class=""D(ib) Mend(20px)""]/span[1]")))
If used without IFNA, it says 'Resource at url not found'.
Here's the value I'm trying to pull in:
I appreciate if you could point me to the right direction.
Thank you!
It does not return any values even for simple importxml.
It seems the site is generated by javascript or protected so it can't be scraped by importxml.
Don't use the "inspect" tool as it will show the DOM as it's being rendered by the web browser including modifications to the source code by client-side JavaScript, instead look at the source code.
Resources
How to know if Google Sheets IMPORTDATA, IMPORTFEED, IMPORTHTML or IMPORTXML functions are able to get data from a resource hosted on a website?
The structure of the DOM is generated by javascript. Nevertheless, all informations you need are contained by a json string called here root.App.main. You can get all the data by these way
function extract(url){
var source = UrlFetchApp.fetch(url).getContentText()
return source.match(/(?<=root.App.main = ).*(?=}}}})/g) + '}}}}'
}
and then retrieve the data by conventionnal json parsing. This will give you the value
[![function marketPrice() {
var code = 'C2PU.SI'
var url='https://finance.yahoo.com/quote/' + code
var source = UrlFetchApp.fetch(url).getContentText()
var jsonString = source.match(/(?<=root.App.main = ).*(?=}}}})/g) + '}}}}'
var data = JSON.parse(jsonString)
var regularMarketPrice = data.context.dispatcher.stores.StreamDataStore.quoteData.item(code).regularMarketPrice.raw
Logger.log(regularMarketPrice)
}
Object.prototype.item=function(i){return this\[i\]};][1]][1]

Inserting sleep inside a particular function in Google Sheets Script

I'm trying to pull data out of an API from a third party and inserting into Google Sheets. However this third party only allows 3 requests per minute, so I'm trying to use a Utilities.sleep feature inside the function I'm building for this request.
My sheet looks like this:
It has the two inputs necessary for the function I'm using (this below):
function GET_DETAILS_RECEITA(CNPJ,sleep_seconds) {
Utilities.sleep(sleep_seconds*1000);
var fields = 'nome,fantasia,email,telefone';
var baseUrl = 'https://www.receitaws.com.br/v1/cnpj/';
var queryUrl = baseUrl + CNPJ;
if (CNPJ == '') {
return 'Give me CNPJ...';
}
var response = UrlFetchApp.fetch(queryUrl);
var json = response.getContentText();
var place = JSON.parse(json);
return [[ place.nome,
place.fantasia,
place.telefone,
place.email,
]];
}
Technically it should work but for some reason I'm getting a return only in the first one.
The error I'm getting is very generic "Erro: Erro interno ao executar a função personalizada." (something like "Error: Internal error in the execution of personalized function").
Any ideas?
From https://developers.google.com/apps-script/guides/sheets/functions
A custom function call must return within 30 seconds. If it does not, the cell will display an error: Internal error executing the custom function.
Considering the above, it's not a good idea to use sleep on a custom function that will be used as intended by the OP. Instead use a custom menu or the Script Editor to execute a script.
In order to minimize changes to your function, you could use a function that read/write the values to the spreadsheet and pass the required arguments to GET_DETAILS_RECEITA
I would consider using something like this in a dialog. You can pass an extra parameter in the set interval as long as your using Chrome.
<script>
var CNPJ='what ever';
window.onload=function(){setInterval(getDetails,25000,CNPJ);}
function getDetails(CNPJ){
google.script.run.GET_DETAILS_RECEITA(CNPJ)
}
</script>
And if you want a callback then use with withSuccessHandler();

Number object from http request returns date type or similar

I make a Facebook API call in Google scripts to get the share count for a URL. It appears that the number (e.g. 31) is being found correctly, but when I pass it to Sheets, it shows e.g. 30/01/1900 in the sheets box.
My appScript code is:
function getShareCount(url) {
var url = "https://any.org/111";
var inputurl = "https://graph.facebook.com/v2.1/?id=" + url + "&access_token=XXXXXXXX";
var response = UrlFetchApp.fetch(inputurl);
var response = JSON.parse(response.getContentText());
var response = response.share.share_count;
Utilities.sleep(500);
return response;
}
and the spreadsheet box has: "=getShareCount(B2)"
If I purposefully break the code and run the debugger in script Apps, I can see that script apps is getting a response with Number: 31. If I change to e.g. "response.id", the URL is returned into sheets as expected. The same with other parts of the object. Those are strings, and this is a number. I can't work out what sort of object sheets is receiving, nor what method I can use to simply show the number `311.
Any ideas? Thanks!
It seems that your cell has a custom format of Date. Select the cells you want to format and click Format > Number > Automatic

Google Spreadsheets as JSON

I want to know if and how is it possible to get data from specific cells of a Google spreadsheet without publishing the sheet as public using HTTP GET request to fetch data in JSON format.
I am not totally sure if this is what you are looking for, but you could just create a doGet() that returns a JSON object and then publish your project as a Webapp. Then make get requests to that URL.
function doGet() {
var cell = SpreadsheetApp
.openById('SPREADSHEET ID HERE')
.getActiveSheet()
.getRange('A1')
.getValue();
var stringified = JSON.stringify({cellValue: cell});
return ContentService.createTextOutput(stringified);
}
EDIT: You could even put in some URL parameters and make it return specific cells. Read more here.

Compare html responses using node js

I have a case where I send a request to a server and record the response. Then I craft the request and send it to server one more time and compare the response with the earlier recorded response.
I am using node.js and I want to know is there any best routine to compare HTML response in node.js which can directly point me the differences in both HTML responses.
Take a look at jsdiff, it can return to you the differences between two pieces of text, or HTML in your case, at a few different levels (chars, words, lines).
You can use a combination of jsdom and dom-compare:
var compare = require('dom-compare').compare,
jsdom = require('jsdom');
// Those are the HTML fragments that we want to compare:
var expectedHTML = '<div><i>m</i><b>q</b></div>';
var actualHTML = '<div><i>h</div>';
var expectedDOM = jsdom.jsdom(expectedHTML);
var actualDOM = jsdom.jsdom(actualHTML);
var result = compare(expectedDOM, actualDOM);
console.log('diff array:', result.getDifferences());
// we can use a reporter to pretty-print the result:
var reporter = require('dom-compare').GroupingReporter;
console.log(reporter.report(result));