---
title: Google Sheets
pagination_prev: demos/cloud/index
pagination_next: demos/bigdata/index
---
import current from '/version.js';
import CodeBlock from '@theme/CodeBlock';
:::note
This demo focuses on Google Apps Script custom functions. For external data
processing, [the "Google Sheets" cloud data demo](/docs/demos/cloud/gsheet)
covers the API for NodeJS scripts
:::
The [Standalone scripts](/docs/getting-started/installation/standalone) can be
uploaded into an Apps Script project. Once uploaded, the `XLSX` variable is
available to other scripts in the project.
Google Sheets currently does not provide support for working with Apple Numbers
files and some legacy file formats. SheetJS fills the gap.
The [Complete Demo](#complete-demo) defines a `SHEETJS` function that fetches a
remote file, parses the contents, and writes data to the sheet:
![Screenshot of final result](pathname:///gsheet/udf.png)
:::note
This demo was last tested on 2023 April 17.
:::
## Integration Details
### Adding the script
The `clasp` command line tool can be used to upload the standalone script:
{`\
curl -LO https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js`}
8) Push the project to Apps Script:
```bash
npx @google/clasp push
```
:::caution
If the push command fails with an error message like
> User has not enabled the Apps Script API
The message includes a URL (`https://script.google.com/home/usersettings` when
the demo was last tested). Visit that URL and enable the Google Apps Script API.
After enabling the API, run `npx @google/clasp push` again.
:::
9) Reopen the Google Sheet and Apps Script editor (Extensions > Apps Script).
In the Files listing, there should be a new entry `xlsx.full.min.gs`
### Creating a Custom Function
10) In Apps Script editor, select `Code.gs` and erase the code in the editor.
Replace with the following function:
```js title="Code.gs"
function SHEETJS(url) {
/* fetch data */
const res = UrlFetchApp.fetch(url || "https://sheetjs.com/pres.numbers");
const content = res.getContent();
/* fix data */
for(var i = 0; i < content.length; ++i) content[i] &= 0xFF;
/* parse */
const wb = XLSX.read(content, {type: "array"});
/* generate array of arrays from worksheet */
const ws = wb.Sheets[wb.SheetNames[0]];
const aoa = XLSX.utils.sheet_to_json(ws, {header: 1});
return aoa;
}
```
Click the "Save Project" icon to save the project.
11) In the Google Sheets window, select cell A1 and enter the formula
```
=SHEETJS("https://sheetjs.com/pres.numbers")
```
The file will be fetched and the contents will be written to the sheet.