• Stars
    star
    242
  • Rank 167,048 (Top 4 %)
  • Language
    JavaScript
  • Created almost 8 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A collection of code snippets built with Script Lab

Office JS Snippets

A collection of code snippets built with Script Lab

Script Lab import gallery screenshot

To contribute:

Note: For all command line interface (CLI) commands mentioned in these instructions, you can use either Git Bash or a Node Command Prompt.

One-time tasks

  1. Fork this project into your GitHub account.
  2. Clone your fork to your development computer.
  3. Ensure that you have Node, version 6.10+, installed. (To check the version run the command node -v.)
  4. Install yarn as a global package npm install yarn --global.
  5. Be sure your CLI is in the root of the office-js-snippets repo and run yarn install. (It is similar to npm install.)
  6. Set up the original \OfficeDev\office-js-snippets as the upstream repo for your local repo by following the steps in Configuring a remote for a fork.
  7. If you'll be using Visual Studio Code as your editor, install the TSLint extension for Visual Studio Code.

Adding a new sample

For the git tasks in this procedure, the instructions assume that you're using a CLI. You are welcome to use a GUI git client. Consult the client's help to learn how to carry out the same tasks.

  1. Create a snippet using Script Lab. Ensure that the name and description are what you want to be shown publicly. Use standard TypeScript indentation. Improper indentation can cause a failure of the build that you run in a later step. See also the Style guidelines section below.

  2. Choose the Share icon, and then choose Copy to Clipboard.

  3. Paste the contents into a text editor.

  4. Near the top of the file, you will see the line api_set: {}. This needs to be changed to specify the host API version of the most recently added API that is used in your snippet. For example, if the snippet is for Excel and it uses some APIs that were introduced in Excel API 1.3, some in 1.4, and some in 1.5, then you need to specify ExcelApi 1.5 as the value of the api_set property. Put a line break and four spaces before the value and no {} characters. To continue the example, when you're done the property would look like this:

    api_set:
        ExcelApi: '1.5'
  5. Check the name and description property values, also near the top of the file, and edit as needed.

  6. Save the file somewhere outside of the office-js-snippets project. (You will move it into the project in a later step.) The file name must have a ".yaml" extension and it must be in kebab-case. For examples, see the existing *.yaml files in the subfolders of the samples folder of the project.

  7. Make sure the main branch of your fork is in sync with the main branch of the upstream \OfficeDev\office-js-snippets repo by following the steps in Syncing a fork.

  8. Create a new branch at the office-js-snippets root folder of your local repo by running the command git checkout -b {name_of_your_new_branch}. (This will create and checkout the new branch. Stay in this branch for all the remaining steps.) Each snippet should have its own branch. Suggestion: use the name of the yaml file that you created above (without the extension) as the branch name.

  9. Decide the folder where your snippet should be added. All snippet files must reside within the appropriate subfolder inside the samples folder. Within the samples folder, the structure of subfolders is as follows:

  • The base folders such as excel, word, etc. primarily represent the various host applications.
  • Within each base folder, group folders organize snippets into various categories.
  • Within each group folder, each .yaml file represents a snippet.

Note: If your snippet doesn't fit with any existing group folder, create a new group folder inside the base folder. If the existing folders in the base folder begin with numbers, such as 03-range, then your new folder should also begin with a number. Since the numbers determine the sequence of the groups in Script Lab, use a number between the numbers of the groups between which you want the new folder to appear.

  1. Open one of the .yaml files already in the group folder. If it has an order property near the top, then the snippets in the group folder are ordered in a particular sequence in Script Lab. Add an order property to the top of your .yaml file and give it a number that is between the order numbers of the snippets between which you want it to appear.
  2. Copy your .yaml file to the chosen group folder.
  3. Run yarn start. If there are no problems, the output will end with a Done!. If there are errors, review the output to check what caused the build validation to fail, and fix as needed. See Known errors and fixes for more information.

