Skip to main content Skip to footer

How to mimic Excel's F2 shortcut key in JavaScript

Background:

The Excel’s F2 shortcut key allows you to quickly edit a cell when a user strikes the F2 key on their keyboard. SpreadJS does not have this functionality right out of the box but can achieve this shortcut’s functionality by using the KeyDown event listener and the startEdit, and getHost methods.

Steps to Complete:

Get the host element of the current workbook with the getHost method

Add a KeyDown event listener to the host element of the current Workbook instance

Start editing the cell using the startEdit method

Getting Started:

Get the host element of the current workbook with the getHost method

We will set this to listen to the host element of the current Workbook instance by using the getHost method to get the hot element of the current Workbook instance.

var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
spread.getHost();

Then we will add a KeyDown event listener to the host element for the F2 button with keycode 113

var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
spread.getHost().addEventListener("keydown", function(e) {
 
  }

Start editing the cell using the startEdit method

var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
spread.getHost().addEventListener("keydown", function(e) {
  if (e.keyCode !== 113) { 
    return;
  }
  var sheet = spread.getActiveSheet();
  if (sheet.isEditing()) {
    return;
  }
  // start editing cell
  sheet.startEdit();
});

Mackenzie Albitz