google spreadsheet set row background color based on note

前端 未结 2 741
夕颜
夕颜 2021-01-07 10:10

I have a Google spreadsheet that has client contact information in it.

I am trying to find a way to highlight edits users make automatically without them having to c

2条回答
  •  不思量自难忘°
    2021-01-07 10:55

    To get the entire row, assume it starts at 1, and use the Range.getLastColumn() method to find the right-extent of the row. (If your rows are staggered, this may highlight empty cells at either end - checking for that, though, will slow down the function further.)

    function onEdit() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = SpreadsheetApp.getActiveSheet();
    
      var cell = ss.getActiveCell();
      var range = sheet.getRange(cell.getRow(),1,1,sheet.getDataRange().getLastColumn());
      var note = cell.getNote();
      var user = Session.getUser();
    
      note = user;
      cell.setNote(note);
    
      if(note = "user@email.com") {
        range.setBackgroundRGB(255, 0, 0);
      }
    };
    

    You also asked about a "better way". Here's a variation with a few 'improvements'.

    • It uses the trigger Event, rather than relying on being a container-bound script. (See Understanding Events.) That makes this more portable, sure, but it also means that you can eliminate some (slow) calls to Spreadsheet services, since you're handed information for free.

      To test this function, use the technique described in How can I test a trigger function in GAS?.

    • Your original script contained an if-then to check which user had made a change, the beginning of a code block that would grow as you added more users. This one takes a data-driven approach, with const userColors. Then, we can check whether the user is known by an in comparison, and act on that.

      You could further improve it by managing the user colors in the spreadsheet, or elsewhere - and even having it dynamically learn new users and assign them their own colors. At any rate, this allows you to easily add more user & color combinations, without needing to change the logic in the function.

    • In onEdit() functions, you should be very concerned about performance. (This example is so short, it's not really an issue, but still a good habit.) This version has a couple of changes to address that. First, there is a check to see if we need to set color at all - if the cell being edited was last edited by the same user, there's no need to do anything else. Second, a number of operations that invoke Spreadsheet Services have been moved so they only execute when needed.

    Updated script:

    function onEdit(event) {
      const userColors = {
        'user1@email.com' : 'red',
        'user2@email.com' : 'blue',
        'user3@email.com' : '#ff00ff'
      };
    
      var note = event.range.getNote();
      var user = new String(Session.getUser());
    
      // update note and color if new editor
      if (note != user) {
        var sheet = event.range.getSheet();
        var range = sheet.getRange(event.range.getRow(),1,1,sheet.getLastColumn());
    
        event.range.setNote(user);
        
        if (user in userColors) range.setBackground(userColors[user]);
        // don't color unknown users
        else range.setBackground('white');
      }
    };
    

提交回复
热议问题