Search code examples
google-apps-scriptgoogle-sheetsgoogle-apps-script-editor

"google spreadsheet" select range to fill the borders in scripteditor (simplify my code)


I have written some code to automatically fill the border lines. The problem is that i want to use a specific range of cells based upon the position of the first empty cell. I can get the code to work for a specific cell but not for a range.

please help me simplify the code

var sheet = ss.getSheetByName('Form Responses 1');

  var cell =sheet.getRange(lastRow + 4, 2);
    cell.setBorder(true, true, true, true, true, true, "black", SpreadsheetApp.BorderStyle.SOLID);
  var cell =sheet.getRange(lastRow + 4, 3); 
    cell.setBorder(true, true, true, true, true, true, "black", SpreadsheetApp.BorderStyle.SOLID);
  var cell =sheet.getRange(lastRow + 4, 4);
    cell.setBorder(true, true, true, true, true, true, "black", SpreadsheetApp.BorderStyle.SOLID);**
  var cell =sheet.getRange(lastRow + 5, 2);
    cell.setBorder(true, true, true, true, true, true, "black", SpreadsheetApp.BorderStyle.SOLID);
  var cell =sheet.getRange(lastRow + 5, 3); 
    cell.setBorder(true, true, true, true, true, true, "black", SpreadsheetApp.BorderStyle.SOLID);
  var cell =sheet.getRange(lastRow + 5, 4);

Solution

  • Thanks to tehhowch's help ended up with a solution to replace the code:

    New code:

    var cell =sheet.getRange(lastRow + 4, 2, 8, 6);