Convert a string into a GmailThread object - gmail-addons

How can you convert a string into a GmailThread object?
var threadid = PropertiesService.getScriptProperties().getProperty('threadid');
var msgs = threadid.getMessages(); //error
Thks,
jfg

Check getThreadById(id) of GmailApp. Hence your code will be like:
var thread = GmailApp.getThreadById(threadId);
var msgs = thread.getMessages();

Related

How to define numbers inside a string to map JSON in Javascript?

The API has numbers to define each of the data containers, I've tried to use several ways to be able to define these numbers, but without success.
In the image you can see the map clubes.262 clubes.264 clubes.265 clubes.266 clubes.275
My attempts:
var idclub = clubes[i].id;
var idclub = clubes.[i].id;
var idclub = clubes[0][i].id;
var idclub = clubes. + i + .id;
The complete script for easy viewing:
function MenuMercadoCartola1() {
var url = 'https://api.cartolafc.globo.com/atletas/mercado';
var response = UrlFetchApp.fetch(url);
var results = JSON.parse(response.getContentText());
var clubes = results.clubes;
var table = [['ID do Clubes','Nome do Clube']];
for (var i = 0; i < clubes.length; i++) {
var idclub = clubes[i].id;
var nameclub = clubes[i].nome;
table.push([idclub,nameclub]);
}
var sheet = SpreadsheetApp.getActive().getSheetByName('Menu');
sheet.getRange(1,1, table.length, table[0].length).setValues(table);
}
It appears that you should be using a 'for in' loop.
Your code revised:
for (var idclub in clubes)
{
idclub = +idclub;
var nameclub = clubes[idclub].nome;
table.push([idclub, nameclub]);
}
Your loop assumes clubes is an array however it is an object which means you could have to iterate on it using a for (var ... in ...) loop.

.getRangeList won't return values from a range of cells

var data_range = srcSheet.getRangeList(['AK7:BB39','BD7:BD39']);
var data_data = data_range.getValues();
Why won't data_data store the array of the .getRangeList?
Error received is "data_range.getValues is not a function".
Instead of this:
var data_range = srcSheet.getRangeList(['AK7:BB39','BD7:BD39']);
var data_data = data_range.getValues();
Try this:
var rgA = srcSheet.getRangeList(['AK7:BB39','BD7:BD39']).getRanges();
var data=[];
rgA.forEach(function(rg,i){
data.push(rg.getValues());
});
data is now an array of two dimensional arrays of data.
Class RangeList

How can I get the next or previous character of findText()

How can I get the next or previous character of findText().
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var str = doc.getbody();
var i = "f";
var x = str.findText(i);
var y = str[str.index(x)-1];
Logger.log(y);
}
It is not working. and get some error.
Example: string = "Abcdefgh" , var x=string.findText("f"); after found "f"; want to get previous char of "f". which is "e" in this case. I want to know special function in google script to get
Flow:
getBody() returns a body object. You need string type. getText() returns string.
Use regexp#exec to get previous and next character of the search string
Snippet:
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var str = doc.getBody().getText();//modified
var search = "f";
var regx = new RegExp("(.)"+search+"(.)")
var y = regx.exec(str);
Logger.log(y);
if (y !== null){ Logger.log("Previous character" + y[1]).log("Next character:" + y[2])}
}
To practice:
Regexp#exec
Doc#body

Parse JSON formatted result of HTTP request into columns

Google Apps Script to pull data from API. I'd like to parse out the information according to the relevant headers.
function Fraud2() {
var ret = "no value";
var response = UrlFetchApp.fetch("https://fraudshield.24metrics.com/api/v1/reports/fraud.json?tracker_id=905&group[]=sub_id&group[]=partner&date_start=2018-01-18&date_end=2018-01-18&timezone=UTC&user_id=XXX&api_token=XXX",{muteHttpExceptions:true})
var user_id = "XXX";
var api_token = "XXX";
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow([response]);
}
The return is pushed into one single cell like so:
{"results":[{"tracker_id":905,"conversion":7883,"click":0,"goal":0,"approved":6511,"rejected":1372,"tracker":"Tatoo
Integration","conversion_rate":"N\/A"},{"tracker_id":906,"conversion":1868,"click":0,"goal":0,"approved":1682,"rejected":186,"tracker":"Aise
Integration","conversion_rate":"N\/A"},{"tracker_id":933,"conversion":413,"click":0,"goal":0,"rejected":290,"approved":123,"tracker":"Tatoo
Invalids Integration","conversion_rate":"N\/A"}]}
I tried this without success.
How can I get the results arranged neatly into columns?
As Hink said, you need to convert the object to an array first.
Hinks solution will work fine but below is a combination of the code you tried to use in your post.
function Fraud2() {
var ss = SpreadsheetApp.getActiveSheet();
var ret = "no value";
var response = UrlFetchApp.fetch("https://fraudshield.24metrics.com/api/v1/reports/fraud.json?tracker_id=905&group[]=sub_id&group[]=partner&date_start=2018-01-18&date_end=2018-01-18&timezone=UTC&user_id=XXX&api_token=XXX",{muteHttpExceptions:true})
var user_id = "XXX";
var api_token = "XXX";
var out=JSON.stringify(response.results);
out=JSON.parse(out)
var title = [];
for (var i in out[0]) {
title.push(i);
}
var res = [];
res.push(title);
for (var i in out) {
var values = [];
for (var j in out[i]) {
values.push(out[i][j]);
}
res.push(values);
}
ss.getRange(1, 1, res.length, res[0].length).setValues(res);
};
You need to convert the response object into an array and append the array:
for (var i = 0; i < response.results.length; i++){
var current = response.results[i];
var myArray = [current.tracker_id, current.conversion, current.click, current.goal, current.approved, current.rejected, current.tracker,current.conversion_rate];
sheet.appendRow(myArray);
}

Google Script to use a cell array and return a list of value from Json API

I want to use a list of cell on the same column to build a custom url and fetch API data. I wrote the code for a single cell (and single value return) but don't know how to extend to the entire column:
function checkAddress() {
var addresses = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Addresses");
var baseUrl = 'https://my.api.website/xxxxx&address=';
var address = addresses.getRange(1, 1);
var addrID = address.getValue();
var url = baseUrl.concat(addrID);
var responseAPI = UrlFetchApp.fetch(url);
var json = JSON.parse(responseAPI.getContentText());
var data = [[json.result]];
var dataRange = addresses.getRange(1, 2, 1, 1);
dataRange.setValue(data);
}
The var addrID is the one that change, and all of them are in the A column; I would like to return the result to the B column on the same row.
Any help would be appreciated, thank you
You just need to loop through Col A. Try the below Code.
function checkAddress() {
var addresses = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Addresses");
var baseUrl = 'https://my.api.website/xxxxx&address=';
var data = addresses.getRange(1, 1,addresses.getLastRow()).getValues();
for(var i=0;i<data.length;i++){
var addrID = data[i][0];
var url = baseUrl.concat(addrID);
var responseAPI = UrlFetchApp.fetch(url);
var json = JSON.parse(responseAPI.getContentText());
var data1 = [[json.result]];
var dataRange = addresses.getRange(i+1,2,1).setValue(data1);
}
}