Question

Trying to do some clean programming in Google Apps Script. Does anyone know how to reuse oft-repeated method/property chains? Example:

var lblDate = app.createLabel("Date")
    .setId('lblDate')
    .setStyleAttribute("fontFamily", "\"Helvetica Neue\", \"HelveticaNeue\", Helvetica,   Arial, \"Lucida Grande\", sans-serif")
    .setStyleAttribute("fontSize", "14px")
    .setStyleAttribute("fontSize", "1.4rem")
    .setStyleAttribute("lineHeight", "1")
    .setStyleAttribute("color", "#222222")
    .setStyleAttribute("position", "relative");

This question has many implications for me, but the task at hand (above) is applying Zurb Foundation styles across various form elements using only GAS Javascript.

Any takers?

Était-ce utile?

La solution

Could you do something like:

function applyCSS(element, style) {
  for (var key in style) {
    element.setStyleAttribute(key, style[key]);
  }
}

var _zurb1 = 
  {
    "fontFamily": "\"Helvetica Neue\", \"HelveticaNeue\", Helvetica,   Arial, \"Lucida Grande\", sans-serif",
    "fontSize": "14px",
    "fontSize": "1.4rem",
    "lineHeight": "1",
    "color": "#222222",
    "position": "relative"
  }

And then in your main code:

var lblDate = app.createLabel("Date").setId('lblDate');
applyCSS(lblDate, _zurb1);

All credit for this method to James Ferreira, author of Google Script (Enterprise Application Essentials).


edit (4/09/2012)

With the new setStyleAttributes() method I think you can now do away with the applyCSS() function and just use:

var lblDate = app.createLabel("Date").setId('lblDate').setStyleAttributes(_zurb1);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top