Loading data error. Why isn't it loading? - javascript

I am trying to set up google sheets to pull data from an API. At first it worked, now it just says "Loading error..." on the cell with the formula.
I have tried it on a new sheet. I have simplified it to one input variable.
function getPrice(item,city){
var url = "https://www.albion-online-data.com/api/v1/stats/prices/"+item+"?
locations="+city;
var jsondata = UrlFetchApp.fetch(url);
var object = JSON.parse(jsondata.getContentText());
return object[0].sell_price_min;
}
The variables are "item" and "city". For which examples include "T1_Wood" item in "Caerleon" city. I'd like to make a table to pull the prices for different items in different cities. Thank you!

Related

Google Sheets Script to duplicate data foreach country code

I have a list of products with their attributes. Example column names are:
id, availability, condition, description, title, price, sale_price
In another sheet "cheat" I have a list of country codes like, FR and DE.
For each of those country codes I want to duplicate the product data and for each of those duplications add the country code in and "override" named column.
Here is a link to an example doc:
https://docs.google.com/spreadsheets/d/1kkcpovmWAcqa3hRBmsw-8g3R1FKIUfNqD5BF6tI4egI/edit?usp=sharing
Any help here is appreciated as I'm struggling to even get started.
I've checked your sample doc sheet and came up with this method below:
Recommendation
I have created a script with custom function named showResult():
function showResult() {
var ss = SpreadsheetApp.getActive();
var products = ss.getSheetByName('products'); //Gets all data on 'products' sheet
var cheat = ss.getSheetByName('cheat'); //Gets all data on 'cheat' sheet
var row = products.getDataRange().getNumRows(); //Counts current # of rows on 'products' sheet
var cheatRow = cheat.getDataRange().getNumRows(); //Counts current # of rows on 'cheat' sheet
var finalResult = [[products.getRange(1,1).getValue().toString(), //Initalize the column titles first on the finalResult array
products.getRange(1,2).getValue().toString(),
products.getRange(1,3).getValue().toString(),
products.getRange(1,4).getValue().toString(),
products.getRange(1,5).getValue().toString(),
'override']]; //Adds the 'override' column
for(var i=2; i<=cheatRow; i++){ //First loop to get each country codes
for(var x=2; x<=row; x++){ //Final loop to add each country codes to the each copy of grouped product values
finalResult.push([products.getRange(x,1).getValue().toString(),
products.getRange(x,2).getValue().toString(),
products.getRange(x,3).getValue().toString(),
products.getRange(x,4).getValue().toString(),
products.getRange(x,5).getValue().toString(),
cheat.getRange(i,1).getValue()
]);
}
}
SpreadsheetApp.getActive().getSheetByName('output').clear().getRange(1, 1, finalResult.length, finalResult[0].length).setValues(finalResult);
}
function onOpen() { //[Optional] Added a custom menu to manually to refresh your 'output' sheet
var ui = SpreadsheetApp.getUi();
ui.createMenu('Refresh output sheet')
.addItem('Refresh', 'showResult')
.addToUi();
}
This script will get all data from 'products' sheet (contains 5 columns):
Then, the script will start adding each country codes from 'cheat' sheet, as seen below, to every group of data from the 'products' sheet:
To run the script, you can click the "Refresh output sheet > Refresh" menu on your Spreadsheet:
To be able to dynamically update your 'output' sheet, you need to create a time-based trigger on the Apps Script editor, like this one that I've created to run the showResult() every 1 minute:
RESULT
On the 'output' sheet, this will be the result:
If there's a new 'product' data, it will be updated to the 'output' sheet as seen here:

How to archive certain rows to another sheet

