Reputation: 51
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
Upvotes: 1
Views: 1998
Reputation: 201398
If my understanding is correct, how about this sample script? Please think of this as just one of several answers.
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
) {
// do something
showSidebar(); // If you want to run the function of showSidebar(), please use this.
}
}
range.rowStart <= 10 && range.columnStart <= 5
to range.rowEnd <= 10 && range.columnEnd <= 5
.If I misunderstood your question and this was not the result you want, I apologize.
If the function includes several methods which are required to authorize, please use the OnEdit event trigger with the installable trigger. https://developers.google.com/apps-script/guides/triggers/installable
At that time, please modify the function name as fullows.
Before you use this script, please install the function of installableOnEdit
as the installable trigger of OnEdit event trigger. You can see how to install it at here.
function installableOnEdit(e){ // Modified
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 && // Modified
"value" in e // Added
) {
showSidebar();
}
}
onEdit
to installableOnEdit
.
onEdit
is installed as the installable trigger, both the simple trigger and the installable trigger are run. By modifying the function name from onEdit
to other name, the duplicate running can be prevented.showSidebar()
is run.
showSidebar()
is NOT run.Upvotes: 1