Calling functions from a spreadsheet formula - google-apps-script

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.

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.

Macro Design - Calling Function

I have a question about the Macro Design in a table for a "After Update" function. In one database that one of my past employees built the "SetField" value as a function call. One of the functions it calls is called GetUserNAme() it is buried in another bas_AuditLog macro, but in a DB that I am building, it doesn't work, even though I thought I had all of the information copied and correct. I have attached an image here that might show my issue. Notice the red exclamation mark.
Image of Macro Builder with error
Perhaps one of you smart people can help me look at an area that in my DB that may have the missing link.
Thanks.
Ok, so in the other (working) applcaiton, there is a going to be a public function called GetUserName(), and it is a VBA function.
So, in that working applcation, you can hit ctrl-g (get to debug window), and then type in GetUserName and then hit shift f2. Your code editor should now jump to that VBA function. You need to copy that code to your new applcation (place it in a plane jane standard code module (not a forms module, and not a class module). and it might very well also use a api call.
So, when you copy over that code. Test and make sure the VBA code works. In most cases in the access debug window, you can type in this:
? GetUserName()
And it should spit out the current windows user name. (or whatever the code supposed to do). So, get the VBA function working, and once you do, then your data macro should now also work.

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.

Refresh Button / Auto Update GOOGLE SPREADSHEET

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! :)

Organizing Spreadsheet Code in several *.gs files - even possible?

I am trying to organize my code for a Spreadsheet in several script files. Within the script editor I can create as many *.gs files as I want, but I can't figure out how to access code that would be defined in another script.
Simple Example of what I'd like do achieve:
Code.gs:
function onEdit(){
myFunctionFromLibrary_gs();
}
Library.gs:
function myFunctionFromLibrary_gs(){
Browser.msgBox("hi there");
}
The onEdit() is obviously called by a Trigger.
Without modification this will result in a Runtime-Error, stating that
myFunctionFromLibrary_gs TypeError: is not a function, it is undefined.
So how can I make this work, or is this currently not supported?
Thx in advance for your help.
Yes, it's possible.
You are not limited to a single server Code.gs file. You can spread server code across multiple files for ease of development. All of the server files are loaded into the same global namespace, so use JavaScript classes when you want to provide safe encapsulation.
Reference: Google Documentation - features and limitations
I don't know what the _gs suffix means for Google, but without it (see code bellow), the code works.
file1.gs:
function onEdit(){
myFunctionFromLibrary();
}
file2.gs
function myFunctionFromLibrary(){
Browser.msgBox("hi there");
}
I know this is an old question but I found it looking for a similar task and happened to find the answer during my same search.
From the docs at https://developers.google.com/apps-script/guide_libraries#writingLibrary:
If you want one or more methods of your script to not be visible (nor usable) to your library users, you can end the name of the method with an underscore. For example, myPrivateMethod_().
While your function does not END in an underscore, it may have special meaning in other places than just this, or the _gs suffix may also have special meaning (particularly given the same filename suffix).