1

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

Aidan Wilson
  • 51
  • 1
  • 10
  • Possible duplicate https://stackoverflow.com/questions/49180442/auto-updating-a-date-in-a-cell-when-another-cell-is-edited-google-sheets – Francisco Araujo Aug 15 '19 at 12:20

1 Answers1

1
  • You want to run the script when the cells of "C2:E10" on "Sheet2" are edited.
  • You are using the OnEdit event trigger of the simple trigger.

If my understanding is correct, how about this sample script? Please think of this as just one of several answers.

Sample script:

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.

  }
}
  • When the values which have 3 rows are put to "E10", the values are put to the cells of "E10:E12". In this script, the function is run.
    • If you don't want to run when the values are out of range, please modify range.rowStart <= 10 && range.columnStart <= 5 to range.rowEnd <= 10 && range.columnEnd <= 5.

Reference:

If I misunderstood your question and this was not the result you want, I apologize.

Added:

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.

Sample script:

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();

  }
}
  • In this case, the function name was modified from onEdit to installableOnEdit.
    • When 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.
  • When the cells of "C2:E10" on "Sheet2" are edited, showSidebar() is run.
    • And also, when the value is deleted in the range, showSidebar() is NOT run.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Hi @Tanaike, thanks for your answer. This is not running the function at all now, i think i may have got the end part wrong. Could you clarify the section where it activates the function. Do i just write `showSidebar()` ? `) { showSidebar(); // do something // showSidebar(); // If you want to run the function of showSidebar(), please use this. } }` – Aidan Wilson Aug 15 '19 at 12:41
  • @Aidan Wilson Thank you for replying. I apologize for the inconvenience. `// do something` is your script that you want to run. If you want to run the function of `showSidebar()` if you have the function of `showSidebar()`, please put it. For this, I updated my answer. Could you please confirm it? – Tanaike Aug 15 '19 at 12:43
  • @Aidan Wilson And 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 – Tanaike Aug 15 '19 at 12:47
  • Thank you @Tanaike, that works now! However it uncovered another problem, is it possible to only run when data is added but not run if data is deleted? – Aidan Wilson Aug 15 '19 at 12:55
  • @Aidan Wilson Thank you for replying. I'm glad your issue was resolved. For your replying, when the value is deleted in the cells of "C2:E10" on "Sheet2", you don't want to run the function of `showSidebar()`. If my understanding is correct, please confirm my updated script. That script includes it. – Tanaike Aug 15 '19 at 13:06
  • @Aidan Wilson I updated my answer just now. Could you please confirm it? – Tanaike Aug 15 '19 at 13:11
  • Thank you so much @Tanaike. That works perfectly. I appreciate your help! – Aidan Wilson Aug 15 '19 at 13:13
  • @Aidan Wilson Welcome. I'm glad your issue was resolved. Thank you, too. – Tanaike Aug 15 '19 at 13:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/197988/discussion-between-aidan-wilson-and-tanaike). – Aidan Wilson Aug 15 '19 at 14:06
  • 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? I get the error 'TypeError: Cannot read property "range" from undefined. (line 10, file "Audio")' – Aidan Wilson Aug 15 '19 at 15:21
  • @Aidan Wilson Thank you for replying. Unfortunately, I cannot understand about the situation of your new question. This is due to my poor English skill. I apologize for this. And your new question is different from your initial question. So can you post it as new question by including the detail information? By this, it will users including me think of the solution. I would like to think of your new question. If you can cooperate to resolve your issue, I'm glad. – Tanaike Aug 15 '19 at 23:27
  • @Aidan Wilson When you posted it as new question, can you provide a sample Spreadsheet for replicating your issue? By this, also it will help users think of the solution. Of course, please remove your personal information. – Tanaike Aug 15 '19 at 23:49
  • Hi @Tanaike - i have posted as a new question. https://stackoverflow.com/questions/57524639/running-a-function-when-a-specific-range-is-edited-follow-up-question-2 – Aidan Wilson Aug 16 '19 at 12:27
  • @Aidan Wilson Thank you for your response. I would like to check it. When I found the solution and workaround, I would like to answer it. – Tanaike Aug 16 '19 at 23:26