I am trying to move my team's productivity to another sheet via Apps Script.
On the sheet "Prod" you can see the weekly productivity the team has put in. The "Approved" column has checkboxes where the work can be manually verified.
The second sheet is the "Archives" sheet. Where the Apps Script will move all of the responses from the "Prod" sheet over there.
Apps Script Code
function archive() {
// Get the information
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Prod');
var a = ss.getSheetByName('Archives');
var data = s.getRange('A2:F').getValues();
// Archive the results
for (i in data) {
var write = data[i];
a.appendRow(write);
}
// Clear the results
s.getRange('A2:E').clear();
s.getRange('F2:F').uncheck();
}
The Problem
The code works perfectly fine, however, when it is moving the "Prod" responses over to the "Archives" it includes the empty rows. This is because in the "Approved" column the checkbox is technically false, which means it's included. How would I go about changing the code to only transfer rows that have data in the A column?
Thanks!
I believe your goal as follows.
You want to copy the values from Prod to Archives when the value of column "A" in Prod is not empty.
Modification points:
In your script, all values of var data = s.getRange('A2:F').getValues(); is copied to Archives. So in this case, it is required to check the column "A" of data.
When appendRow() is used in a loop, the process cost will be high. Ref
When above points are reflected to your script, it becomes as follows.
Modified script:
function archive() {
// Get the information
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Prod');
var a = ss.getSheetByName('Archives');
var data = s.getRange('A2:F').getValues();
// Archive the results <--- I modified this part.
var copyValues = data.filter(([a]) => a.toString() != "");
a.getRange(a.getLastRow() + 1, 1, copyValues.length, copyValues[0].length).setValues(copyValues);
// Clear the results
s.getRange('A2:E').clear();
s.getRange('F2:F').uncheck();
}
References:
Benchmark: Reading and Writing Spreadsheet using Google Apps Script
filter()
setValues(values)

How to get information on hidden rows from Google Sheet API without Google Apps Script

I am using the Google Sheets API V4 to retrieve its data in JSON like below. That includes all rows, even the ones currently not shown in the Spreadsheet UI because of a filtering in one of the columns.
Is there a way to get only the shown rows or information whether a row is hidden or not?
The Google Apps Script allows such data retrieval using i.e. the .hiddenByFilter Method (see https://cloud.google.com/blog/products/application-development/using-google-sheets-filters-in-add-ons ). However, I am not able to include that in my API Query whatsoever.
$.getJSON("https://sheets.googleapis.com/v4/spreadsheets/" + SpreadsheetID + "/values/Color1!A2:Q?key=<MY_API_KEY>", function(data) {
// data.values contains the array of rows from the spreadsheet. Each row is also an array of cell values.
$(data.values).each(function() {
var location = {};
location.title = this[2];
location.latitude = parseFloat(this[10]);
location.longitude = parseFloat(this[9]);
location.institution = this[3];
//location.hidden = <?>;
locations.push(location);
});
Is there any workaround?
You can use Spreadsheets.get endpoint. Then filter sheets/data/rowData/values using sheets/data/rowMetadata/hiddenByUser field client side.

Google Sheets/JIRA Connection

I'm trying to connect Google Sheets to JIRA to gather the data for updating reports automatically.
I'm struggling however with a couple of points in this modified script.
I want to return the component field but calling the name field returns undefined.
var components = data["issues"][id].fields.components.name;
If I remove the name field, then I get the following response:
{name=#####, self=https://www.#########/rest/api/2/component/26357, id=26357}
The second issue is that only a handful of issues are being rendered. As far as I can see my REST call looks OK, as does the writing to tables:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); //select active spreadsheet
sheet.getRange(2, 1, issuessss.length, 7).setValues(issuessss); // write from cell A2
break;
Anyone have any ideas that could help?
The field component is an array so components.name does not exist (i.e components[0].name would work).
Before setting the values, try to execute a flush first.

Send HTTP Post When Google Spreadsheet Updated

I already have a spreadsheet that submitted by 3rd party apps (which I can't change its code).
Then, I need to send HTTP (Post) automatically to http://THISISMYURL.url/go.aspx?fieldone=xxx&fieldtwo=xxx when my Google spreadsheet updated.
What in my mind is to create a script in Google Spreadsheet and use UrlFetchApp in my code.
function SendHTTPpost() {
//How to pick data from any field in last column (everytime google spreadsheet updated)?
var data =
{
"fieldOne" : "value for field one (from last column)",
"fieldTwo" : "value for field two (from last column)",
};
var options =
{
"method" : "post",
"payload" : data
};
UrlFetchApp.fetch("http://THISISMYURL.url/go.aspx", options);
}
I know that I missed some command which to fetch some data from spreadsheet. Anybody get some idea to do this?
Thanks for helping me :)
Thanks to #Mogsdad for giving a clue for me. And thanks to #Zig Mandel for suggesting me to read spreadsheetApp documentation.
Here are what I did in my code, and it's works!
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the first sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();
// figure out what the last row is
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
At the last command, I use UrlFetchApp classes by fetch(url) method that I learn from here
Thanks to all for helping me.
And hopefully it will help others too.
Yeay!!!

Categories