Refresh Button / Auto Update GOOGLE SPREADSHEET - json

I hope you can help me. I have been trying to get this working for quite some time.
I am taking the API from a website import it into google spreadsheets VIA importjson()
ImportJSON("http://api.796.com/v3/futures/ticker.html?type=weekly")
https://gist.github.com/chrislkeller/5719258
I have no problem importing the json data into google spreadsheet, now the hard part is making the data update, I would really like it to auto update but even a "update now" button would be great.
Thanks,
This has been driving me nuts.
BV

So I was searching for a good way to do this as well. Here is what I have found so far.
If you change the function line of the importJSON like this:
function ImportJSON(url, query, parseOptions, recalc) {
}
Which just adds the recalc parameter to the function definition. This is a 'trick' because that parameter is not used for anything other than to make GSheet think something has changed.
Then in the call to ImportJSON (in a GSheet cell), you add in the function call like this:
=ImportJSON("https://yourDomain.com/something","","rawHeaders",A1)
Then in the sheet you can manually change the value of A1 to anything different than it is. The Sheet and all the imports will get refreshed.
Another ... non-technical trick ... manually change A1 to 1 then 2 then 3. Each time the sheet will recalc. Now you can use the UNDO and REDO buttons to change the numbers and recalc the sheet.
A little of a kludge Hack, but it does make for a one-button "recalc" of a sort.

You can just add the time trigger
https://developers.google.com/apps-script/understanding_triggers

File > Spreadsheet settings > Calculation > Recalculation: On change and every minute
Then you can insert a =now() column and it will refresh the sheet every minute.
Apologies if you've tried this already, I have it setup now in a sheet but am waiting for a live event to test, so it might not even work.

Im not really a javascript person, but I might have a little code that might help start you off. (Sorry if it does not help). What I use in my site is this (I changed it a little bit to incorporate your url).
$( document ).ready(function($) {
setInterval(function(){
$('.sell').load('http://api.796.com/v3/futures/ticker.html?type=weekly');
}, 5000);
});
This just grabs the JSON every 5 seconds, so be sure you parse the JSON and display the data you need. Hope this helps! :)

Related

Apps Script - some run, some don't

So I have a code that pulls NBA prop bets - I have a code that runs for rebounds, points and assists. It is supposed to load into the spreadsheet in each individual tab. When the code was first created, it ran fine. Now, only the rebounds code runs... the points or assists tab does not.
I'm not sure why 2 of the 3 are not running. *** Disclaimer - I am not very good at coding, kind of trying to self teaching, so I'm sorry if its a dumb question. ***
Here is the link: https://docs.google.com/spreadsheets/d/12qWi9cRoKMFLpAPhQ31p6sETXI4LvQ3-zXF5RBO8i4w/edit?usp=sharing
It is because you are using the same function name across all your 3 functions. This is causing the confusion on the apps script, even though they are in different .gs files you can't have the same function name. What you can do is rename these 3 functions then call them all in one function:
function SPORTBOOK(){
SPORTBOOKAssists();
SPORTBOOKPoints();
SPORTBOOKRebound();
}
Or another approach is, since your codes are repetitive you can just put them all in one function where you are changing the URL or have the 3 URLS and 3 sheets created in one function before setting the values to the sheet.

How to reference an external spreadsheet with Google Apps Scripts

Update: It worked for 5 minutes then stopped working again. Not exactly sure why it's only working part of the time?
This sounds like it's supposed to be simple but I'm only finding information on "getActive" and my function works if I'm using all internal sheets but I tried using openById and nothing happened and openByUrl returns an error. The following is a simplified example of what I'm trying to do:
var store_data_sheet = SpreadsheetApp.openByID('ID here..').getSheetByName('Data');
store_data_sheet.getRange('A1').setValue('testing')
store_data_sheet.insertRows(1);
I'm sure I'm just using the wrong keywords or maybe this isn't even allowed but multiple users will be using their own copy of a specific sheet that runs this function and I'd just like to take certain cell's data and copy it to one external sheet (which is why it also inserts a row so it can push down entries)
Maybe I'm using the wrong "ID"? I assumed the number inside the URL was the ID. If it's something more specific, please let me know.
Like I said, this works flawlessly if I'm referencing an internal sheet. I'd like to be able to do the same exact thing with an external spreadsheet if possible. Thank you!
I found the issue. This function also renames the sheet file so if there is anything in the cell that is used to rename the file, it won't run the storing functions. I either need to run these before the renaming or take out the renaming aspect. ---moving these before the renaming part fixed the problem.

Why won't my macro correctly copy/paste values only after the first instance?

