Reputation: 31
I am very new to using Google Scripts. I am having this very basic problem, but I can't seem to find an explanation. I programmed a function on a Google Sheets (opening the script editor from the same sheet in which I want to try the code). It turns out that when I want to use my function, Google Sheets doesn't find it. That means, when I type "=" plus the name of the function, I can't find it. My code is the following:
function distance(origin,destination) {
var mapObj = Maps.newDirectionFinder()
mapObj.setMode(Maps.DirectionFinder.Mode.DRIVING)
//Set the Orgin
mapObj.setOrigin(origin)
//Set the Destination
mapObj.setDestination(destination)
//Retrieve the Distance
var directions = mapObj.getDirections();
var meters = directions["routes"][0]["legs"][0]["distance"]["value"];
}
I would appreciate any help!!
Upvotes: 3
Views: 3469
Reputation: 11
Try to use:
* @customfunction
in comments before the function
Upvotes: 1
Reputation: 1770
I copied your function and it seems to be missing alright! I can't find a reference but I think "distance" is reserved in some way. If you change the name of your function to getDistance() it works fine.
/**
* Calculate the distance between origin and destination
*
* @param {Sheet3!B32} reference Cell or range to monitor.
* @returns The distance.
* @customfunction
*/
function getDistance(origin,destination) {
...
}
If you add a jsDoc comment before the function, as I have, the intellisense will work - i.e. when you type "=" in the formula bar, the contents of your comment will show:
Upvotes: 4
Reputation: 462
Not sure exactly what you mean by "sheets doesn't find it". Here are a couple of tips that might help:
You can easily add a custom menu to any doc with the following code:
function onOpen() { var ui = SpreadsheetApp.getUi(); var menu = ui.createMenu("My Script Menu"); menu.addItem("Menu Item 1", "function1").addToUi(); menu.addItem("Menu Item 2", "function2").addToUi(); ...etc. }
When you open the document, the custom menu will appear (wait a few seconds) and you can chose 'function1' from 'My Script Menu'.
Click the down arrow and then chose the function you want to run.
Upvotes: -1