I could Modify the code to the following:
var menu = [{name: "Draw Borders", functionName: "borders"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Borders", menu);
}
function borders() {
var range = SpreadsheetApp.getActive().getSheetByName('Tab_Name').getDataRange();
var cell = SpreadsheetApp.getActive().getSheetByName('Tab_Name').getRange("B50:H200")
cell.setBorder(false, false, false, false, false, false);
var values = range.getValues();
for (var i = 2; i < values.length; i++) {
for (var j = 1; j < values[i].length; j++) {
range.getCell(i + 1, j + 1).setBorder(true, true, true, true, true, true);
}
}
}
cell variable is to clear borders of cells every time the tab is changed. Problem I found when you don't set "cell" when your initial data has 100 rows for example then you edit and change the data to 40 rows, The borders will be cleared for the new data range only while the remaining range from rows 41 to 100 will still have the borders. So I set the cell value to the minimum that worked for me because I will never get data less than 50 rows. you could modify that according to your need.
Variable i is the rows. I don't want the borders to start from row 1 because I have preformatted cell which is unchangeable (title of page). Variable j is column number which I have column 1 is static as well.