Get data from web depending on cell value (JSON) - json

I can see here (https://www.youtube.com/watch?v=MbJcOL39H8s) how to get data from web, but how can I get data from web depending on a cell value.
For example: 'https://someurl.com/json?v=' + A$2
Thank you very much.

I think I found an answer:
https://exceltown.com/en/tutorials/power-bi/power-query-m-language/value-from-excel-cell-directly-to-power-query/
Everything is well explained.
So in Power Query:
var = Excel.CurrentWorkbook(){[Name="tableName"]}[Content][columnName],
Origem = Json.Document(Web.Contents("https://someurl.com/file.php", [Query=[a=var{0}, a1=var{1}]])),
tableName: Is the name of the table you create;
columnName: Is the name of the column you create in that table.
Thank you all.

Related

How do I get values of a column in an array and search for a value in it? (Google Sheets' Apps Script)

I have this sheet in my Google sheets spreadsheet which looks like this - (this is a simpler representation of my actual data)
Now let's say I want to find the row number of where animal = dog - wrt above data I should get 4 in return - as in the column 1, the value "dog" is in the 4th row. I built this code in Apps Script but it keeps returning -1 and I don't understand why...
var values = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();
Logger.log(values);
//above prints [[animal, owner], [cat, Max], [doggo, Sam], [dog, Jack], [cow, Mary], [rabbit, Jimmy]]
var index = values[0].indexOf("dog");
var responsePrint = index;
Logger.log(responsePrint); // -1 is printed
What should I do if I want to get the 1st column of my sheet in an array and search for a particular value in it? I'm trying to avoid using a loop to scan each and every element, as my data might get large - so if an inbuilt function can do this, it'd be great for me... Any idea is appreciated! Thanks :)
P.S. I'd like to say this might sound like a question which has been asked before, but trust me I've searched a lot, and I can't seem to find a suitable solution for me...
values is a 2D array, or an array of arrays. The first index of values is the row and the second index is the column so values[1][0] would be the second row first column.
So in your case you could do this.
let index = values.findIndex( row => row[0] === dog );
To get the row you have to add 1 to the index because index are zero based.
What findIndex does is it takes each element of values which is a "row" and then looks at the first value in the row array and compares to "dog"
Reference
Array.findIndex()
Arrow function
If you want to use indexOf you need to isolate the first column of your data and then use the indexOf method:
function myFunction(){
var values = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();
var index = values.map(r=>r[0]).indexOf("dog") + 1;
console.log(index); //outputs 4
}

How can I group exact string together?

I'm working with a google sheet that contains a column of unique project numbers followed by a column with the equipment description used in that project. I have set up my code to send an automatic email with this information to a list of recipients. What I want to do that I don't know how to is what code can I use that will read the project number column and when it finds two project numbers exactly the same, it will group the equipment together into a list that will be sent in that automatic email. I know the list can be a variable but I just don't know how to make it look for spa project numbers to join the equipment together without changing anything in the google sheet. Thank y'all!
I'm agree with Cooper. To build an object is a most obvious way to get unique values (ID, etc) from any list:
var data = [
[1,'a'],
[2,'b'],
[3,'c'],
[1,'d']
]
var obj = {};
data.forEach(x => {
try { obj[x[0]].push(x[1]) } // try to add the value to an existed array
catch(e) { obj[x[0]] = [x[1]] } // or make an array if there is no existed one
});
console.log(obj); // out: [ {1:[a,d]}, {2:[b]}, {3:[c]} ]
var id = 1
console.log('All the values from ID '+ id + ': ' + obj[id].join(', ')); // out: a, d

Apps Script summing up Query Data (original data is number) results in concatenation

I came across the following issue when I am trying to work on Query data (data resulting from query function in Google Sheet) using Apps Script:
Query funtion used: =Query('data source', "select ...sum(..)... where ... group by... pivot...")
Script used:
var summe = 0;
for (i=0;i<data.length;i++) {
for (k=0;k<data[i].length;k++){
summe = summe + data[i][k];
console.log(...); # to track exactly which column's value becomes text
console.log(summe);
}
The results in execution log show that from a certain point it starts to concate text instead of summing up values:
enter image description here
Appreciate a lot in advance for any help or ideas out there!!!
Best regards,
Xubo
You can avoid this problem by manually converting your data to numbers:
summe = summe + Number(data[i][k]);
See Number.
In addition to the Zicanotchka's answer. There is a convenient way to keep track the types of your variables:
var a = 1;
var b = "1";
var c = [1];
console.log(a.constructor.name) // Number
console.log(b.constructor.name) // String
console.log(c.constructor.name) // Array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor

How do I use filterByFormula in Airtable API with Google App Scripts?

I am able to retrieve records using views, but I am not sure how to filterByFormula in Google App Scripts
function mwe() {
API_KEY = SECRET;
BASE_URL = BASE;
var url = "https://api.airtable.com/v0/" + BASE_URL + "/TRNO?api_key=" + API_KEY + '&maxRecords=3&view=All%20not%20entered';
var response = UrlFetchApp.fetch(url);
var result = JSON.parse(response.getContentText());
var myResult = result["records"]
Logger.log(myResult);
}
There are a few things that can be done to try to achieve the formula field via the API.
The more in-depth and entwined the data and in the formulas are naturally it becomes harder to scheme JSON from retrieving or query of the base and records. i.e.
lookups
rollups
Nested Ifs with rollups lookups & links
Or concatenates string from link to other formulas in lookups or rollups.
In your case, there is only the need to use an encoded URL try this tool will help you correctly establish the correctly, encoded URL needed. Plus save you some time in the future.
https://codepen.io/airtable/full/rLKkYB
The Endpoint should maybe look something like this below once you play around with it.
var url = "https://api.airtable.com/v0/YOUR_BASE_ID/filterByFormula=NameOfYourFormulaField"/TRNO?api_key=" + API_KEY + '&maxRecords=3&view=All%20not%20entered'";
But when trying to filterByRecords and retrieve records using views the value for filterByFormula should be a string. You can test the formula by creating a formula field from the Airtable UI.
{
pageSize: 50,
filterByFormula: '{Record1} = TRUE()',
sort: [{field: 'Record1 Date',direction: 'desc' }]
}
And just one last thing,If you are wanting to use filterByFormula with record IDs in a linked table, you can create a formula in the linked table that is RECORD_ID(), then add a lookup to that formula field in the main table. and list NameFormalfield
[https://api.airtable.com][2] goes a lot more in-depth into the String formatting etc.
Hope this helps Good Luck.

I am creating a copy of a Spreadsheet. How do I make a script to copy the users that can view/edit/own the file across aswell?

I am creating a copy of a Spreadsheet. How do I make a script to copy the users that can view/edit/own the file across aswell?
Currently I cannot change the owner nor add editors or viewers that are as part of a googlegroup.
Thank you very much for your replies. GREATLY APPRECIATED.
I used this ...
var originalowner = originalSpreadsheet.getOwner();
var originaleditors = originalSpreadsheet.getEditors();
var originalviewers = originalSpreadsheet.getViewers();
// Logger.log(originalowner);
// Logger.log(originaleditors);
// Logger.log(originalviewers);
newSheet.addEditor(originaleditors);
enter code here newSheet.addViewer(originalviewers);
and get an error
Invalid email: xxxxxxx#googlegroups.com, yyyyyyyyy#googlegroups.com
If you read the documentation you will see that the argument for addEditor() is a single email as a string. You are trying to use an array of strings and the error message returned is quite explicit Invalid email: xxxxxxx#googlegroups.com, yyyyyyyyy#googlegroups.com
You should use a simple loop to add each viewer / editor one by one.
for(var n = 0 ; n<originaleditors.length ; ++n){
newSheet.addEditor(originaleditors[n]);
}
and do the same for viewers...
EDIT : or, as mentioned in Fred's comment (good point ! ) use the addEditors() method (with an s) that takes an array of editors emails as argument.
Note : as I said, (re)reading the documentation is always a good idea, that's true for me too ;-)