Note: The yarn start command adds an id property to the top of the file.

  1. Re-run yarn start, and fix errors, until the build succeeds.

  2. Run git status. You should see that, in addition to your new .yaml file (or possibly new folder), a playlist\{host}.yaml file (where {host} is excel, word, etc.) has also been changed. This is expected. The build tool you just ran added a reference to your new snippet to this file.

  3. Run the following two commands. The commit message should be a brief description of what the snippet demonstrates; for example, "shows how to use getWhatever method".

    git add -A
    git commit -m "{commit message}"
    
  4. Push the snippet to your fork by running:

    git push --set-upstream origin {name_of_your_new_branch}
    
  5. You now create a pull request. In your fork on GitHub, switch to your new branch.

  6. Choose New pull request.

  7. On the Open a pull request page, verify that:

  • the base fork is OfficeDev/office-js-snippets
  • the base branch is main
  • the head fork is {your-GitHub-account}/office-js-snippets
  • the "compare" branch is {name_of_your_new_branch}.
  1. The title of the pull request defaults to your commit message. Change it as needed and optionally add a comment to provide additional information about the pull request to the reviewers.
  2. All pull requests to office-js-snippets must be approved by at least one reviewer. On the right side of the page is a Reviewers section. You can optionally suggest one or more people to review the pull request. (GitHub sometimes lists one or more admins of the repo by default, but it is not consistent in doing this.) Your pull request will be reviewed even if you don't suggest anyone.
  3. Choose Create pull request. The page for your pull request will open. There will initially be a message on the page saying Some checks haven’t completed yet. An online version of the same build tool that you ran locally is testing the files again. It usually takes a few minutes.

Note: Since your pull request passed locally, it should pass the online test too. Once in a while, the online test fails when the local test passed. This is usually a bug in the online test service. If this happens, cancel the pull request, wait a few hours, and then repeat the steps for creating a pull request.

  1. The reviewers may make comments on your pull request and ask you to make changes. Make changes in Script Lab and then repeat the process of creating the .yaml file. You do not have to create the new branch again, but make sure it is checked out when you copy the changed .yaml file over the previous version. After you commit and push the changed version to your fork, the new version is automatically added to your existing pull request. Do not create a new pull request.
  2. When the reviewers are satisfied, your pull request will be merged to the main branch and the pull request will be closed.

Note: In a few days, the repo admins will merge your snippet into the prod branch. It will then appear in Samples area of Script Lab. (It is in the My Snippets area as soon as you create it.)

  1. Optionally, you can delete the branch you created from your fork and/or your local clone.

Known errors and fixes in the build tool

  • An error saying that name has upper-case letters or other disallowed characters is not referring to the name property in the file. It is referring to the file name itself. You'll also get this error, if the file extension is not .yaml.

Style guidelines:

Basic snippet structure is as follows:

$("#run").click(() => tryCatch(run));

async function run() {
    await Word.run(async (context) => {
        const range = context.document.getSelection();
        range.font.color = "blue";

        await context.sync();
    });
}

/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
    try {
        await callback();
    }
    catch (error) {
        OfficeHelpers.UI.notify(error);
        OfficeHelpers.Utilities.log(error);
    }
}

A few style rules to observe:

  • Use standard TypeScript indentation.
  • For each button, define a corresponding async function to be run when the button is clicked. The async function can be called "run" if there is only one button on the page -- otherwise, name it as you will.
  • Each button-click handler should invoke the tryCatch function, passing in the name of the async function to be executed when the button is clicked.
  • All HTML IDs should be all-lower-case-and-hyphenated.
  • Unless you are explicitly showing pretty UI, you don't have to do the popup notification except for one or two samples. It's a lot of HTML & JS code, and also not strictly Fabric-y (there is a more "correct" way of doing this with components).
  • Strings should be in double-quotes.
  • Don't forget the semicolons.
  • Libraries in snippets must have a specific version. Eg. [email protected].

Debugging the build script

  • The scripts for building/validating the snippets are under the config folder -- in particular, under build.ts.

Note: If debugging in Visual Studio Code, you can use "F5" to attach the debugger, but be sure to run npm run tsc before you do (and after any code change!). F5 is not set to recompile!

