I have a for loop inside a getjson call, but the loop not work…
var woeid = '455827';
var yql = encodeURIComponent('select * from weather.forecast where woeid = "' + woeid + '"and u="c"')
$$.getJSON('https://query.yahooapis.com/v1/public/yql?q='+ yql + '&format=json', function (data) {
var forecast = data.query.results.channel.item.forecast;
//Forecast
for(var i=0;i<=forecast.length;i++){
//Get
code = data.query.results.channel.item.forecast[i].code;
data = data.query.results.channel.item.forecast[i].date;
weekday = data.query.results.channel.item.forecast[i].day;
max = data.query.results.channel.item.forecast[i].high;
min = data.query.results.channel.item.forecast[i].low;
console.log(max);
}
});},3000)
The loop not work and console.log show nothing… What's wrong?
Thanks
Solved: I change my code to this:
Rather than
var forecast = data.query.results.channel.item.forecast;
for(var i=0;i<=forecast.length;i++){
//Get
code = data.query.results.channel.item.forecast[i].code;
data = data.query.results.channel.item.forecast[i].date;
weekday = data.query.results.channel.item.forecast[i].day;
max = data.query.results.channel.item.forecast[i].high;
min = data.query.results.channel.item.forecast[i].low;
}
I did
var forecast = data.query.results.channel.item.forecast;
for(var i=0;i<=forecast.length;i++){
//Pega
code = forecast[i].code;
data = forecast[i].date;
weekday = forecast[i].day;
max = forecast[i].high;
min = forecast[i].low;
}
Although I believe that the first code should work, the second code works for me like a charm
Thank you all :)
Related
I have a code that retrieves project ID from specific column and uses it on API call to get more data that needs to be appended starting from the last column with data.
Here is the code that reads above column and uses it for API call
function readDates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("F2:F" + sheet.getLastRow()).getValues();
var searchString = "Project";
var contents = [];
var team_array = [];
for (var i = 0; i < range.length; i++) {
var lastRow = sheet.getRange(2 + i, 1, 1, 8).getValues();
var data = {'project_id': lastRow[0][3]};
var options = {method: 'get',headers: {Authorization: 'Bearer ' + TKF_AUTH}};
var url = TKF_URL + Endpoint + data.project_id + '/users?auth=' + TKF_AUTH
var response = UrlFetchApp.fetch(url, options);
var team = JSON.parse(response);
var content = team.data;
team_array.push(content);
contents = contents.concat(content);
}
The response needs to be appended from column G, row 2 but this is what I find challenging to do and any help will be appreciated here. Take a look at my code blow does, which is not what I intended.
if (contents.length > 0 ) {
var dstSheet = SpreadsheetApp.getActive().getSheetByName('Projects');
var values = contents.map(e => ([e.id, e.first_name, e.last_name, e.display_name, e.email, e.user_type_id, e.role]));
var new_values=values.filter(item=>{
if(item[6] == 'Project Leader' || item[6] == 'Senior Project Leader')
{
return true
}
})
dstSheet.getRange(7, 1, new_values.length, new_values[0].length).setValues(new_values);// How do I structure this part to ensure it starts seting values from column G
}Logger.log(contents.map)
}
Is this what you're looking for:
function readDates() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var vA = sheet.getvA(2,1,sheet.getLastRow()-1,sheet.getLastColumn()).getValues();
var w=sheet.getLastColumn();
for (var i = 0;i<vA.length;i++) {
var options = {method: 'get',headers: {Authorization: 'Bearer ' + TKF_AUTH}};
var url = TKF_URL + Endpoint + vA[i][3] + '/users?auth=' + TKF_AUTH
var response = UrlFetchApp.fetch(url, options);
var team = JSON.parse(response.getContentText());
var v=vA.slice(0,7);
vA[i]=v.concat(team.data);//assume team.data is an array
if(vA[i].length>w){w=vA[i].length;}
}
sh.getRange(2,1,vA.length,w).setValues(vA);
}
It would be nice to see what the return data looks like and if possible could we create headers that we can use to place the data correctly in the extended columns.
i want to show bitcoin price but there are several exchangers. i want to get from bitstamp only but positions of exchangers are changing and can't get only one
i wrote some code on jquery but i can't finish it
$.getJSON('https://chain.so/api/v2/get_price/BTC/USD', function(data) {
var text = ` ${data.data.prices[1].price} `
var integer = parseFloat(text, 10)
var percent = 3
var display = (integer - (integer/100 * percent)).valueOf()
$(".mypanel").html(display.toPrecision(7) + '$');
});
if you want to get request with jquery you should use $.get() and I write you code again (jquery version 3.3.1) get more detail
$(document).ready(function() {
var url= "https://chain.so/api/v2/get_price/BTC/USD";
$.get(url,function(data){
var text = ` ${data.data.prices[1].price} `
var integer = parseFloat(text, 10)
var percent = 3
var display = (integer - (integer/100 * percent)).valueOf()
$(".mypanel").html(display.toPrecision(7) + '$');
console.log(data);
})
});
https://jsfiddle.net/unqL14gs/4/
my first ever question pretty sure I'm being a bit daft here, but am a beginner and would appreciate your help.
Im working on a webpage where there is a html table listing several columns of data.
When the page loads it runs a jquery script which counts the different types of "incidents" and plots them in another table which then another jquery script populates a graph.
I have a third script (javascript) which after a button is clicked, runs an if loop, which looks at the data in the first column and if it does not match the criteria then the row is deleted.
So far so good, the issue is that I want the script which populates the table for the graph to run again, but Im not sure how to call it from my if loop.
Ive put the two scripts below, basically I want to call the 1st script in the second script.
$(function () {
var NumFireAlarms = $("#incidents tr:contains('Fire Alarm')");
$("#result").html(NumFireAlarms.length + " Fire Alarm");
var firealarms = NumFireAlarms.length;
document.getElementById("no_of_incident_type").rows[1].cells[1].innerHTML = firealarms
var NumLockout = $("#incidents tr:contains('Lockout Out of Office Hours')");
$("#result").html(NumLockout.length + " Lockout Out of Office Hours");
var lockouts = NumLockout.length;
document.getElementById("no_of_incident_type").rows[2].cells[1].innerHTML = lockouts
var NumLockoutDayTime = $("#incidents tr:contains('Lockout Day Time')");
$("#result").html(NumLockout.length + " Lockout Day Time");
var lockoutsDayTime = NumLockoutDayTime.length;
document.getElementById("no_of_incident_type").rows[3].cells[1].innerHTML = lockoutsDayTime
var NumSensitiveIncident = $("#incidents tr:contains('Sensitive Incident')");
$("#result").html(NumSensitiveIncident.length + " Sensitive Incident");
var SensitiveIncident = NumSensitiveIncident.length;
document.getElementById("no_of_incident_type").rows[4].cells[1].innerHTML = SensitiveIncident
});
function filterForGraph() {
var incident_category = document.getElementById("Incident_Category").value;
var start_date = document.getElementById("start_date").value;
var end_date = document.getElementById("end_date").value;
var staff_type = document.getElementById("Job_Title").value;
var i;
var count = 0;
var table_length = document.getElementById("incidents").rows;
var TL = table_length.length;
for (i = TL - 1; i >= 1; i--)
{
var category_column = document.getElementById("incidents").rows[i].cells.item(0).innerHTML;
var date_column = document.getElementById("incidents").rows[i].cells.item(1).innerHTML;
var staff_colunm = document.getElementById("incidents").rows[i].cells.item(8).innerHTML;
if (category_column === incident_category)
{
alert("yay")
count++
}
else if (category_column !== incident_category)
{
alert("boo")
document.getElementById("incidents").deleteRow(i);
//CALL FIRST SCRIPT HERE??
}
}
}
I removed a few bits of code that did not seem to do anything, but I'm sure you can put them back. I think you might want something like this:
function updateTable(){
var elResult = document.getElementById("result");
var elNumIncidentType = document.getElementById("no_of_incident_type");
var firealarms: document.querySelectorAll("#incidents tr:contains('Fire Alarm')").length;
var lockouts: document.querySelectorAll("#incidents tr:contains('Lockout Out of Office Hours')").length;
var lockoutsDayTime: document.querySelectorAll("#incidents tr:contains('Lockout Day Time')").length;
var sensitiveIncident: document.querySelectorAll("#incidents tr:contains('Sensitive Incident')").length;
elResult.innerHTML = "";
elResult.innerHTML += "<div>" + firealarms + " Fire Alarm</div>";
elResult.innerHTML += "<div>" + lockouts + " Lockout Out of Office Hours</div>";
elResult.innerHTML += "<div>" + lockoutsDayTime + " Lockout Day Time</div>";
elResult.innerHTML += "<div>" + sensitiveIncident + " Sensitive Incident</div>";
elNumIncidentType.rows[1].cells[1].innerHTML = firealarms;
elNumIncidentType.rows[2].cells[1].innerHTML = lockouts;
elNumIncidentType.rows[3].cells[1].innerHTML = lockoutsDayTime;
elNumIncidentType.rows[4].cells[1].innerHTML = sensitiveIncident;
}
function filterForGraph() {
var elIncidents = document.getElementById("incidents");
var incident_category = document.getElementById("Incident_Category").value;
var table_length = document.getElementById("incidents").rows.length;
for (var i = table_length - 1; i >= 1; i--) {
var currentIncident = elIncidents.rows[i].cells;
var category_column = currentIncident.item(0).innerHTML;
if (category_column != incident_category) { elIncidents.deleteRow(i); }
}
updateTable();
}
$(function(){ updateTable(); });
Hi JonSG tried your code and it didnt work not sure why, but it gave me some ideas to work with and I think Ive cracked it
function Populate_Incident_No_Table() {
//previously function called updateTable
$(function () {
var NumFireAlarms = $("#incidents tr:contains('Fire Alarm')").length;
document.getElementById("no_of_incident_type").rows[1].cells[1].innerHTML = NumFireAlarms
var NumLockout = $("#incidents tr:contains('Lockout Out of Office Hours')").length;
document.getElementById("no_of_incident_type").rows[2].cells[1].innerHTML = NumLockout
var NumLockoutDayTime = $("#incidents tr:contains('Lockout Day Time')").length;
document.getElementById("no_of_incident_type").rows[3].cells[1].innerHTML = NumLockoutDayTime
var NumSensitiveIncident = $("#incidents tr:contains('Sensitive Incident')").length;
document.getElementById("no_of_incident_type").rows[4].cells[1].innerHTML = NumSensitiveIncident
});
}
function filterForGraph() {
var incident_category = document.getElementById("Incident_Category").value;
var i;
var TL = document.getElementById("incidents").rows.length;
for (i = TL - 1; i >= 1; i--)
{
var category_column = document.getElementById("incidents").rows[i].cells.item(0).innerHTML;
if (category_column !== incident_category)
{
document.getElementById("incidents").deleteRow(i);
}
}
Populate_Incident_No_Table();
drawGraph();
}
I think the issue was how I was trying to call the functions. So what I've done to achieve what I wanted (please excuse any bad terminology / phrasing).
First I tried to name the function $(function updateTable(){ this did not work when I then tried to call the function like this updateTable();
Second thing I tried was putting the updateTable() function "inside" a function and call that function. This has worked for me I dont know why.
Thanks for your help without it I would not have thought to try what I did
I've been stuck for a few days now on this. I can get my albums to write to the console but I can't figure out how to get them to write into an array. The code works so when I click "Show Albums" button, it returns nothing other than [Object, object]. Can anyone help me figure out how to write the new albums to the array so it displays properly? Thanks in advance.
(function() {
var name = document.getElementById("name");
var year = document.getElementById("year");
var showbutton = document.getElementById("showlist");
var submitbutton = document.getElementById("submit");
var validate = document.getElementById("validate");
var albumlist = [];
var albums = function album(name, year) {
this.name = name;
this.year = year;
this.album = function() {
return ((this.name + ", " + this.year));
};
}
albums.prototype.genre = "rock";
var album1 = new albums("album1", "1999");
var album2 = new albums("album2", "2000");
var album3 = new albums("album3", "2001");
var album4 = new albums("album4", "2002");
var album5 = new albums("album5", "2003");
albumlist.push(album1, album2, album3, album4, album5);
showbutton.onclick = function() {
for (var i = 0; i < albumlist.length; i++) {
console.log(albumlist[i]);
}
document.getElementById("myalbums").innerHTML = albumlist.sort() + "<br/>";
}
document.getElementById("myalbums").innerHTML = albumlist.sort() + "<br/>";
Take a look at what this line does. This takes an array, which is an object with many functions, keys, etc, and tries to plot in on the page.
It will not be possible, since javascript doesn't know how to turn that array into a string.
For you to do that, you have to first create the string you want and than plot it on the page.
You need to iterate through the items and generate a string, for example:
var names = "";
for(var i = 0; i < albumlist.length; i++){
names += albumlist[i].name + ' ';
}
and then replace it on the html:
document.getElementById("myalbums").innerHTML = names + "<br/>";
So here is my scenario, I am "serializing" some json data with anonymous types in javascript to post back to an MVC controller. Maybe I am approaching this problem in the wrong way, but I have a list of objects or an array of objects that are in a nested object. Below is a sample of the data I am posting back.
var items = [];
var counter = 0;
$("#table > tbody > tr").each(function () {
var ModelId = $(this).find("#models option:selected").val();
var Attrib1 = $(this).find("#attrib1 option:selected").val();
var Attrib2 = $(this).find("#attrib2 option:selected").val();
var Price = $(this).find("#price").val();
var Notes = $(this).find("#notes").val();
var ModelName = "MyObjects[" + counter + "].ModelId";
var Attrib1Name = "MyObjects[" + counter + "].Attrib1";
var Attrib2Name = "MyObjects[" + counter + "].Attrib2";
var PriceName = "MyObjects[" + counter + "].Price";
var NotesName = "MyObjects[" + counter + "].Notes";
var item = {
ModelName: ModelId,
Attrib1Name: Attrib1,
Attrib2Name: Attrib2,
PriceName: Price,
NotesName: Notes
}
items.push(item);
counter++;
});
Tried to do this in a couple different ways (laughs are accepted) :). But essentially I need that property name to be the value of the variable e.g ModelName.
Any help is greatly appreciated!
Thanks,
Steven