Skip to content

R XLSX Functions

These functions can open and edit an Excel's XLSX file. Do not forget to call on a file before editing or reading a document. Also, do not forget to select the current worksheet with xlsx.selectWorksheet() to make sure that all other functions are executed in the correct sheet. The modification will be saved only after the call to xlsx.saveAs().


Please note that all XLSX functions are currently only available on Windows.

Complete Example

# Open the XLSX"template.xlsx"));

# Cells management
xlsx.clearCells("A2", "B4");
xlsx.mergeCells("A6", "B6");
xlsx.unmergeCells("A2", "B2");
test1 <- xlsx.getCells("C4");
test2 <- xlsx.getCells("D5:H6");
test3 <- xlsx.getCells("D5:H6", asNumber = FALSE);
xlsx.setCell("A1", "A string to put in the A1 cell");
xlsx.setName("cellnametest", 12.56);

# Document info
xlsx.setTitle("New excel title");
xlsx.setAuthor("John Doe");
xlsx.setDescription("This is a description");

# Columns & Rows
xlsx.removeRows(13, 2);
xlsx.removeColumns("M", 3);
xlsx.copyRows(4, 5, 9);
xlsx.copyColumns("G", "H", "J");
xlsx.addRows(3, 2);
xlsx.addColumns("E", 2)

# Worksheets
xlsx.addWorksheet("New worksheet");
xlsx.copyWorksheet("New worksheet", "Copy");
xlsx.moveWorksheet("Copy", 0);
xlsx.renameWorksheet("Copy", "First Worksheet");
xlsx.selectWorksheet("First Worksheet");

# Images
xlsx.insertImage(rpgm.pgmFilePath("logo.png"), "I18");

# Save


xlsx.addColumns(origin, number)

Adds a number of columns after the origin column. origin is a string representing a column (Example: A, DF).

xlsx.addRows(origin, number)

Adds a number of rows after the origin row. origin is a number.


Creates a new worksheet in the Excel spreadsheet named name.

xlsx.clearCells(start, end)

Clears the content of the cells from start to end.

xlsx.copyColumns(start, end, target)

Copies all columns from start to end to the position target.

  • start, end and target are strings representing the columns (like A or DF).
  • For copying a single column, start and end must have the same value.

xlsx.copyRows(start, end, number)

Copies all rows from start to end to the position number.

  • start, end and number are numbers representing the row.
  • For copying a single row, start and end must have the same value.

xlsx.copyWorksheet(origin, target)

Duplicates an existing worksheet named origin to a new worksheet named target.

xlsx.getCells(cells, asNumber = TRUE)

Retreives values in the cells from the opened spreadsheet.

  • cells can be a single cell like B3 or a range like C4:F9
  • asNumber will return a matrix of numbers if set to TRUE. Set to FALSE for returning a matrix of strings.


This function only returns the values of the ORIGINAL document. Opening an Excel file, setting a custom value in a cell and then getting the same cell's value will return the original value, not the one which was set afterwards.


This function is pretty slow to execute. Try to minimise the calls to this function. Prefer calling getCell() once with a bigger range value (even if some cells in the range are empty or not needed) rather than calling getCell() multiple times.

xlsx.insertImage(path, cell)

Inserts an image at the cell cell. path is the path to the image.

xlsx.mergeCells(start, end)

Merges the cells from start to end to a single cell.

xlsx.moveWorksheet(origin, position)

Moves the worksheet named origin to a new position.

  • Position is a number. The first worksheet is at the position 0. The second at 1, and so on.

Opens the filename Excel spreadsheet file for editing.


This function MUST be called before any other XLSX functions, as other functions will be executed on the last Excel spreadsheet opened.

xlsx.removeColumns(origin, number)

Deletes number columns from origin.

xlsx.removeRows(origin, number)

Deletes number rows from origin.


Deletes a worksheet names target.

xlsx.renameWorksheet(oldname, newname)

Renames an existing worksheet named oldname to newname.


Saves all the modifications made to a new file outputfile.


Selects a worksheet for other functions.

xlsx.setAuthor(author =[["user"]])

Sets the Author property of the Excel spreadsheet to author. By default the value will be[["user"]] which is the name of the user currently connected in the Operating System of the computer.

xlsx.setCell(cell, value)

Sets the cell cell to the value value.


Sets the description property to desc of the Excel spreadsheet.

xlsx.setName(name, value)

Sets the cell named name to the value value.


Sets the title property of the Excel spreadsheet to title.

xlsx.unmergeCells(start, end)

Unmerges the cells from start to end.