Join the Microsoft 365 Developer Program

Get a free sandbox, tools, and other resources you need to build solutions for the Microsoft 365 platform.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

More Repositories

1

office-ui-fabric-core

The front-end CSS framework for building experiences for Office and Microsoft 365.
SCSS
3,770
star
2

Microsoft-Teams-Samples

Welcome to the Microsoft Teams samples repository. Here you will find task-focused samples in C#, JavaScript and TypeScript to help you get started with the Microsoft Teams App!
C#
1,000
star
3

TrainingContent

Training Content used for developer.microsoft.com/office
C#
884
star
4

Office-IT-Pro-Deployment-Scripts

A collection of useful PowerShell scripts to make deploying Office 2016 and Office 365 ProPlus easier for IT Pros and administrators. If you have any feature requests or ideas for future scripts please add the idea to the issues list in this repository
Visual Basic
884
star
5

ews-java-api

A java client library to access Exchange web services. The API works against Office 365 Exchange Online as well as on premises Exchange.
Java
856
star
6

generator-office

Yeoman generator for building Microsoft Office related projects.
TypeScript
833
star
7

Office-Add-in-samples

Code samples for Office Add-in development on the Microsoft 365 platform.
JavaScript
726
star
8

script-lab

Create, run and share your code directly from Office
TypeScript
703
star
9

office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
JavaScript
674
star
10

ews-managed-api

C#
573
star
11

ui-fabric-ios

DEPRECATED Please use the new repo
Swift
572
star
12

O365-InvestigationTooling

PowerShell
563
star
13

VBA-content

