1

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 https://docs.google.com/spreadsheets/d/1wfSEQtqZJ3XEPla_KRBp7v56sPV5wlciHEHA7IS0MaI/edit?usp=sharing

Thank you!

  • Are your images the sample input situations? If my understanding is correct, can you provide the sample output situation as the image? – Tanaike May 06 '22 at 00:07
  • Thank you for replying and adding more information. When I saw your sample output situation, I cannot understand the logic for obtaining from the sample input situation to the sample output situation. Can I ask you about the detail of the logic you want to achieve? – Tanaike May 06 '22 at 00:24
  • The logic is to highlight the person from the list who speak that specific language that is working on that day. – Sergiu Tihon May 06 '22 at 00:26
  • Thank you for replying. For example, when the sample input and output images, at the sample input, the cell "B2" is "Majid". At the sample output, the cell "B2" is "Mohadeseh Barikbin". I cannot understand the logic for changing these values. I apologize for my poor English skill. – Tanaike May 06 '22 at 00:32
  • Sorry, In the mean time I edit the doc, I changed the screenshot – Sergiu Tihon May 06 '22 at 00:35
  • Thank you for replying and updating your question. In your question, by checking the column "A" and the row 3 in "List" sheet, you want to change the background of cell at "Roster" sheet. Is my understanding correct? If my understanding is correct, do you want to compare the values of row 3 of "List" with the date of today? For example, in order to test the script, can you provide the sample Spreadsheet? – Tanaike May 06 '22 at 00:39
  • I want to to change the background of cell in "List" sheet. I want to check the name from 'List' sheet with 'Roster' sheet and what time is the person working, and if it's 8-5 then change background in 'List' sheet – Sergiu Tihon May 06 '22 at 00:44
  • I added the link to the file – Sergiu Tihon May 06 '22 at 00:52
  • Thank you for replying and providing the sample Spreadsheet. About `I want to to change the background of cell in "List" sheet.`, I understood that my understood sheet name was the opposite. I have one question. In your sample output image, the situation is "05 May" or "06 May"? For example, why is the background color of "Majid" cell is not changed? – Tanaike May 06 '22 at 00:54
  • Is not changing, my script is not working, that's why I'm asking for help, I would like the script change the background of cell based on shifts of person – Sergiu Tihon May 06 '22 at 00:58
  • Thank you for replying. Your provided sample output situation was the current result which is not your expected result. Is my understanding correct? – Tanaike May 06 '22 at 01:00
  • yes, that's right. I'm trying to achieve what I describe. Thank you for help – Sergiu Tihon May 06 '22 at 01:03
  • Thank you for replying. About `OFF` and `HOLIDAY`, in this case, you don't want to change the background? – Tanaike May 06 '22 at 01:07
  • No, just for 5-8 – Sergiu Tihon May 06 '22 at 01:14
  • Thank you for replying. About `No, just for 5-8`, in this case, it's `08 -- 17`? – Tanaike May 06 '22 at 01:16
  • Thank you for replying. From your reply, I proposed a sample script as an answer. Could you please confirm it? If I misunderstood your question and that was not useful, I apologize. – Tanaike May 06 '22 at 01:22

1 Answers1

1

In your situation, how about the following sample script?

Sample script:

function myFunction() {
  // 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 colors = listRange.getValues().map((r, i) => r.map((c, j) => obj[c] && obj[c] == "08 -- 17" ? "green" : null));

  // 5. Change the background color of cells.
  listRange.setBackgrounds(colors);
}
  • The flow of this script is as follows.
    1. Retrieve 2 sheets.
    2. Retrieve values.
    3. Create an object for searching names.
    4. Create an array for changing the background color of cells.
    5. Change the background color of cells.

Testing:

When this script is run to your sample Spreadsheet, the following result is obtained.

From:

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);
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @Sergiu Tihon Thank you for replying and testing it. I'm glad your issue was resolved. With your kind support, I could correctly understand your question. Thank you, too. – Tanaike May 06 '22 at 01:33
  • 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? – Sergiu Tihon May 06 '22 at 01:38
  • I'm a beginner in JS, I just started the course, for one month, this code is a bit complicated to understand for me, that's why I'm asking. You are very kind, thanks a lot for your help. – Sergiu Tihon May 06 '22 at 01:41
  • @Sergiu Tihon About your additional question, I added one more sample script. Could you please confirm it? – Tanaike May 06 '22 at 01:43
  • It's working perfectly! Once more, I want to thank you very much for your time and help! – Sergiu Tihon May 06 '22 at 01:49
  • @Sergiu Tihon Thank you for replying. I'm glad your issue was resolved. Thank you, too. – Tanaike May 06 '22 at 01:49