convertxlsform writes the paper
form into a reference Word document, the template, and uses the
paragraph styles it defines. Pass your own template with the
template option (--template in the terminal)
to change the spacing, indents, shading and borders of the form to match
your organization’s house style. This vignette shows where the default
template lives, which styles it defines, how to change them in Word,
LibreOffice or R, and which options always win over the template. All
files are written to tempdir().
The package ships one template. It is A4 with one-inch margins and Arial 11 point text.
library(convertxlsform)
default_template <- system.file("templates", "convertxlsform.docx",
package = "convertxlsform")
basename(default_template)
#> [1] "convertxlsform.docx"The template is built by the script dev/make_template.R
in the source repository. Read it to see every value the default styles
use.
Start your own template from a copy of the default one, so that every style the package looks for is already there.
Each part of the paper form is written with one paragraph style. The style names are the names shown in Word and LibreOffice.
| Style | Used for |
|---|---|
Title |
The form title at the top of the first page. |
heading 1, heading 2,
heading 3 |
Group headings, by nesting level. Deeper groups use
heading 3. |
cxf Question |
The question number and label. |
cxf Hint |
Hints, the form details under the title, and the legend. |
cxf Logic |
Skip logic (“Ask if”), constraints and “Go to” lines. |
cxf Note |
Notes (questions of type note). |
cxf Banner |
The “For each” banner at the start of a repeat. |
Normal |
The header and footer, and any style that is missing. |
The answer options, answer lines and grids are Word tables, so they
do not take a paragraph style. The template also defines
cxf Choice, which the current version does not use.
If your template lacks one of these styles, that part of the form
uses Normal with the package’s built-in spacing, so a
template made from scratch still works. Keep the style names exactly as
written above, including spaces and capital letters.
convertxlsform sets the font and size of all text directly, from the
font_family and font_size options, and sets
the colour of headings, hints, skip logic and the repeat banner. Those
settings win over the template. What the template controls is the
paragraph:
So to change the font of the whole form, use
font_family. To put a light rule above each question and
give the repeat banner a different background, change the
cxf Question and cxf Banner styles.
In Microsoft Word:
cxf Question, and
choose Modify..docx).In LibreOffice Writer:
.docx) format.Do not rename the styles, and delete the sample text if you add any: the body of the template is not copied into the form.
The officer package, which convertxlsform uses to write Word files, can change the styles too. The code below gives each question a thin grey rule above it and more space, makes question labels dark blue, and gives the repeat banner a pale yellow background.
library(officer)
tpl <- read_docx(my_template)
tpl <- docx_set_paragraph_style(
tpl, style_id = "cxfQuestion", style_name = "cxf Question",
base_on = "Normal",
fp_p = fp_par(keep_with_next = TRUE, padding.top = 12, padding.bottom = 4,
border.top = fp_border(color = "#BFBFBF", width = 0.5)),
fp_t = fp_text_lite(color = "#00205B")
)
tpl <- docx_set_paragraph_style(
tpl, style_id = "cxfBanner", style_name = "cxf Banner",
base_on = "Normal",
fp_p = fp_par(shading.color = "#FFF2CC", keep_with_next = TRUE,
padding.top = 6, padding.bottom = 4),
fp_t = fp_text_lite(bold = TRUE)
)
print(tpl, target = my_template)Check that the styles are there with styles_info().
Pass the template with template. The example form ships
with the package.
path <- system.file("extdata", "all_types", "all_types.xlsx",
package = "convertxlsform")
docx <- convertxlsform(path,
output = file.path(tempdir(), "all_types_custom.docx"),
template = my_template, overwrite = TRUE,
verbose = FALSE)
basename(docx)
#> [1] "all_types_custom.docx"
file.exists(docx)
#> [1] TRUEFrom the terminal, the same conversion is:
These options are applied to every document, whatever the template says:
| Option | Terminal | What it sets |
|---|---|---|
font_family |
--font |
The font of all text (default Arial). |
font_size |
--font-size |
The base size of all text, in points (default 11). |
font_family_cs |
--font-cs |
The font for complex scripts such as Arabic or Bengali. |
page_size |
--page-size |
A4 or letter (default A4). |
orientation |
Portrait or landscape (default portrait). | |
margins_in |
--margins |
All four page margins, in inches (default 1). |
For example, a letter-size form in Calibri 10 point with narrow margins, keeping the paragraph styles of your template:
?convertxlsform describes every option.