All content in this repository has been migrated to a new repository (https://github.com/MicrosoftDocs/VBA-Docs). Please do not open issues or pull requests here. We are no longer monitoring this content.
PowerShell
563
star
14

teams-toolkit

Developer tools for building Teams apps
TypeScript
453
star
15

office-js-docs-pr

Microsoft Office Add-ins Documentation
PowerShell
400
star
16

office-js-docs

[ARCHIVED] Microsoft Office Add-ins API Reference Documentation
397
star
17

microsoft-teams-library-js

JavaScript library for use by Microsoft Teams apps
TypeScript
370
star
18

office-ui-fabric-js

JavaScript components for building experiences for Office and Office 365.
CSS
367
star
19

teams-toolkit-samples

Scenario-focused sample applications that help you get started with building Microsoft Teams App.
TypeScript
340
star
20

microsoft-teams-apps-company-communicator

Company Communicator app template
C#
327
star
21

Excel-Custom-Functions

Learn about custom functions in Excel.
TypeScript
319
star
22

BotBuilder-MicrosoftTeams

Microsoft Bot Builder extension library for developing bots for Microsoft Teams
C#
256
star
23

microsoft-teams-apps-requestateam

Power Platform based solution that allows users to request teams and automates team creation. NO LONGER MAINTAINED. Please use 'Provision Assist' - https://github.com/pnp/provision-assist-m365/ instead.
PowerShell
235
star
24

Office-365-SDK-for-Android

Microsoft Services SDKs for Android produced by MS Open Tech.
Java
224
star
25

script-lab-2017

[Archived. This is an older version that's no longer maintained. See https://github.com/officedev/script-lab instead]
JavaScript
219
star
26

Office-Add-in-Commands

[Moved] Sample that illustrates how to add custom commands to the Office Ribbon and Context Menus
TypeScript
206
star
27

O365-EDU-Tools

O365 Education tools and scripts repository
PowerShell
199
star
28

ui-fabric-android

This repo has been moved. See website for new url.
Kotlin
179
star
29

office-scripts-docs

Office Scripts Documentation
PowerShell
172
star
30

microsoft-teams-apps-icebreaker

Icebreaker is an open-source app for Microsoft Teams that helps the whole team get closer by pairing members up every week at random to meet for coffee, burgers, pizza, or a walk around the block.
C#
172
star
31

Project-Power-BI-Templates

Expand Project with a Power BI Template
165
star
32

Office-Addin-Scripts

A set of scripts and packages that are consumed in Office add-ins projects.
TypeScript
156
star
33

skype-docs

This repository contains the content of the Skype Web SDK documentation. To review your PR, contact [email protected]
HTML
150
star
34

microsoft-teams-apps-faqplus

FAQ Plus is a friendly Q&A bot that brings a human in the loop when it is unable to help with an answer from the knowledge base.
C#
141
star
35

office-content

Contains content from our Office Developer Portals (https://developer.microsoft.com/office) that is openly editable by the public.
137
star
36

Open-XML-Package-Editor-Power-Tool-for-Visual-Studio

This Power Tool is a Visual Studio add-in that provides an easy way to parse and edit Open Packaging Conventions files, including Word, Excel and PowerPoint documents.
C#
129
star
37

microsoft-teams-apps-champion-management

Champion Management Platform is a custom Teams app that enables organizations to onboard and maintain champions/ SME in their organization in Teams.
TypeScript
129
star
38

Project-Accelerator

An Accelerator solution for Project for the web
127
star
39

microsoft-teams-sample-complete-node

A template for building complex bots for Microsoft Teams - Node.JS version
TypeScript
127
star
40

microsoft-teams-ui-component-library

Component library enhanced experiences styled for use in Microsoft Teams custom applications.
TypeScript
127
star
41

Copilot-for-M365-Samples

Microsoft Copilot for Microsoft 365 samples
125
star
42

outlook-add-in-command-demo

[ARCHIVED] The Add-in Command Demo add-in uses the commands model for Outlook add-ins to add buttons to the ribbon.
JavaScript
124
star
43

office-js-helpers

[ARCHIVED] A collection of helpers to simplify development of Office Add-ins & Microsoft Teams Tabs
TypeScript
124
star
44

skype-web-sdk-samples

HTML
115
star
45

office-js-docs-reference

Microsoft Office JavaScript API Reference
TypeScript
112
star
46

office-custom-ui-editor

Standalone tool to edit custom UI part of Office open document file format
Rich Text Format
103
star
47

Office-Apps

Office Apps by Microsoft Open Technologies, Inc.
JavaScript
100
star
48

microsoft-teams-sample-complete-csharp

A template for building complex bots for Microsoft Teams - C# version
C#
99
star
49

Office-365-SDK-for-iOS

Microsoft Services SDKs for iOS Preview produced by MS Open Tech.
Objective-C
93
star
50

msteams-ui-components

Empowering developers to build beautiful Microsoft Teams integrations
TypeScript
91
star
51

microsoft-teams-emergency-operations-center

The Microsoft Teams Emergency Operations Center (TEOC) solution template leverages the power of the Microsoft 365 platform to centralize incident response, information sharing and field communications using powerful services like Microsoft Lists, SharePoint and more.
TypeScript
89
star
52

office-fluent-ui-command-identifiers

Office Fluent User Interface Control Identifiers
88
star
53

microsoft-teams-apps-greatideas

An “innovation challenge” system that lets employees submit and share ideas and insights, with voting and contests. An app using which employees can submit an innovative idea in a selected category to be visible to all colleagues and leadership, ideas can be voted upon and a leader board of best idea contributors can be shared. This app can also be a route to file patent ideas by anyone in an organization.
C#
84
star
54

msteams-samples-hello-world-nodejs

Microsoft Teams hello world sample app in Node.js
JavaScript
83
star
55

PnP-WOPI

Patterns and Practices repo for Office Online integration via WOPI Host
C#
83
star
56

Office-Inspectors-for-Fiddler

Microsoft Office (MAPI, WOPI, and FSSHTTP) inspectors for Fiddler
C#
83
star
57

open-xml-docs

Doc repository for the Office OpenXML documentation.
PowerShell
83
star
58

Office-Add-in-UX-Design-Patterns-Code

[ARCHIVED] Implementations in HTML and CSS of the design patterns in the repo Office-Add-in-UX-Design-Patterns
CSS
80
star
59

MCCA

Microsoft Compliance Configuration Analyzer
PowerShell
78
star
60

Office-Add-in-Fabric-UI-Sample

[ARCHIVED]
CSS
78
star
61

outlook-dev-docs

Microsoft Outlook Developer Documentation
PowerShell
76
star
62

microsoft-teams-tunnelrelay

Tunnel relay allows you to expose local services to the outside world over HTTPS
C#
72
star
63

Office-Add-in-UX-Design-Patterns

[ARCHIVED] A library of common Office Add-in design patterns
69
star
64

microsoft-teams-apps-newemployeeonboarding

New Employee Onboarding(NEO) enables your organization to connect new employees to people & culture and provide them with consistent experience and information to be productive faster. Built with Sharepoint New Employee Onboarding solution as a backend with deep integration in Teams, NEO app makes it super easy for learning teams/ Human resources to manage relevant content and process for new employees using sharepoint lists. HR teams can also configure the time for an employee to be a new hire ex. 90 days and the app will stop notifying the new hires after that window. The new hire checklist is integrated into the NEO Teams app and is used to guide the new hire through the onboarding journey. The checklist can be corporate or departmental. Additionally, new hires can also introduce themselves using the app by sharing an introduction which is automatically shared with their managers. This automation makes it easier for managers to review introductions about all new hires and share them with the relevant teams in one shot! At any time, new employees can share feedback on a task in their onboarding journey or on the overall experience using a helpful bot command. All feedback is shared with HR team through helpful notifications and can be downloaded. Moreover, HR teams can also use the app to share pulse surveys for new employees at a configurable frequency.
C#
69
star
65

office-ui-fabric-ios

[ARCHIVED] Please switch to the new Office UI Fabric for iOS: https://github.com/OfficeDev/ui-fabric-ios
Swift
67
star
66

BotBuilder-MicrosoftTeams-dotnet

BotBuilder's SDK extension for Microsoft Teams
C#
66
star
67

msteams-samples-hello-world-csharp

Microsoft Teams "Hello world" application for .NET/C#
CSS
65
star
68

Office-365-REST-API-Explorer

This project is a Windows Store app that uses the Office 365 APIs client libraries to get access tokens. The app then uses the tokens with the REST API in SharePoint to show you how to build HTTP requests that perform CRUD operations on lists, list items, and files.
C#
65
star
69

Project-Samples

This Project Online add-in demonstrates how to create a project using DotNet with CSOM
C#
64
star
70

microsoft-teams-apps-stickers

The awesome Stickers app can help your organization enhance messaging in Microsoft Teams!
C#
63
star
71

SP-AngularJS-ExpenseManager-Code-Sample

An Expense Manager scenario with a App for SharePoint with a Provider Hosted AngularJS backend.
JavaScript
63
star
72

msteams-meetings-template

Template for an app that creates Microsoft Teams meetings
TypeScript
61
star
73

O365-Windows-Start

[DEPRECATED] To see current samples that demonstrate the concepts explained here, visit https://github.com/microsoftgraph/uwp-csharp-snippets-sample and https://github.com/microsoftgraph/uwp-csharp-snippets-rest-sample
C#
60
star
74

microsoft-teams-sample-todo

Sample that shows how to adapt an existing web app into a tab app for Microsoft Teams
TypeScript
57
star
75

microsoft-teams-apps-employeetraining

Employee training app helps you manage the people side of any event smoothly. Not only coordinating teams can create a new event and provide detailed event information within to attract the right learning crowd​, but also Manage all event-related information within a channel tab to keep the participants updated with the latest progress​. Employee training app provides a dedicated space to learners and enable them to browse, discover, filter, and register for events with ease​. Users can get a view of the event that are mandatory or recommended for them. Allow users to access events they have registered for in a separate tab to help them manage their registrations with ease​. Lastly, users can receive notifications for upcoming events to execute all the planned event-management activities on time. It's a seamless event /training management app which will ease event management for all employees. ​
C#
57
star
76

O365-ASPNETMVC-Start

[DEPRECATED] To see a current sample that demonstrates the concepts explained here, visit https://github.com/microsoftgraph/aspnet-snippets-sample
C#
56
star
77

microsoft-teams-apps-incentives

Power App to incentivize and track participation in training and other adoption and change management initiatives.
PowerShell
56
star
78

microsoft-teams-sample-auth-node

Sample illustrating seamless inline authentication for Microsoft Teams apps.
Handlebars
56
star
79

Excel-IO

A utility library that makes it easy to read and write Excel workbooks using C#
C#
55
star
80

microsoft-teams-apps-bookaroom

Book-a-room bot lets you quickly find and book a meeting room on the go.
C#
54
star
81

Excel-Add-in-WoodGrove-Expense-Trends

A compelling Excel add-in that demonstrates how you can use the new JavaScript API for Excel 2016. Comes in two flavors - task pane and add-in commands.
CSS
54
star
82

Office-Add-in-NodeJS-SSO

[MOVED] The getAccessToken API in Office.js enables users who are signed into Office to get access to an AAD-protected add-in and to Microsoft Graph without needing to sign-in again. This sample is built on Node.js and express.
JavaScript
53
star
83

microsoft-teams-sample-outgoing-webhook

Samples to create "Custom Bots" to be used in Microsoft Teams
C#
52
star
84

microsoft-teams-apps-remotesupport

Search & report incidents, and connect with specialists immediately
C#
52
star
85

skype-android-app-sdk-samples

This repo contains Android samples powered by the Microsoft Skype for Business App SDK. Samples developed using Android Studio
Java
51
star
86

PHP-App-for-SharePoint

Sample project to build an app for SharePoint using PHP. You can reuse the TokenHelper class in your project to get access tokens that work with apps for SharePoint. The project also includes an example page that shows how to use the TokenHelper class to contact a REST endpoint in SharePoint.
PHP
51
star
87

office-js-snippet-explorer

Simple web application for browsing, tweaking, and writing samples that use the new JavaScript APIs for Word and Excel
JavaScript
50
star
88

microsoft-teams-apps-attendance

The Attendance Power App helps teachers record and save the attendance of students in their class.
PowerShell
50
star
89

O365-WebApp-MultiTenant

[ARCHIVED] This sample shows how to build a multitenant MVC web application that uses Azure AD for sign-in using the OpenID Connect protocol, and then calls a Office 365 API under the signed-in user's identity using tokens obtained via OAuth 2.0. This sample uses the OpenID Connect ASP.Net OWIN middleware and ADAL .Net.
C#
50
star
90

office-scripts-docs-reference

Office Scripts API Reference Documentation
TypeScript
49
star
91

Outlook-Add-in-SSO

[MOVED] The sample implements an Outlook add-in that uses Office's SSO system to get access to Microsoft Graph APIs and adds buttons to the Outlook ribbon.
JavaScript
49
star
92

microsoft-teams-faqplusplus-app

DEPRECATED - This repository contains a deprecated version of the FAQ Plus app template. Please see the README file for more details and a link to the new repository
C#
48
star
93

Office-Addin-TaskPane-JS

Template to get start started writing a TaskPane Office Add-in using JavaScript.
JavaScript
48
star
94

SharePoint-Add-in-REST-OData-BasicDataOperations

Use the SharePoint REST/OData endpoints to perform create, read, update, and delete operations on lists and list items from a SharePoint Add-in.
C#
48
star
95

SharePoint-Power-Hour-Code-Samples

[ARCHIVED] All the code samples shown in the SharePoint Power Hour including Windows 8 and ASP.NET MVC Office 365 API demos
JavaScript
47
star
96

MS-Graph-Data-Connect

One stop shop for developers using Microsoft Graph data connect
C#
46
star
97

skype-ios-app-sdk-samples

This repository contains Objective C and Swift samples powered by the Microsoft Skype for Business App SDK.
Swift
46
star
98

Office-365-SDK-for-Java

Office 365 client for Java
Java
46
star
99

microsoft-data-visualization-library

TypeScript
46
star
100

Office-Addin-TaskPane

Template to get started writing a TaskPane Office Add-in.
TypeScript
46
star