I have a function which opens a Sidebar containing a message and music. This function works perfectly but i would like the function showSidebar
to run when certain cells are edited (C2:E10 on "Sheet2"). I have tried the following code but this still runs the function when an edit is made anywhere in the Spreadsheet.
function onEdit(showSidebar){
var range =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getRange(2, 3, 10, 1)
}
Full page of code here:
function onEdit(e){
var range = e.range;
var sheetName = e.source.getActiveSheet().getSheetName();
if ( // C2:E10 on "Sheet2"
sheetName == "Sheet2" &&
range.rowStart >= 2 &&
range.columnStart >= 3 &&
range.rowStart <= 10 &&
range.columnStart <= 5
) {
showSidebar();
// do something
// showSidebar(); // If you want to run the function of showSidebar(), please use this.
}
}
var SIDEBAR_TITLE = 'Dashboard Notification!';
/**
* Adds a custom menu with items to show the sidebar and dialog.
*
* @param {Object} e The event parameter for a simple onOpen trigger.
*/
function onOpen(e) {
SpreadsheetApp.getUi()
.createAddonMenu()
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
/**
* Runs when the add-on is installed; calls onOpen() to ensure menu creation and
* any other initializion work is done immediately.
*
* @param {Object} e The event parameter for a simple onInstall trigger.
*/
function onInstall(e) {
onOpen(e);
}
/**
* Opens a sidebar. The sidebar structure is described in the Sidebar.html
* project file.
*/
function showSidebar() {
var ui = HtmlService.createTemplateFromFile('Sidebar')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle(SIDEBAR_TITLE);
SpreadsheetApp.getUi().showSidebar(ui);
}
EDIT:
Having copied this script from my test area to live Sheet, the trigger no longer works when the range is edited. Can anyone shed any light on why this may happen