Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-macros

Assigning Macro/Script to a button which changes sheets


I am looking to create a button which when pressed will take me from my Master sheet to a sheet titled '01-03'. I have used the macro function to set this up and have the following script:

function myFunction() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('B8').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('01-03'), true);
}; 

The script works when I go into the script and click run, when I use the CTRL+ALT+SHIFT+1 keyboard shortcut and when I click on the Tools drop down and go into the Macros drop down and find the relevant macro. The only time it doesn't work is when I click on the button I have set up as I get the following error message:

"Script function 01-03 could not be found"

I have checked and double checked that I have named the macro correctly and I've run out of ideas as to why this may be happening. Can anyone help please?


Solution

  • Your problem is that you are trying to assign the name of the script instead of the name of the function to the button.

    To solve this, right click on your button -> assign script -> What script do you want to assign? -> myFunction (instead of 01-03 as you were doing).

    It can seem a bit confusing as the question is not asking for the function name but for the script name. Here you can find out more about assigning scripts/functions to buttons.

    I hope this has helped you, let me know if you need anything else or if you did not understood something.