尝试在Google Apps脚本中进行一些干净的编程。有人知道如何重用经常重复的方法/属性链吗?示例:
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");
这个问题对我有很多启示,但手头的任务(如上)是仅使用GAS Javascript在各种表单元素中应用Zurb Foundation样式。
有人接电话吗?
你能做一些类似的事情吗:
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"
}
然后在你的主代码中:
var lblDate = app.createLabel("Date").setId('lblDate');
applyCSS(lblDate, _zurb1);
这种方法的所有功劳都归功于James Ferreira,Google Script(Enterprise Application Essentials)的作者。
编辑(2012年9月4日)
有了新的setStyleAttributes()方法,我认为您现在可以取消applyCSS()函数,只需使用:
var lblDate = app.createLabel("Date").setId('lblDate').setStyleAttributes(_zurb1);