2
votes

UPDATE: Solution is to simply create it in EXCEL (making sure all formulas are compatible with Sheets), then upload and convert it from xls to a sheet format. https://docs.google.com/spreadsheets/d/1TI8qkTvnanNW1IwxAYbkPISmwMLOOGu5Go0WQVBDAXQ/edit?usp=sharing

I have a timetable in Google Sheets that needs to have the borders change depending on if a class occurs during that time.

It needs to be triggered onEdit so that as the spreadsheet changes, the borders update. I will need to remove borders at the beginning of each trigger, and I have this code from another answered question:

    function RemoveBorders() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("C5:G29");

cell.setBorder(false, false, false, false, false, false, "black", SpreadsheetApp.BorderStyle.solid);
}

An example of this can be found at this link I need each class to be boxed in (and any cells not involved in a class need to retain their borders.

The conditional formatting for the background was done using this formula and it works very well.

=SUMPRODUCT(($B5>=($L$5:$L)),(C$4=($K$5:$K)),($B5<($M$5:$M)))>=1     

I have the code that works to edit borders in Excel via conditional formatting. But since I'm new to Google scripts and Java, this is really overwhelming of how to convert the concept

I have three conditional rules

Rule 1: For the first cell of a class, add borders to the left, right, and top of the cell, if the cell is in a timetable column that has a class on that day AND if the cell's time period is equal to a value in the Start Time column

=SUMPRODUCT((C$4=($K$5:$K)),($B5=($L$5:$L)))>=1

Rule 2: For the "middle cells" of a class, add borders to the left and right side, if the cell is in a timetable column that has a class on that day AND if the cell's time period (9:00-9:30) is greater or equal to Class Start time values, AND THE cell's time period is less than Class End time values.

   =SUMPRODUCT((C$4=($K$5:$K)),($B5>=($L$5:$L)),($B5<($M$5:$M)))>=1

Rule 3: Add a border to the top of the cell (for cells right after the end of a class) if the cell is in a timetable column that has a class on that day, AND if the cell's time period is in the Class End time columns.

=SUMPRODUCT((C$4=($K$5:$K)),($B5=($M$5:$M)))>=1

This is my very last step in finishing a whole dynamic data import and manipulation that dynamically updates an individual teacher's timetable. I have searched and searched and looked up so much stuff but I keep getting stumped on how to achieve this in script form. Any help would be really appreciated.

EDIT: As a complete newbie to all of this, I have just been trying to get things to work. In the process, I have stumbled upon a few other answers to questions and have hashed together this code which at least slightly delineates the classes, although is not the perfect solution

    function onEdit() {
var sheet = SpreadsheetApp.getActiveSheet();
   var range = sheet.getRange("C5:g30");
  var adress= range.getA1Notation();
  Logger.log("changed cell %s", adress);
  sheet.getRange('F2').setValue(adress);


  // Remove all borders
  var range = sheet.getRange("c5:g30");
  range.setBorder(true, true, true, true, true, true,"#D3D3D3",SpreadsheetApp.BorderStyle.SOLID);


//addtop rows
  var numRows = range.getNumRows();
  var numCols = range.getNumColumns();
  for (var i = 1; i <= numRows; i++) {
    for (var j = 1; j <= numCols; j++) {
      var currentValue = range.getCell(i,j).getValue();
      Logger.log("changed cell %s", range.getCell(i,j).getA1Notation() );
      if(currentValue != "") {
        Logger.log("positive on %s", range.getCell(i,j).getA1Notation());
        range.getCell(i,j).setBorder(true, null, null, null, null, null, "black", SpreadsheetApp.BorderStyle.SOLID_MEDIUM);
      }
    }
  }
}
1
Can you show the expected result, with images? For example if I change cell D20 this will happen(image with formatting). Because it is difficult to follow right now. - Jeff Rush
Sorry about the confusion. I have added a tab to the link shared in the original post that shows my desired formatting. Fundamentally, I need each cell to be checked for three situations. 1. Is it in the starting time of a class? (Border around left, right, and top) 2. Is it it in the “middle” time of a class (the cell is not the start or end time, but is within the time a class is occurring)? (Add left and right borders) 3. Or is in the ending time of a class? (Add left, right, and bottom borders) - Lauren
I understand you now :) Let me create you the working example. - Jeff Rush

1 Answers

0
votes

Question:

How to add borders to cells with multiple conditions?

From this:

enter image description here

To this:

enter image description here

Answer:

Test this snippet and adapt it to your needs:

function test() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange("C5:g29");
  var adress = range.getA1Notation();
  sheet.getRange('F2').setValue(adress);

  // Remove all borders
  var range = sheet.getRange("c5:g29");
  range.setBorder(true, true, true, true, true, true, "#D3D3D3", SpreadsheetApp.BorderStyle.SOLID);

  //addtop rows
  var numRows = range.getNumRows();
  var numCols = range.getNumColumns();
  for (var row = 1; row <= numRows; row++) {
    for (var column = 1; column <= numCols; column++) {

      var currentValue = range.getCell(row, column).getValue();
      // setBorder(top, left, bottom, right, vertical, horizontal, color, style)
      if ( (currentValue != "" && range.getCell(row+2, column).getValue() != "" ) || (currentValue != "" && range.getCell(row-2, column).getValue() != "" )) {
        range.getCell(row, column).setBorder(true, true, null, true, null, null, "black", SpreadsheetApp.BorderStyle.SOLID_MEDIUM);
        range.getCell(row+1, column).setBorder(false, true, true, true, null, null, "black", SpreadsheetApp.BorderStyle.SOLID_MEDIUM);
      } else if (currentValue != ""){
        range.getCell(row, column).setBorder(true, true, null, true, null, null, "black", SpreadsheetApp.BorderStyle.SOLID_MEDIUM);
        range.getCell(row+1, column).setBorder(false, true, false, true, null, null, "black", SpreadsheetApp.BorderStyle.SOLID_MEDIUM);
        range.getCell(row+2, column).setBorder(false, true, false, true, null, null, "black", SpreadsheetApp.BorderStyle.SOLID_MEDIUM);
        range.getCell(row+3, column).setBorder(false, true, true, true, null, null, "black", SpreadsheetApp.BorderStyle.SOLID_MEDIUM);
      }

    }
  }
}

Reference:

  • setBorder - Sets the border property with color and/or style. Valid values are true (on), false (off) and null (no change). For color, use Color in CSS notation (such as '#ffffff' or 'white').