Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set border color & style in spreadsheet programmatically

Google Spreadsheet has in the toolbar under the border button also a button to change the color and change the border style.

How can these be accessed within a Google Apps Script?

The setBorderColor function which is described for documents seems unavailable for spreadsheets.

like image 487
user1747567 Avatar asked Oct 15 '12 16:10

user1747567


People also ask

How do you change border color?

On the Border tab, under Color, click the color that you want to apply, and then under Border, click the specific pieces of the cell border to apply the color to. Click OK. Tip: To apply your new cell style, select the cells that you want to change, and then on the Home tab, under Format, click the style.

Which property is used to set border colors in HTML?

The border-color property is used to set the color of the four borders. The color can be set by: name - specify a color name, like "red" HEX - specify a HEX value, like "#ff0000"


3 Answers

The reported issue has been fixed, as of 12 Jan 2016. Range now has these methods:

  • setBorder(top, left, bottom, right, vertical, horizontal), as before.
  • setBorder(top, left, bottom, right, vertical, horizontal, color, style), NEW!

Examples are provided in the documentation; here's how to set a dashed red border*:

var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0];  var cell = sheet.getRange("B2"); // Sets borders on the top and bottom, but leaves the left and right unchanged // Also sets the color to "red", and the border to "DASHED". cell.setBorder(true, null, true, null, false, false, "red", SpreadsheetApp.BorderStyle.DASHED); 

*Corrected, as per comment: the documentation is wrong, it should be SpreadsheetApp.BorderStyle.DASHED/DOTTED/SOLID, not Range. – gotofritz

like image 193
Mogsdad Avatar answered Sep 21 '22 13:09

Mogsdad


Currently the setBorder() properties do not allow us to provide color and style. There is an open issue you can follow here.

like image 45
Rafael De Alemar Vidal Avatar answered Sep 19 '22 13:09

Rafael De Alemar Vidal


You can do a little trick. Copy the formatting in a coloured border cell to where ever you want.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheets()[0];
var destination = ss.getSheets()[1];

var blueBorderRange = source.getRange("B2:D4");

// This copies the formatting in B2:D4 from the source sheet to
// D4:F6 in the second sheet
blueBorderRange.copyFormatToRange(destination, 4, 6, 4, 6);
like image 37
Rhemzo Avatar answered Sep 21 '22 13:09

Rhemzo