Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9705

Code Run by Google Script Editor has Unwanted Effects on Google Sheet Conditional Formatting

$
0
0

I manage a "tasks" spreadsheet our office uses to coordinate. There are two main features that make it useful:

  1. A script run with OnEdit trigger sorts the rows alphabetically by column E, which lists priority of the task in the row (e.g., "1. High,""2. Medium,""3. Low," etc.) This effectively sorts the sheet by priority as new tasks are added.
  2. A Conditional Formatting Rule searches Column E using regex matching for "High,""Medium," or "Low" to color code Columns A and B. I originally set the range for the conditional formatting rules to be A:B.

As far as I can tell, the issue is that when the script runs it affects conditional formatting by doing two things:

  1. It changes the range of preexisting rules from A:B to something like A1:A986,B1:B24,B26:B986. These ranges are arbitrary and don't correspond to the number of rows.
  2. It adds new conditional formatting rules with extremely limited ranges like B25. The custom formula also gets changed, please see below for the direct changes.

This works for a while, but eventually the rules start to interfere with each other and the formatting gets broken and I got sick of fixing it.

Here is the script that is running:

function autoPrioritize(event){  var sheet = event.source.getActiveSheet();  var editedCell = sheet.getActiveCell();  var columnToSortBy = 5;  var tableRange = "A2:H40";  if(editedCell.getColumn() == columnToSortBy){       var range = sheet.getRange(tableRange);    range.sort( { column : columnToSortBy } );  }}

And here is the template custom formula I use for conditional formatting:=and($E1<>"",search("Med",$E1)>0). The new rules that get added for some reason have a different formula based on the numbers we use for the sorting. Instead of the above, the new formulas look like this: =and(#REF!<>"",search("2.",#REF!)>0).I was wondering if someone could help me understand why the ranges are changed and why new rules get added. I have a suspicion that these new rules appear as new tasks are added to the sheet in the form of rows. Thank you in advance for nay help or advice.Original Rule, Altered Rule, and Script Editor Added Rule in that Order.

Original rule:

Altered rule:

Rule that appears to be added by the script editor:


Viewing all articles
Browse latest Browse all 9705

Latest Images

Trending Articles



Latest Images