Updated everything to include the ultimate goal of this spreadsheet and the issue I encountered during early testing so that if there's a better way to do this that someone with experience can identify, I can change my approach and do that instead.
Link to copy of sheet here
The Issue:
This macro uses paste special (values only) twice, but it only correctly pastes the first one and does nothing for the second. For example, the the first instance copies data from C6 and pastes only the value to F6, which it does. The second copies from K6 and pastes only the value to M6, which it does not do. Everything else is functioning correctly except for this.
For context, this macro also copies a formula down in between the two copy/paste actions. You'll see the code for that below as well. It's working as intended, so you can ignore that.
With help from below, we identified the issue was that the second paste values only function wasn't working because it was copying the cell before that cell could finish summing its formula.
For example, let's say K6 was =sum(K4+K5) with K4=1 and k5=4. Before the sum could record the answer, "5" in that cell, the copy was grabbing the empty data and pasting it, thus pasting nothing.
I tried setting up a delay using sleep, but it was not a consistent solution. It only works about 50% of the time. If it's possible to set up a delay based around letting a formula finishing its output, that would be a perfect solution. If anyone has experience with a similar issue or suggestions for optimization threads, please link below. I'll loop back if I find a solution.
The Goal and Backstory:
This is a mock-up of an internal tracker we use to gauge our availability (mins) against the project (in mins) to determine roughly when we have the project complete. We use this tracker to determine if something will be late, if we have the availability from our team to take on more projects, etc. It's all manual, and we don't have anyone particularly strong with scripts.
Right now, our solution is to manually enter in our availability in each cell with the assistance of basic sum formulas. This takes a lot of time and is prone to human error. So, I'm trying to find a macro that will do the math for us with my very limited experience in scripts/macros.
In the "The Ideal" tab, I've manually created an example of what I'm ultimately working towards -- a macro that sums and enters in data based on availability vs. project mins. I'm obviously new to this, so there are inevitably inefficiencies and I may be completely on the wrong track. If so, please tell me and offer me an explanation as if you were speaking to a grandparent with basic computer knowledge.
Thanks everyone for your help!
Here's the code:
function TestCalc1() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('D5:H5').activate();
spreadsheet.getActiveRange().autoFill(spreadsheet.getRange('D5:H6'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
spreadsheet.getRange('I6').activate();
spreadsheet.getRange('H6').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
spreadsheet.getRange('H6').activate();
};
Try this,
function TestCalc1() {
var spreadsheet = SpreadsheetApp.getActive();
var cellsD5H5 = spreadsheet.getRange("D5:H5").getValues();
spreadsheet.getRange("D6:H6").setValues(cellsD5H5);
var cellH6 = spreadsheet.getRange("H6").getValues();
spreadsheet.getRange("I6").setValues(cellH6);
//spreadsheet.getRange("H6").activate();
};
getValues() -
https://developers.google.com/apps-script/reference/spreadsheet/range#getValues()

Checking if a file has been edited

I want to know if there is a way to check if a file has been edited. I looked for methods that can make it for me on Google Apps library, but I have found nothing about it. I don't know if I searched wrong.
Basically, I need to take a file, take a measurable data (something like size) of this file and store on a variable. Then, I need to take that measurable data again, store on another variable, and compare if there was a change. I need a boolean return.
Anyone?
You could do the following (pseudo with links to documentation):
Get the file you want to check using the DocList Class.
Get the ID of that File once you have it using File.getID()
Get the last edit timestamp using File.getLastUpdated()
Store this value in a Spreadsheet, or maybe Script or User Properties.
When you want to check to see if the File was updated, simply File.getFileById()
Repeat step 3.
Then compare the two last-edited timestamps with an operator like !=, or do more complex comparisons on the Dates if you want.
Depending on the result of step 7, return true or false.
Google's documentation is great, for all their services. You just need to read it a bit to understand what kind of power you have through scripting. Hopefully my pseudo-method helps in solving your problem!
Look at the file update date: https://developers.google.com/apps-script/reference/drive/file#getLastUpdated() and for storing data look up the storing data section in the apps script help page.
You could also use the GAT General Audit Tool http://goo.gl/hzZ2yf... which reports when files were edited , viewed and much more.

Calling functions from a spreadsheet formula

I'm starting to learn Google Apps Script and trying to complete "Your First Custom Function" tutorial. Maybe something has changed and they forgot to update the tutorial? When I type =in2mm(10) in a cell, it shows #NAME and Unknown function name popup is displayed.
So how do I call my own functions from a spreadsheet formula?
run your function just once through the editor's menu to be sure it is saved correctly then in the spreadsheet use it like this in cell B1 for example if A1 contains a numeric value :
=in2mm(A1)
or if you prefer, just like in your example : =in2mm(10) and it will show 254 ;-)
I solve it by deleting "inNum" in the function claiming part "function in2mm(inNum)", running it without any parameter in the spreadsheet once and getting THINKING... and ERROR, and retyping "inNum" in the function claiming part "function in2mm(inNum)", running and getting THINKING and correct result. Maybe it's the encoding issue when you copy and paste from the web page directly.