--- title: Spreadsheets in Dropbox sidebar_label: Dropbox pagination_prev: demos/local/index pagination_next: demos/extensions/index ---
[Dropbox](https://www.dropbox.com/) is a file hosting service that offers APIs for programmatic file access. [SheetJS](https://sheetjs.com) is a JavaScript library for reading and writing data from spreadsheets. This demo uses SheetJS to read and write spreadsheets stored on Dropbox. We'll explore two Dropbox API workflows: - A "Chooser"[^1] application allows users to select files from their Dropbox accounts. This demo will fetch and parse the selected file. - A "Saver"[^2] application allows users to save a generated spreadsheet to their Dropbox account. This demo will generate a XLS workbook using SheetJS. ## Integration Details "Dropbox Apps" are the standard way to interact with the service. The ["Dropbox App"](#dropbox-app) section describes how this demo was configured. :::info pass The Dropbox API script is loaded in this page with ```html ``` The `data-app-key` used in this demo is a "Development" key associated with the `localhost` and `docs.sheetjs.com` domains. Dropbox API does not require "Production" approval for the Chooser or Saver. ::: The live demos require a Dropbox account. ### Reading Files "Chooser" is a small library that lets users select a file from their account. `Dropbox.createChooseButton` is a function that accepts an options argument and returns a DOM element that should be added to the page: ```js var button = Dropbox.createChooseButton({ /* ... options described below ... */ }); document.appendChild(button); ``` The following options must be set: - `multiselect: false` ensures only one file can be selected - `folderselect: false` limits selection to real files - `linkType: "direct"` ensures the link points to a raw file - `success` method is called when the user selects a file The following options are optional: - `extensions: ['.xlsx', '.xls']` limits the file types for selection #### Chooser Callback The `success` callback method receives an array of File objects even if only one file is selected. This file object has the following properties: - `name` is the name of the selected file - `link` is a temporary URL that can be fetched This demo fetches the link using the `fetch` API, parses the raw data using the SheetJS `read` function[^3] and generates a HTML table using `sheet_to_html`[^4] ```js async(files) => { /* get file entry -- note that dropbox API always passes an array */ var file = files[0]; console.log(`Selected ${file.name} ID=${file.id}`); /* fetch file and parse */ var wb = XLSX.read(await (await fetch(file.link)).arrayBuffer()); /* convert first sheet to HTML table and add to page */ var html = XLSX.utils.sheet_to_html(wb.Sheets[wb.SheetNames[0]]); console.log(html); } ``` #### Chooser Live Demo :::caution pass If the live demo shows a message ``` ReferenceError: Dropbox is not defined ``` please refresh the page. This is a known bug in the documentation generator. ::: ```jsx live function SheetJSChoisissez() { const [msg, setMsg] = React.useState("Press the button to show a Chooser"); const btn = useRef(), tbl = useRef(); React.useEffect(() => { if(typeof Dropbox == "undefined") return setMsg("Dropbox is not defined"); /* create button */ var button = Dropbox.createChooseButton({ /* required settings */ multiselect: false, folderselect: false, linkType: "direct", /* optional settings */ extensions: ['.xlsx', '.xls', '.numbers'], // list of extensions /* event handlers */ cancel: () => setMsg("User Canceled Selection!"), success: async(files) => { /* get file entry -- note that dropbox API always passes an array */ var file = files[0]; setMsg(`Selected ${file.name} ID=${file.id}`); /* fetch file and parse */ var wb = XLSX.read(await (await fetch(file.link)).arrayBuffer()); /* convert first sheet to HTML table and add to page */ tbl.current.innerHTML = XLSX.utils.sheet_to_html(wb.Sheets[wb.SheetNames[0]]); } }); /* add button to page */ btn.current.appendChild(button); }, []); return ( <>{msg}