---
title: Excel JavaScript API
---
:::info
This demo focuses on the JavaScript API included with Excel. For reading and
writing Excel files, [other demos](/docs/demos/) cover a wide variety of use cases
:::
Office 2016 introduced a JavaScript API for interacting with the application.
It offers solutions for custom functions as well as task panes.
Excel currently does not provide support for working with Apple Numbers files
and some legacy file formats.  SheetJS fills the gap.
This demo creates a new custom function to add much-needed functionality:
- `SHEETJS.EXTERN()` tries to fetch an external spreadsheet and insert the data
into the worksheet.
This demo focuses on the basic mechanics.  Advanced topics like Excel Custom
Function parameters are covered in the official Office JavaScript API docs.
SheetJS worksheet metadata and other properties are covered in this doc site.
## Creating a new Add-in
Initial Platform Setup (click to show)
The tool for generating Office Add-ins depends on NodeJS and various libraries.
[Install NodeJS](https://nodejs.org/) and the required dependencies:
```powershell
npm install -g yo bower generator-office
```
Creating a new Project (click to show)
Run `yo office` from the command line.  It will ask a few questions.
- "Choose a project type": "Excel Custom Functions Add-in project"
- "Choose a script type": "JavaScript",
- "What do you want to name your add-in?": "SheetJSImport"
You will see a screen like
```
? Choose a project type: Excel Custom Functions Add-in project
? Choose a script type: JavaScript
? What do you want to name your add-in? SheetJSImport
----------------------------------------------------------------------------------
      Creating SheetJSImport add-in for Excel using JavaScript and Excel-functions
at C:\Users\SheetJS\Documents\SheetJSImport
----------------------------------------------------------------------------------
```
It helpfully prints out the next steps:
```powershell
cd SheetJSImport
npm run build
npm start
```
If [VSCodium](https://vscodium.com/) is installed, the folder can be opened:
```powershell
codium .
```
Excel bug related to `fetch` (click to show)
`fetch` is available to custom functions:
```js
async function extern() {
  try {
    const url = "https://sheetjs.com/pres.numbers"; // URL to download
    const res = await fetch(url); // fetch data
    const ab = await res.arrayBuffer(); // get data as an array buffer
    // DO SOMETHING WITH THE DATA HERE
  } catch(e) { return e; } // pass error back to Excel
}
```
When fetching data, functions typically receive an `ArrayBuffer` which stores
the file data.  This is readily parsed with `read`:
```js
var wb = XLSX.read(ab); // parse workbook
```
**This is how it should work**.
[There are outstanding bugs in Excel.](https://github.com/OfficeDev/office-js/issues/2186)
For the purposes of this demo, a Base64-encoded file will be used.  The
workaround involves fetching that Base64 file, getting the text, and parsing
with the [`base64` type:](../api/parse-options#input-type)
```js
async function extern() {
  try {
    const url = "https://sheetjs.com/pres.numbers.b64"; // URL to download
    const res = await fetch(url); // fetch data
    const text = await res.text(); // get data as an array buffer
    var wb = XLSX.read(text, { type: "base64" });
    // DO SOMETHING WITH THE DATA HERE
  } catch(e) { return e; } // pass error back to Excel
}
```
Base64-encoded files can be generated with PowerShell:
```powershell
[convert]::ToBase64String([System.IO.File]::ReadAllBytes((Resolve-Path "path\to\file"))) > file.b64
```