### Install office-addin-dev-certs Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/office-addin-dev-certs/README.md Installs the office-addin-dev-certs package using npm. ```bash npm install office-addin-dev-certs ``` -------------------------------- ### Install office-addin-lint Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-office-addins/README.md Install the office-addin-lint package as a development dependency. ```bash $ npm i office-addin-lint --save-dev ``` -------------------------------- ### Install Dependencies Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/CONTRIBUTE.md Installs all necessary project dependencies. Run this after cloning the repository. ```bash npm i ``` -------------------------------- ### Install ESLint Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/README.md Install ESLint as a development dependency for your project. ```bash $ npm i eslint --save-dev ``` -------------------------------- ### Install eslint-plugin-office-addins Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-office-addins/README.md Install the main eslint-plugin-office-addins package as a development dependency. ```bash $ npm install eslint-plugin-office-addins --save-dev ``` -------------------------------- ### Install Office-Addin-Lint Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/office-addin-lint/README.md Install the office-addin-lint and office-addin-prettier-config packages as development dependencies. ```bash npm install -D office-addin-lint npm install -D office-addin-prettier-config ``` -------------------------------- ### Build Project Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/CONTRIBUTE.md Compiles the TypeScript code into JavaScript. Run this after installing dependencies. ```bash npm run build ``` -------------------------------- ### Clone the Repository Source: https://github.com/officedev/office-addin-scripts/blob/master/README.md Clone the Office-Addin-Scripts repository to your local machine to start contributing. ```git git clone https://github.com/OfficeDev/Office-Addin-Scripts ``` -------------------------------- ### Install eslint-plugin-excel-custom-functions Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/README.md Install the custom ESLint plugin for Excel custom functions as a development dependency. ```bash $ npm install eslint-plugin-excel-custom-functions --save-dev ``` -------------------------------- ### Mocking PowerPoint API with Mocha Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/office-addin-mock/README.md Illustrates mocking the PowerPoint JavaScript API using OfficeMockObject for unit testing with Mocha. This example tests a function that uses `setSelectedDataAsync`. ```javascript import { OfficeMockObject } from "office-addin-mock"; async function run() { const options = { coercionType: Office.CoercionType.Text }; await Office.context.document.setSelectedDataAsync(" ", options); await Office.context.document.setSelectedDataAsync("Hello World!", options); } const PowerPointMockData = { context: { document: { setSelectedDataAsync: function (data, options?) { this.data = data; this.options = options; }, }, }, CoercionType: { Text: {}, }, }; describe(`PowerPoint`, function () { it("Run", async function () { const officeMock = new OfficeMockObject(PowerPointMockData); global.Office = officeMock; await run(); assert.strictEqual(officeMock.context.document.data, "Hello World!"); }); }); ``` -------------------------------- ### Incorrect: Adding Multiple Tables in Custom Function Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/docs/rules/no-office-read-calls.md This example demonstrates an incorrect pattern of adding multiple tables, which involves read operations and can lead to performance issues within custom functions. ```javascript /** * Custom Function for Testing * @customfunction */ function myCustomFunction() { Excel.run((context) => { var currentWorksheet = context.workbook.worksheets.getActiveWorksheet(); var expensesTable = currentWorksheet.tables.add("A1:D1", true /*hasHeaders*/); expensesTable = currentWorksheet.tables.add("A2:D2", true /*hasHeaders*/); return context.sync(); }); } ``` -------------------------------- ### Mocking Excel API with Mocha Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/office-addin-mock/README.md Demonstrates how to mock the Excel JavaScript API using the OfficeMockObject for unit testing with Mocha. This example focuses on testing a function that interacts with the selected range and its formatting. ```javascript import { OfficeMockObject } from "office-addin-mock"; function run() { try { await Excel.run(async (context) => { const range: Excel.Range = context.workbook.getSelectedRange(); range.load("address"); // Update the cell color range.format.fill.color = "yellow"; await context.sync(); console.log(`The range address was ${range.address}.`); }); } catch (error) { console.error(error); } } const MockData = { context: { workbook: { range: { address: "G4", format: { fill: {}, }, }, getSelectedRange: function () { return this.range; }, }, }, run: async function(callback) { await callback(this.context); }, }; describe(`Run`, function () { it("Excel", async function () { const excelMock = new OfficeMockObject(MockData) as any; global.Excel = excelMock; await run(); assert.strictEqual(excelMock.context.workbook.range.format.fill.color, "yellow"); }); }); ``` -------------------------------- ### Correct: Creating a New Workbook in Custom Function Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/docs/rules/no-office-read-calls.md This is a correct example of a custom function that creates a new workbook, which does not involve reading from the existing workbook's state. ```javascript /** * Custom Function for Testing * @customfunction */ function myCustomFunction() { Excel.createWorkbook(undefined); } ``` -------------------------------- ### Correct: Console Log in Custom Function Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/docs/rules/no-office-read-calls.md This example shows a correct custom function that performs a simple console log, avoiding any Office.js API read operations. ```javascript /** * Custom Function for Testing * @customfunction */ function myCustomFunction() { console.log("Hello World!"); } function readOperations() { Excel.run(function (context) { var currentWorksheet = context.workbook.worksheets.getActiveWorksheet(); var expensesTable = currentWorksheet.tables.getItemAt(0); var expenseValues = expensesTable.getHeaderRowRange().values; return context.sync(); }); } ``` -------------------------------- ### Incorrect: Accessing Active Worksheet in Custom Function Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/docs/rules/no-office-read-calls.md This example shows an incorrect usage where the active worksheet is accessed, which is a read operation and should be avoided in custom functions. ```javascript /** * Custom Function for Testing * @customfunction */ function myCustomFunction() { let context = new Excel.RequestContext(); context.workbook.worksheets.getActiveWorksheet(); } ``` -------------------------------- ### Navigate to Project Directory Source: https://github.com/officedev/office-addin-scripts/blob/master/README.md Change your current directory to the cloned Office-Addin-Scripts folder. ```git cd Office-Addin-Scripts ``` -------------------------------- ### API Usage for HTTPS Server Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/office-addin-dev-certs/README.md Demonstrates how to use the office-addin-dev-certs API to create an HTTPS server in Node.js. Ensure the 'https' and 'office-addin-dev-certs' modules are required. ```javascript var https = require('https') var devCerts = require("office-addin-dev-certs"); var options = await devCerts.getHttpsServerOptions(); var server = https.createServer(options, function (req, res) { res.end('This is servered over HTTPS') }) server.listen(443, function () { console.log('The server is running on https://localhost:443') }) ``` -------------------------------- ### Configure ESLint Extended Configurations Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-office-addins/README.md Extend ESLint with recommended configurations provided by the office-addins plugin. ```json { "extended": [ "plugin:office-addins/recommended" ] } ``` -------------------------------- ### Run Tests Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/CONTRIBUTE.md Executes the test suite to ensure code quality and functionality. Run this after building the project. ```bash npm run test ``` -------------------------------- ### Configure Plugin with Multiple Input Files Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/custom-functions-metadata-plugin/README.md Configure the plugin to handle multiple JavaScript files as input for custom function metadata. ```javascript new CustomFunctionsMetadataPlugin({ input: [ "./src/functions/someFunctions.js", "./src/functions/otherFunctions.js" ], output: "functions.json" }), ``` -------------------------------- ### Create Azure AD Application using `az rest` Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/office-addin-sso/src/scripts/azRestAppCreateCmd.txt Use this command to create a new Azure AD application registration. It configures the application for single-page applications (SPAs) with specific redirect URIs and defines API permissions for accessing Microsoft Graph and other resources. ```bash az rest -m post -u https://graph.microsoft.com/beta/applications --headers "Content-Type=application/json" --body "{\"displayName\": \"{SSO-AppName}\", \"signInAudience\": \"AzureADMultipleOrgs\", \"spa\": {\"redirectUris\": [\"https:\/\/localhost:{PORT}\/fallbackauthdialog.html\", \"https:\/\/localhost:{PORT}\/taskpane.html\"]}, \"web\": {\"implicitGrantSettings\": { \"enableIdTokenIssuance\": true, \"enableAccessTokenIssuance\": true}}, \"api\": {\"requestedAccessTokenVersion\": 2,\"acceptMappedClaims\": null, \"knownClientApplications\": [],\"oauth2PermissionScopes\": [{\"adminConsentDescription\": \"Enable Office to call the add-in's web APIs with the same rights as the current user.\", \"adminConsentDisplayName\": \"Office can act as the user\", \"id\": \"5b3a4e4a-e55e-45ba-820b-ea16efbe3d5f\",\"isEnabled\": true,\"type\": \"User\", \"userConsentDescription\": \"Enable Office to call the add-in's web APIs with the same rights that you have.\", \"userConsentDisplayName\": \"Office can act as you\", \"value\": \"access_as_user\"}], \"preAuthorizedApplications\": [{ \"appId\": \"bc59ab01-8403-45c6-8796-ac3ef710b3e3\", \"permissionIds\": [ \"5b3a4e4a-e55e-45ba-820b-ea16efbe3d5f\"]}, { \"appId\": \"57fb890c-0dab-4253-a5e0-7188c88b2bb4\", \"permissionIds\": [ \"5b3a4e4a-e55e-45ba-820b-ea16efbe3d5f\"]}, {\"appId\": \"d3590ed6-52b3-4102-aeff-aad2292ab01c\", \"permissionIds\": [\"5b3a4e4a-e55e-45ba-820b-ea16efbe3d5f\"]}, {\"appId\": \"ea5a67f6-b6f3-4338-b240-c655ddc3cc8e\", \"permissionIds\": [\"5b3a4e4a-e55e-45ba-820b-ea16efbe3d5f\"]}]},\"requiredResourceAccess\": [{\"resourceAppId\": \"00000003-0000-0000-c000-000000000000\", \"resourceAccess\": [{\"id\": \"14dad69e-099b-42c9-810b-d002981feec1\", \"type\": \"Scope\"}, {\"id\": \"7427e0e9-2fba-42fe-b0c0-848c9e6a8182\", \"type\": \"Scope\"}, {\"id\": \"37f7f235-527c-4136-accd-4a02d197296e\", \"type\": \"Scope\"}, {\"id\": \"e1fe6dd8-ba31-4d61-89e7-88639da4683d\", \"type\": \"Scope\"}]}]} ``` -------------------------------- ### Configure ESLint Plugins Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-office-addins/README.md Add 'office-addins' to the plugins section of your ESLint configuration file. ```json { "plugins": [ "office-addins" ] } ``` -------------------------------- ### Configure Scripts in package.json Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/office-addin-lint/README.md Add scripts to your package.json to enable lint checking, fixing, and prettier formatting actions. ```json "lint": "office-addin-lint check", "lint:fix": "office-addin-lint fix", "prettier": "office-addin-lint prettier" ``` -------------------------------- ### Configure Plugin with Single Input File Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/custom-functions-metadata-plugin/README.md Configure the plugin to specify the input TypeScript file and the output JSON file for custom function metadata. ```javascript plugins: [ new CustomFunctionsMetadataPlugin({ input: './src/functions/functions.ts', output: 'functions.json'}) ] ``` -------------------------------- ### Configure Prettier in package.json Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/office-addin-lint/README.md Add the prettier configuration to the top level of your package.json to enable the prettier config. ```json "prettier": "office-addin-prettier-config" ``` -------------------------------- ### Configure ESLint Rules Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/README.md Configure the 'no-office-read-calls' and 'no-office-write-calls' rules in your ESLint configuration. ```json { "rules": { "excel-custom-functions/no-office-read-calls": "warn", "excel-custom-functions/no-office-write-calls": "error" } } ``` -------------------------------- ### Configure ESLint Plugins Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/README.md Add 'excel-custom-functions' to the plugins section in your ESLint configuration file. ```json { "plugins": [ "excel-custom-functions" ] } ``` -------------------------------- ### Commit Changes Source: https://github.com/officedev/office-addin-scripts/blob/master/README.md Stage and commit your changes locally. This command will open your configured text editor to write a commit message. ```git git commit ``` -------------------------------- ### Utils File Location Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/CONTRIBUTE.md Indicates the location of utility functions used within the ESLint plugin. ```text src\rules\utils.ts ``` -------------------------------- ### Import Custom Functions Metadata Plugin Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/custom-functions-metadata-plugin/README.md Import the plugin into your Webpack configuration file. ```javascript const CustomFunctionsMetadataPlugin = require("custom-functions-metadata-plugin"); ``` -------------------------------- ### Set Sign-In Audience to Multiple Organizations Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/office-addin-sso/src/scripts/azSetSignInAudienceCmd.txt Use this Azure CLI command to update an application's manifest to allow sign-ins from any Azure AD organization. Replace `` with your add-in's object ID. ```bash az rest --method patch --uri https://graph.microsoft.com/beta/applications/ --headers "Content-Type=application/json" --body "{\"signInAudience\": \"AzureADMultipleOrgs\"}" ``` -------------------------------- ### Test Files Location Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/CONTRIBUTE.md Identifies the test files used for verifying the ESLint rules. ```text tests\rules\no-office-read-calls.test.ts tests\rules\no-office-write-calls.test.ts ``` -------------------------------- ### Add Remote for Your Fork Source: https://github.com/officedev/office-addin-scripts/blob/master/README.md Add a remote repository pointing to your GitHub fork. Replace '{username}' with your GitHub username. ```git git remote add {username} https://github.com/{username}/Office-Addin-Scripts.git ``` -------------------------------- ### Disable Office Add-in CLI Usage Data Collection Source: https://github.com/officedev/office-addin-scripts/blob/master/usage-data.md Run this command before using the tools to prevent the collection of anonymized usage data. ```bash npx office-addin-usage-data off ``` -------------------------------- ### Rule Files Location Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/CONTRIBUTE.md Identifies the TypeScript files that contain the ESLint rules for custom functions. ```text src\rules\no-office-read-calls.ts src\rules\no-office-write-calls.ts ``` -------------------------------- ### Create a New Branch Source: https://github.com/officedev/office-addin-scripts/blob/master/README.md Create a new branch for your feature or bug fix. It's recommended to use lowercase with hyphens for branch names. ```git git checkout -b my-branch-name ``` -------------------------------- ### Clear Office Add-in Cache Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/office-addin-cache/README.md Use this command to clear the Office Add-in cache. Options can be added to force-close Office applications or to enable verbose logging. ```bash office-addin-cache clear [options] ``` -------------------------------- ### Add Secret to Azure AD Application Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/office-addin-sso/src/scripts/azAddSecretCmd.txt Use this command to add a new password credential to an existing Azure AD application. Ensure you replace placeholders like '' and '' with your specific values. ```bash az rest -m post -u https://graph.microsoft.com/v1.0/applications//addPassword --headers "Content-Type=application/json" --body "{\"passwordCredential\": { \"displayName\": \"sso-secret\", \"endDateTime\": \"\"}}" ``` -------------------------------- ### Push Changes to Your Fork Source: https://github.com/officedev/office-addin-scripts/blob/master/README.md Push your committed changes to the remote branch in your GitHub fork. Replace '{username}' and '{branch-name}' accordingly. ```git git push -u {username} {branch-name} ``` -------------------------------- ### Incorrect: Office.js write operations within Excel.run Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/docs/rules/no-office-write-calls.md This snippet demonstrates an incorrect pattern where write operations (like adding a table) are performed inside an Excel.run callback within a custom function. The rule flags this to prevent unintended side effects. ```javascript /** * Custom Function for Testing * @customfunction */ function myCustomFunction() { Excel.run((context) => { var currentWorksheet = context.workbook.worksheets.getActiveWorksheet(); var expensesTable = currentWorksheet.tables.add("A1:D1", true /*hasHeaders*/); expensesTable.name = "ExpensesTable"; return context.sync(); }); } ``` -------------------------------- ### Correct: Console log and separate write operations Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/eslint-plugin-excel-custom-functions/docs/rules/no-office-write-calls.md This snippet is considered correct because the custom function only performs a console log, which is a read operation. Any write operations are contained within a separate function, `writeOperations`, which is not directly called by the custom function itself. ```javascript /** * Custom Function for Testing * @customfunction */ function myCustomFunction() { console.log("Hello World!"); } function writeOperations() { Excel.run((context) => { var currentWorksheet = context.workbook.worksheets.getActiveWorksheet(); var expensesTable = currentWorksheet.tables.add("A1:D1", true /*hasHeaders*/); expensesTable.name = "ExpensesTable"; return context.sync(); }); } ``` -------------------------------- ### Patch Azure AD Application Identifier URI Source: https://github.com/officedev/office-addin-scripts/blob/master/packages/office-addin-sso/src/scripts/azRestSetIdentifierUri.txt Use this command to update the identifier URIs for an Azure AD application. Replace `` with the application's object ID and `` with the application's client ID. ```bash az rest --method patch --uri https://graph.microsoft.com/beta/applications/ --headers "Content-Type=application/json" --body "{\"identifierUris\": [\"api://localhost:{PORT}/\"]}" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.