I faced the same problems. I here document, what worked for me.
To create a new custom function, I did the following:
- In spreadsheet: Menu -> Tools -> Script Editor
- Wrote the function in Script Editor
- In spreadsheet: Menu -> Tools -> Import -> Clicked "Add Function" next to the custom function name.
- In spreadsheet: In a cell, I did
=CustomFunctionName()
This worked.
Now, when I wanted to reuse the same in another spreadsheet, there are 2 ways to do that.
Make Copy
of the sheet in which the custom function is written.
- Make library of custom functions and reuse them in other sheets, by writing custom functions specific for a particular sheet.
#1 Make Copy of the sheet in which the custom function is written.
In this way, the whole custom function code gets duplicated. So, if any changes needs to be done in the custom function, the change must be done in all the copied files.
Also, once the sheet is copied, the custom function does not simply works. I had to delete the cells & reenter the function names again, to get them working.
#2 Make library of custom functions and reuse them in other sheets, by writing custom functions specific for a particular sheet.
This is better, because, the core logic will stay in one place. Only wrapper functions will be written for each sheet, in which the custom functions are used.
To create a library:
- Write the function in Script Editor
- Click on "Deploy -> New Deployment"
- "Select Type -> Library"
- Provide a description, if you want to and click "Deploy" & "Done"
- Click on the "Project Settings" on the left menu & copy the "Script Id"(to use the library)
To use a library:
- Goto the spreadsheet where you want the library to be used.
- Menu -> Tools -> Script Editor
- In Script Editor: Click
+
button next to the Library section on the left.
- Enter the "Script Id" & Click on "Look Up".
- You will see the script details. Then click on the "Add" button.
- Remember the "Identifier" field value. Using that, the functions of the library can be accessed.
- Now, in the Script Editor, you can call the library functions with the identifier name. See examples below.
Library Code:
function SheetName() {
return SpreadsheetApp.getActiveSpreadsheet().getName();
}
function SayHello(name) {
return 'Hello ${name}';
}
Let's say, while including this library into a spreadsheet, I entered the identifier as utility
. So, in the script editor, I would write like this:
function SheetName() {
return utility.SheetName();
}
How to update a library and use the updated code in spreadsheets?
To update a library:
- Make the changes in the library project.
- Click on "Deploy -> Manage Deployments".
- Select the correct deployment from the left side.
- Click on the edit button(on top left).
- Select "New Version" in the "Version" input.
- Click "Deploy" & make note of the new version number displayed.
To use the updated library:
- Goto the spreadsheet in which the library is already being used & open the Script Editor in there.
- If the Script Editor of that sheet is already open, reload the page once.
- Click on the library name in the
Library
section(on the left).
- Select the latest version number on the
Version
input & click "Save".
Now, you can call the new functions in the same way you did previously & the existing functions will work in the new way(if there are any changes to the existing functions).
.html
file instead of a.gs
file? Are you editing a script that's in a different spreadsheet? Are the custom functions you are interested in contained inside another function (thus hiding them)? Did yousave
the script? Can you rundouble()
from within the debugger? – Mogsdad