Tag Archives: code

Stylesheets in Google Apps Script UiApp

Despite the documentation, it is quite well possible to use style sheets in a Google Apps Script UiApp.

The documentation of Google Apps Script is quite clear: “… there is no way to use custom style sheets in UiApp.”. This is mentioned in relation to the stylename methods, such as setStyleName(). However, most elements in the UiApp have the  setStyleAttributes() method. This accepts an object with style attributes, e.g.

label.setStyleAttributes({background: "black", color: "green"});

This is how you can assign style information to an element.

The essence of a style sheet is to separate the formatting from the content and the program logic. Separate style sheets make the script more readable and enable you to change the appearance and layout of all pages in a UiApp, just by editing one single file.

In Google Apps Script, you can realize this by setting the style attributes using named objects, and define these objects in a separate file. The following example shows how this works.

Continue reading

Sprintf: merge text and variables in Javascript

The function sprintf is common in many programming languages, as a way to merge programme variables in a text string. It has good options for formatting, such as outlining text string, or setting the precision of numbers. In many cases, you don’t need those, and a simple function that just inserts the variables at the right place is fine, and a better option in order to keep your JavaScript code short. See the example below:

// Simple string concatenation
var html1 = '<a href="' + url + '">' + link + '</a>';

// Using sprintf
var html2 = sprintf( '<a href="%s">%s</a>', url, link );

In this article, I share a simple function doing string replacements. For sprintf alternatives with the full functionality, just Google on ‘javascript sprintf’, and you will find some good versions. If you don’t need all that, read on.

Continue reading