This guide will help you get up and running with the TypeScript Excel development environment.
Before you begin, ensure you have the following installed:
- Docker (version 20.10 or later)
- VS Code (version 1.70 or later)
- Dev Containers extension
-
Open the project in VS Code
code DevContainer
-
Reopen in Container
- VS Code should automatically detect the devcontainer configuration
- Click "Reopen in Container" when prompted
- Wait for the container to build (this may take 5-10 minutes)
-
Build the container
cd DevContainer docker build -t typescript-excel-dev .
-
Run the container
docker run -it --rm -v $(pwd):/workspaces typescript-excel-dev
Once the container is running, verify that everything is installed correctly:
# Check Node.js version
node --version
# Check TypeScript version
tsc --version
# Check npm version
npm --version
# Check if Office.js types are available
npm list @types/office-js-
Navigate to the templates directory
cd /workspaces/templates -
Choose a template
# For a basic Excel Add-in cp -r excel-addin-basic ../my-first-addin # For a function builder cp -r excel-function-builder ../my-function-builder # For a game cp -r excel-game ../my-excel-game
-
Set up the project
cd ../my-first-addin npm install -
Start development
npm run dev-server
-
Create a new directory
mkdir /workspaces/my-new-project cd /workspaces/my-new-project -
Initialize the project
npm init -y
-
Install dependencies
npm install --save-dev typescript @types/office-js office-js webpack webpack-cli ts-loader html-webpack-plugin
-
Copy configuration files
cp /workspaces/tsconfig.json . cp /workspaces/webpack.config.js . cp /workspaces/.eslintrc.js . cp /workspaces/.prettierrc . cp /workspaces/jest.config.js .
-
Create your source files
mkdir src # Create your TypeScript files in the src directory
tsconfig.json: TypeScript configurationwebpack.config.js: Webpack build configurationpackage.json: Project dependencies and scriptssrc/index.ts: Main TypeScript entry pointsrc/index.html: HTML template for the Add-in
src/: Source code directorydist/: Built/compiled files (created after build)node_modules/: Dependencies (created after npm install)
-
Start the development server
npm run dev-server
-
Make changes to your code
- Edit TypeScript files in the
src/directory - The development server will automatically reload
- Edit TypeScript files in the
-
Test your changes
npm test -
Format your code
npm run format
-
Lint your code
npm run lint
-
Build the project
npm run build
-
Verify the build
- Check the
dist/directory for compiled files - Ensure all assets are properly bundled
- Check the
-
Run unit tests
npm test -
Run tests in watch mode
npm run test:watch
-
Generate coverage report
npm run test:coverage
// Initialize Office.js
Office.onReady((info) => {
if (info.host === Office.HostType.Excel) {
// Your Excel-specific code here
console.log('Excel is ready!');
}
});// Get the selected range
await Excel.run(async (context) => {
const range = context.workbook.getSelectedRange();
range.load('values');
await context.sync();
console.log('Selected range values:', range.values);
});
// Set cell values
await Excel.run(async (context) => {
const range = context.workbook.getSelectedRange();
range.values = [['Hello, Excel!']];
await context.sync();
});try {
await Excel.run(async (context) => {
// Your Excel operations here
});
} catch (error) {
console.error('Error:', error);
// Handle the error appropriately
}- Set breakpoints in your TypeScript code
- Press F5 to start debugging
- Use the debug console to inspect variables
console.log('Debug message');
console.error('Error message');
console.warn('Warning message');// Enable Office.js debugging
Office.debug = true;
// Log Office.js events
Office.onReady((info) => {
console.log('Office.js ready:', info);
});- Check out the examples in the
examples/directory - Review the templates in the
templates/directory - Look at the Office Add-in samples
If you encounter issues:
- Check the troubleshooting guide in
docs/troubleshooting.md - Review the logs for error messages
- Verify your setup using the verification steps above
- Ask for help in the community forums
Happy coding! 🎉