I am trying to do exactly what Add border format to row if condition met in Google Sheets is asking, but the accepted answer simply does not work. The original post was very clear, so here it is once more:
Excel apparently has this type of conditional formatting built-in.
I'd like to accomplish the same in Google Sheets via Google Apps Script. The following should demonstrate the before and after conditions:
Example Dataset
A B C 1 apple Macintosh 2 apple Granny Smith 3 orange Florida 4 orange Valencia 5 pear Garden 6 banana Chiquita
Resulting Example Dataset
A B C 1 apple Macintosh 2 apple Granny Smith ----------------------------------- 3 orange Florida 4 orange Valencia ----------------------------------- 5 pear Garden ----------------------------------- 6 banana Chiquita -----------------------------------
The script/answer to this question should display a bottom border under the entire row (of columns 'A', 'B' & 'C') for rows: 2, 4, 5, & 6.
The accepted answer to this original post provides the following script:
function onOpen() {
GroupMyData(); // trigger this function on sheet opening
}
function GroupMyData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet 1'); // apply to sheet name only
var rows = sheet.getRange('a1:g'); // range to apply formatting to
var numRows = rows.getNumRows(); // no. of rows in the range named above
var values = rows.getValues(); // array of values in the range named above
var testvalues = sheet.getRange('a1:a').getValues(); // array of values to be tested (1st column of the range named above)
rows.setBorder(false, false, false, false, false, false, "red", SpreadsheetApp.BorderStyle.SOLID_MEDIUM); // remove existing borders before applying rule below
//Logger.log(numRows);
for (var i = 0; i <= numRows - 1; i++) {
var n = i + 1;
//Logger.log(n);
//Logger.log(testvalues[i] > 0);
//Logger.log(testvalues[i]);
if (testvalues[i] > 0) { // test applied to array of values
sheet.getRange('a' + n + ':g' + n).setBorder(null, null, true, null, null, null, "red", SpreadsheetApp.BorderStyle.SOLID_MEDIUM); // format if true
}
}
};
That script functions like so: A border will appear along the bottom of each row where column A's cell has a number in it (and no letters), regardless of whether the number is the same as the one in the cell above it or different.
However the goal is for A to have text, and wherever consecutive text down column A changes from a repeated string to a differing string, there should be a border between the differing strings.