0

I want to have one-page-calendar as orgmode table, but I can't figure out how to add background colors on cells (in orgmode buffer not exported files).

Here is the calendar, I want to at least make cells which have "Sat" and "Sun" has blue background color. Would be better if the background color could be formatted almost same as that website (light gray for days, light blue for Mon/Tue/Wed/Thu/Fri, Blue for Sat/Sun).

* One page calendar 2021
  |   |    |    |    |    | Feb | Jun  | Sep | Apr  | Jan | May | Aug |
  |   |    |    |    |    | Mar |      | Dec | Jul  | Oct |     |     |
  |   |    |    |    |    | Nov |      |     |      |     |     |     |
  |---+----+----+----+----+-----+------+-----+------+-----+-----+-----|
  | 1 |  8 | 15 | 22 | 29 | Mon | Tue  | Wed | Thu  | Fri | Sat | Sun |
  | 2 |  9 | 16 | 23 | 30 | Tue | Wed  | Thu | Fri  | Sat | Sun | Mon |
  | 3 | 10 | 17 | 24 | 31 | Wed | Thu  | Fri | Sat  | Sun | Mon | Tue |
  | 4 | 11 | 18 | 25 |    | Thu | Fri  | Sat | Sun  | Mon | Tue | Wed |
  | 5 | 12 | 19 | 26 |    | Fri | Sat  | Sun | Mon  | Tue | Wed | Thu |
  | 6 | 13 | 20 | 27 |    | Sat | Sun  | Mon | Tue  | Wed | Thu | Fri |
  | 7 | 14 | 21 | 28 |    | Sun | Mon  | Tue | Wed  | Thu | Fri | Sat |

It should be able to be done similar like this one by eval elisp codes, I can't figure it out how to format it for specified strings.

3
  • I take it you want colors in the Org mode buffer - or are you talking about exporting the calendar to e.g. HTML? – NickD Jul 16 at 23:42
  • @NickD in Orgmode buffer not exported files. Seems that there are two ways to achieve this, text properties and overlays. although I'm also a bit curious what kind of approach there could be for exported HTML to have some colored cells. – Dash Eclipse Jul 17 at 15:22
0

For this simple case, and if your table is the only text in your buffer, then you could just use the search-forward function e.g.:

(defun org-table-format-cells ()
  (interactive)
  (dolist (x '("Sat" "Sun"))
    (goto-char (point-min))
    (while (search-forward x nil t)
      (let ((overlay (make-overlay (match-beginning 0)
                                   (match-end 0))))
        (overlay-put overlay 'face '(background-color . "blue"))))))

You can get a list of colors and their names with list-colors-display.

For the more general case (working in any org buffer), you could use the Org Element API:

* One page calendar 2021
#+name: item-prices
  |   |    |    |    |    | Feb | Jun  | Sep | Apr  | Jan | May | Aug |
  |   |    |    |    |    | Mar |      | Dec | Jul  | Oct |     |     |
  |   |    |    |    |    | Nov |      |     |      |     |     |     |
  |---+----+----+----+----+-----+------+-----+------+-----+-----+-----|
  | 1 |  8 | 15 | 22 | 29 | Mon | Tue  | Wed | Thu  | Fri | Sat | Sun |
  | 2 |  9 | 16 | 23 | 30 | Tue | Wed  | Thu | Fri  | Sat | Sun | Mon |
  | 3 | 10 | 17 | 24 | 31 | Wed | Thu  | Fri | Sat  | Sun | Mon | Tue |
  | 4 | 11 | 18 | 25 |    | Thu | Fri  | Sat | Sun  | Mon | Tue | Wed |
  | 5 | 12 | 19 | 26 |    | Fri | Sat  | Sun | Mon  | Tue | Wed | Thu |
  | 6 | 13 | 20 | 27 |    | Sat | Sun  | Mon | Tue  | Wed | Thu | Fri |
  | 7 | 14 | 21 | 28 |    | Sun | Mon  | Tue | Wed  | Thu | Fri | Sat |

#+begin_src emacs-lisp :results silent :var table=item-prices
  (defun org-element-example ()
    (interactive)
    (let* ((tree (org-element-parse-buffer))
           (tables (org-element-map tree 'table 'identity)))
      (org-element-map (car tables) 'table-cell
         (lambda (x)
           (when (or (string= (car (org-element-contents x)) "Sat")
                     (string= (car (org-element-contents x)) "Sun"))
               (let ((overlay (make-overlay (org-element-property :contents-begin x)
                                            (org-element-property :contents-end x))))
                 (overlay-put overlay 'face '(background-color . "blue"))))))))
  
#+end_src

;; Local Variables:
;; eval: (org-babel-execute-buffer)
;; eval: (add-hook 'org-ctrl-c-ctrl-c-hook 'org-element-example nil t)
;; End:

After saving this code in a file, then after opening the file, and with the cursor on the table, you can format the table by pressing C-c C-c (this is just to copy the style of the answer you referred to).

But generally I assume you prefer to put the emacs-lisp code in your .emacs.d file and then define a file variable by adding the following line to the beginning of your org-file:

;; -*- eval: (org-element-example) -*-

Alternatively, you probably could use org-table-map-tables directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.