Skip to content

Instantly share code, notes, and snippets.

@alison-mk
Last active November 30, 2023 00:25
Show Gist options
  • Save alison-mk/9d66c10857477adb5e9c264c0cb0da1b to your computer and use it in GitHub Desktop.
Save alison-mk/9d66c10857477adb5e9c264c0cb0da1b to your computer and use it in GitHub Desktop.
This sample shows how to set a cell value to an error data type, and then update the value of a cell that contains an error data type.
name: 'Data types: Set and change error values'
description: >-
This sample shows how to set a cell value to an error data type, and then
update the value of a cell that contains an error data type.
host: EXCEL
api_set: {}
script:
content: |
$("#setup").click(() => tryCatch(setup));
$("#set-busy-error").click(() => tryCatch(setBusyError));
$("#change-busy-error").click(() => tryCatch(changeBusyError));
async function setBusyError() {
// This function sets the value of cell A1 to a #BUSY! error using data types.
await Excel.run(async (context) => {
// Retrieve the Sample worksheet and cell A1 on that sheet.
const sheet = context.workbook.worksheets.getItemOrNullObject("Sample");
const range = sheet.getRange("A1");
// Get the error data type and set its type to `busy`.
const error: Excel.ErrorCellValue = {
type: Excel.CellValueType.error,
errorType: Excel.ErrorCellValueType.busy
};
// Set cell A1 as the busy error.
range.valuesAsJson = [[error]];
await context.sync();
});
}
async function changeBusyError() {
// This function checks if a range contains a #BUSY! error, and then updates the range if it contains the error.
await Excel.run(async (context) => {
// Retrieve the Sample worksheet and cell A1 on that sheet.
const sheet = context.workbook.worksheets.getItemOrNullObject("Sample");
const range = sheet.getRange("A1");
// Load the `valuesAsJson` property for comparison.
range.load("valuesAsJson");
await context.sync();
// Check if the range contains a #BUSY! error.
if (range.valuesAsJson[0][0]["errorType"] == Excel.ErrorCellValueType.busy) {
// If the range contains a #BUSY! error, load and change the value of the range.
range.load("values");
await context.sync();
range.values = [["Process unavailable."]];
}
await context.sync();
});
}
async function setup() {
await Excel.run(async (context) => {
// Create a new worksheet called "Sample" and activate it.
context.workbook.worksheets.getItemOrNullObject("Sample").delete();
const sheet = context.workbook.worksheets.add("Sample");
sheet.activate();
await context.sync();
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
language: typescript
template:
content: "<section class=\"ms-font-m\">\n\t<p>This sample shows how to set the value of cell A1 to the #BUSY! error data type. The sample then checks to see if\n\t\tcell A1 contains a #BUSY! error, and updates the cell value if it does.</p>\n</section>\n<section class=\"setup ms-font-m\">\n\t<h3>Set up</h3>\n\t<button id=\"setup\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Add worksheet</span>\n </button>\n\t<h3>Try it out</h3>\n\t<button id=\"set-busy-error\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Set A1 to #BUSY! error</span>\n </button>\n\t<button id=\"change-busy-error\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Change output of cell with #BUSY! error</span>\n </button>\n</section>"
language: html
style:
content: |-
section.samples {
margin-top: 20px;
}
section.samples .ms-Button, section.setup .ms-Button {
display: block;
margin-bottom: 5px;
margin-left: 20px;
min-width: 80px;
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css
core-js@2.4.1/client/core.min.js
@types/core-js
jquery@3.1.1
@types/jquery@3.3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment