I am working with different Google spreadsheets which all use the same sheets and structure. All of them do also use an identical set of functions which are executed by using custom menu items.
To maintain the functions only once for all spreadsheets, one serves as a Library for all others. Now here is the problem: All spreadsheets have a set of individual properties which need to be passed to Library function calls. That's why I am currently doing something like this:
Sample function in the library which is made available as lib
as well as the menu construction for all sheets:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Actions')
.addItem("My Library Function", "trackingHelper")
.addItem("My 2nd Library Function", "anotherFunction")
.addToUi();
}
function trackingHelper(sheetProperties) {
do something and use the sheetProperties
}
And now, in the "child" sheets I am adding something like this:
var sheetProperties = {key: value, ...}
function onOpen() {
lib.onOpen();
}
function trackingHelper() {
lib.trackingHelper(sheetProperties);
}
function anotherFunction() {
lib.anotherFunction(sheetProperties);
}
The problem is, that I always need to edit all sheets if I add new functions. That's why I'd like to do something like this with the menu in the library spreadsheet:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Actions')
.addItem("My Library Function", "lib.trackingHelper")
.addItem("My 2nd Library Function", "lib.anotherFunction")
.addToUi();
}
So I only want to add one onOpen
with this menu to all child sheets to spare the need for adding all functions individually. But how can I pass my child properties now? I tried to use the PropertiesService
but executing a script from a child sheet always yielded the properties scope of the library sheet.
Is there any possibility to avoid the need for remapping all local functions to library functions in order to pass some sheet-specific variables? Thanks a lot.
DocumentProperties
– tehhowchUserProperties
with a key of thefileId
and the appropriate config info – tehhowchgetRange().getValues()
as this approach always references the calling sheet. It's not elegant or fast but seems to work. However, maybe there is yet another way. – AdmPicard