Home > Software engineering >  Google Sheet App Script: Match 1 values from one sheet to another sheet and then if condition met se
Google Sheet App Script: Match 1 values from one sheet to another sheet and then if condition met se

Time:05-06

I'm new to app script and still learning how to do thing. Here's what I want to do: I have 2 sheets, in the 1st are just the names ('List') in columns based on each person language, the 2nd one is schedule ('Roster'). I want to change cell background into green the cell with person name when person is on schedule from 8-5.

Any help or idea is very appreciated. Thank you in advance.

Here's what I tried so far to do:

function addShiftColor() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var actSheet = ss.getSheetByName('List');
  var rosSheet = ss.getSheetByName('Roster');
  var lastCol = actSheet.getLastColumn()  1;
  var lastRow = actSheet.getLastRow()  1;
  var end = rosSheet.getLastRow();
    for (var column = 1; column < lastCol; column  ) {
      for (var row = 1; row < lastRow; row  ) {
        var cellget = actSheet.getRange(row, column).getValue();
        var cellset = actSheet.getRange(row, column);
          for(i=1, j=5; i <= end, j <= end; i  ,j  ){
            var cell1 = rosSheet.getRange(i, 1).getValue();
            var cell2 = rosSheet.getRange(j, 253).getValue();
          if(cell1 === cellget && cell2 === "08 -- 17"){
          cellset.setBackground("green");
          }
      }
    }   
  }
}

2nd Sheet 'Roster' enter image description here

1st Sheet 'List' enter image description here

This is the result I want to achieve.enter image description here

Here is the link to the file enter image description here

To:

enter image description here

Note:

  • In this sample, green is used as the background color. This is from your script. If you want to change this, please modify "green".

  • This sample script is for your sample Spreadsheet. So, when the structure of the Spreadsheet is changed, this script might not be able to be used. So, please be careful abuot this.

References:

Added:

About your following additional question,

I have a question, If I would like to add as well different colors for 11-20 and 17-20, what should be changed in the script?

In this case, how about the following sample script?

Sample script:

function myFunction() {
  const colorObj = { "08 -- 17": "green", "17 -- 02": "blue", "11 -- 20": "red" }; // Please modify this for your actual situation.

  // 1. Retrieve 2 sheets.
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const listSheet = ss.getSheetByName("List");
  const rosterSheet = ss.getSheetByName("Roster");

  // 2. Retrieve values.
  const listRange = listSheet.getRange(2, 1, listSheet.getLastRow() - 1, listSheet.getLastColumn());
  const [, , dates, , ...rosterValues] = rosterSheet.getDataRange().getDisplayValues();

  // 3. Create an object for searching names.
  const today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd MMM");
  const col = dates.indexOf(today);
  if (col == -1) {
    throw new Error(`Date of ${today} was not found.`);
  }
  const obj = rosterValues.reduce((o, r) => {
    if (r[0]) o[r[0]] = r[col];
    return o;
  }, {});

  // 4. Create an array for changing the background color of cells.
  const keys = Object.keys(colorObj);
  const colors = listRange.getValues().map((r, i) => r.map((c, j) => obj[c] && keys.includes(obj[c]) ? colorObj[obj[c]] : null));

  // 5. Change the background color of cells.
  listRange.setBackgrounds(colors);
}
  • Related