Search code examples
google-sheetshighlight

How can I highlight a cell when it it is changed from a referenced cell?


I have a Google sheet that is a bit complex.

  • The first tab is a Summary tab that uses vlookup to collect data from other tabs in the worksheet.
  • The other tabs are created by the IMPORTRANGE function.
  • The sources for the IMPORTRANGE tabs are edited and maintained by others.

When someone edits their sheet the IMPORTRANGE sheets get updated automatically and the Summary Tab gets update accordingly.

I need to provide a weekly summary of the changes to the data on the first (Summary) tab. I want to automatically highlights the cells on the Summary Tab when someone updates their private sheet.

I have a script that runs onEdit which updates changes when I update a cell directly on the Summary Tab but when the data is changed on the private sheet, which updates the Summary Tab the changed cell is not highlighted.

Any ideas how to accomplish the highlighting when the Summary Tab data is changed when a referenced cell is changed?

More- I have an installable trigger and a short script which should be run when any change is made:

function createSpreadsheetOpenTrigger() {
   var ss = SpreadsheetApp.getActive();
   ScriptApp.newTrigger('onEdit2')
    .forSpreadsheet(ss)
    .onChange()
    .create();
    }

function onEdit2(e)
{
  var range=e.range;
  var column=range.getColumn();
  if(column>1 && column<27)
  {
    range.setBackground('#ffff00');

  }
}

The execution fails and provide this error, "TypeError: Cannot read property 'getColumn' of undefinedat onEdit2(Code:12:20)"

Thanks in advance.


Solution

  • I believe you can do this by using an installable onChange(e) trigger instead of an onEdit, since onEdit will not fire when changes are made via script execution.

    See this answer: https://webapps.stackexchange.com/a/119702/237316


    UPDATE:

    The trigger restrictions say that neither onEdit nor onChange are executed when the edit to the bound document is one via script or API. However there is a workaround with the sheets API that would trigger the triggers. If you use ValueInputOption = USER_ENTERED with Sheets API via Apps Script. See https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption?hl=en