To create a 2D array in OfficeScripts, you can use nested arrays. Here’s an example of how to create a 2D array and assign values to it: function create2DArray() { // Define the dimensions of the 2D array var numRows = 3; var numCols = 4; // Create an empty 2D array var array2D = new Array(numRows); // Loop through the rows and create arrays for each row for (var i = 0; i < numRows; i++) { array2D[i] = new Array(numCols); } // Assign values to the 2D array array2D[0][0] = "A"; array2D[0][1] = "B"; array2D[0][2] = "C"; array2D[0][3] = "D"; array2D[1][0] = 1; array2D[1][1] = 2; array2D[1][2] = 3; array2D[1][3] = 4; array2D[2][0] = true; array2D[2][1] = false; array2D[2][2] = "X"; array2D[2][3] = "Y"; // Logging the 2D array console.log(array2D); } In this example, the create2DArray function creates a 2D array with 3 rows and 4 columns. It starts by creating an empty array array2D with the specified number of rows. Next, it loops through each row and creates an array for each row using new Array(numCols). This initializes each row of the 2D array as a separate array. After creating the 2D array, the function assigns values to the individual elements of the array using the row and column indices. In this example, different types of values are assigned to demonstrate the flexibility of a 2D array. Finally, the 2D array is logged to the console. You can call the create2DArray function to see the resulting 2D array in the console output. Please note that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.
OfficeScripts Type Variables set and assign value
Here’s a sample OfficeScript that demonstrates various types of variables and how to assign data to them: function sampleVariableAssignments() { // String variable var name = "John Doe"; // Number variable var age = 30; // Boolean variable var isActive = true; // Array variable var fruits = ["Apple", "Banana", "Orange"]; // Object variable var person = { firstName: "John", lastName: "Doe", age: 30 }; // Date variable var currentDate = new Date(); // Null variable var nullValue = null; // Undefined variable var undefinedValue; // Assigning data to variables name = "Jane Smith"; age = 35; isActive = false; fruits.push("Mango"); person.age = 40; // Logging variable values console.log("Name: " + name); console.log("Age: " + age); console.log("isActive: " + isActive); console.log("Fruits: " + fruits.join(", ")); console.log("Person: " + JSON.stringify(person)); console.log("Current Date: " + currentDate); console.log("Null Value: " + nullValue); console.log("Undefined Value: " + undefinedValue); } In this sample code, we have declared variables of different types: name is a string variable assigned with a name. age is a number variable assigned with an age. isActive is a boolean variable assigned with a true/false value. fruits is an array variable assigned with a list of fruits. person is an object variable assigned with properties of a person. currentDate is a date variable assigned with the current date. nullValue is a variable assigned with a null value. undefinedValue is a variable assigned without any value, resulting in an undefined value. The code then demonstrates how to assign new values to these variables using simple assignment statements. Finally, the values of these variables are logged to the console. You can execute the sampleVariableAssignments function to see the assigned values and their corresponding types in the console output. Please note that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.
Loop through all shapes in sheet using OfficeScripts
To loop through all shapes in a sheet using OfficeScripts, you can utilize the shapes property of the Worksheet object. Here’s an example of a function that loops through all shapes in a sheet: function loopThroughShapesInSheet() { // Get the active worksheet var worksheet = context.workbook.worksheets.getActiveWorksheet(); // Load the shapes property of the worksheet var shapes = worksheet.shapes; context.load(shapes, "items/name"); // Execute the request and get the shapes return context.sync() .then(function () { var shapeNames = shapes.items.map(function (shape) { return shape.name; }); console.log("Shape Names: " + shapeNames.join(", ")); }) .catch(function (error) { console.log("Error occurred: " + error); }); } In this example, the loopThroughShapesInSheet function fetches the active worksheet using context.workbook.worksheets.getActiveWorksheet(). The shapes property of the worksheet is then loaded using worksheet.shapes, and the name property of each shape is specified to be loaded. After executing the request and synchronizing the changes with context.sync(), the function retrieves the names of all shapes by mapping over the items array of the shapes object. The shape names are then logged to the console. To use this function, you can call it as follows: loopThroughShapesInSheet() .catch(function (error) { console.log(“Error occurred: ” + error); }); Executing the loopThroughShapesInSheet function will print the names of all shapes in the active sheet to the console. Please note that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.
OfficeScripts to loop through all values(non-formulas) in range
To loop through all non-formula values in a range using OfficeScripts, you can utilize the values property of the Range object. Here’s an example of a function that loops through all non-formula values in a range: function loopThroughValuesInRange(rangeAddress) { // Get the active worksheet var worksheet = context.workbook.worksheets.getActiveWorksheet(); // Get the range object based on the provided range address var range = worksheet.getRange(rangeAddress); // Load the values property of the range var values = range.values; context.load(values); // Execute the request and get the values return context.sync() .then(function () { for (var i = 0; i < values.length; i++) { for (var j = 0; j < values[i].length; j++) { var value = values[i][j]; console.log("Value at cell (" + (i + 1) + "," + (j + 1) + "): " + value); } } }) .catch(function (error) { console.log("Error occurred: " + error); }); } In this example, the loopThroughValuesInRange function takes a rangeAddress parameter, which represents the address of the range you want to loop through. The function fetches the active worksheet using context.workbook.worksheets.getActiveWorksheet() and gets the range object based on the provided range address using worksheet.getRange(rangeAddress). The values property of the range is loaded using range.values. After executing the request and synchronizing the changes with context.sync(), the function retrieves the values from the values property. The values are stored in a two-dimensional array. The function then loops through the values array and retrieves each non-formula value. The values are logged to the console along with the corresponding cell coordinates. To use this function, you can call it as follows: loopThroughValuesInRange("A1:C3") .catch(function (error) { console.log("Error occurred: " + error); }); Executing the loopThroughValuesInRange function with the desired range address will print the non-formula values in the specified range, along with their cell coordinates, to the console. Please note that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.
OfficeScripts to loop through all formulas in range
To loop through all formulas in a range using OfficeScripts, you can utilize the formulas property of the Range object. Here’s an example of a function that loops through all formulas in a range: function loopThroughFormulasInRange(rangeAddress) { // Get the active worksheet var worksheet = context.workbook.worksheets.getActiveWorksheet(); // Get the range object based on the provided range address var range = worksheet.getRange(rangeAddress); // Load the formulas property of the range var formulas = range.formulas; context.load(formulas); // Execute the request and get the formulas return context.sync() .then(function () { var formulaArray = formulas.values; for (var i = 0; i < formulaArray.length; i++) { for (var j = 0; j < formulaArray[i].length; j++) { var formula = formulaArray[i][j]; console.log("Formula at cell (" + (i + 1) + "," + (j + 1) + "): " + formula); } } }) .catch(function (error) { console.log("Error occurred: " + error); }); } In this example, the loopThroughFormulasInRange function takes a rangeAddress parameter, which represents the address of the range you want to loop through. The function fetches the active worksheet using context.workbook.worksheets.getActiveWorksheet() and gets the range object based on the provided range address using worksheet.getRange(rangeAddress). The formulas property of the range is loaded using range.formulas. After executing the request and synchronizing the changes with context.sync(), the function retrieves the formulas from the formulas property. The formulas are stored in a two-dimensional array formulaArray, representing the formulas for each cell in the range. The function then loops through the formulaArray and retrieves each formula value. The formulas are logged to the console along with the corresponding cell coordinates. To use this function, you can call it as follows: loopThroughFormulasInRange("A1:C3") .catch(function (error) { console.log("Error occurred: " + error); }); Executing the loopThroughFormulasInRange function with the desired range address will print the formulas in the specified range, along with their cell coordinates, to the console. Please note that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.
OfficeScripts to loop through all sheets in book
To loop through all sheets in a workbook using OfficeScripts, you can utilize the worksheets property of the Workbook object. Here’s an example of a function that loops through all sheets in a workbook: function loopThroughAllSheets() { // Get the active workbook var workbook = context.workbook; // Load the worksheets property of the workbook var worksheets = workbook.worksheets; context.load(worksheets, "items/name"); // Execute the request and get the worksheets return context.sync() .then(function () { var sheetNames = worksheets.items.map(function (sheet) { return sheet.name; }); console.log("Sheet Names: " + sheetNames.join(", ")); }) .catch(function (error) { console.log("Error occurred: " + error); }); } In this example, the loopThroughAllSheets function fetches the active workbook using context.workbook. The worksheets property of the workbook is then loaded using workbook.worksheets, and the name property of each worksheet is specified to be loaded. After executing the request and synchronizing the changes with context.sync(), the function retrieves the names of all sheets by mapping over the items array of the worksheets object. The sheet names are then logged to the console. To use this function, you can call it as follows: loopThroughAllSheets() .catch(function (error) { console.log("Error occurred: " + error); }); Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo Executing the loopThroughAllSheets function will print the names of all sheets in the workbook to the console. Please note that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments. .
OfficeScripts to find first row and column with values in sheet
o find the first row and column with values in a sheet using OfficeScripts, you can utilize the UsedRange property of the Worksheet object. Here’s an example of a function that retrieves the first row and column with values in a sheet: function findFirstRowAndColumnWithValues() { // Get the active worksheet var worksheet = context.workbook.worksheets.getActiveWorksheet(); // Load the UsedRange property of the worksheet var usedRange = worksheet.getUsedRange(); context.load(usedRange, "rowIndex", "columnIndex"); // Execute the request and get the used range return context.sync() .then(function () { var firstRow = usedRange.rowIndex; var firstColumn = usedRange.columnIndex; console.log("First Row with Values: " + firstRow); console.log("First Column with Values: " + firstColumn); }) .catch(function (error) { console.log("Error occurred: " + error); }); } In this example, the findFirstRowAndColumnWithValues function fetches the active worksheet using context.workbook.worksheets.getActiveWorksheet(). The UsedRange property of the worksheet is then loaded using worksheet.getUsedRange(), and the rowIndex and columnIndex properties of the used range are specified to be loaded. After executing the request and synchronizing the changes with context.sync(), the function retrieves the first row and column with values from the usedRange object. The values are logged to the console. To use this function, you can call it as follows: findFirstRowAndColumnWithValues() .catch(function (error) { console.log("Error occurred: " + error); }); Executing the findFirstRowAndColumnWithValues function will print the first row and column with values to the console. Please note that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.
OfficeScripts to send notification to teams chat
To send a notification to a Teams chat using OfficeScripts, you can make use of the Microsoft Teams API by sending an HTTP POST request. Here’s an example of a function that sends a notification to a Teams chat: function sendTeamsNotification(message) { var url = "https://graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages"; var requestOptions = { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ body: { content: message } }) }; return ExcelScript.UrlFetch.fetch(url, requestOptions) .then(function (response) { if (response.status === 201) { console.log("Notification sent successfully"); } else { console.log("Error occurred while sending notification. Status: " + response.status); } }) .catch(function (error) { console.log("Error occurred: " + error); }); } In this example, the sendTeamsNotification function takes a message parameter, which represents the content of the notification you want to send. The function constructs the URL with the appropriate teamId and channelId for the Teams chat you want to send the notification to. The function then sends an HTTP POST request to the Microsoft Teams API using ExcelScript.UrlFetch.fetch. The request includes the message content in the request body as JSON. Once the response is received, the function checks the status code of the response. If the status code is 201 (indicating a successful response), it logs a success message. If the status code is not 201, it logs an error message with the status code. To use this function, you need to replace {teamId} and {channelId} in the URL with the appropriate values for your Teams chat. For example: sendTeamsNotification("Hello, this is a notification") .catch(function (error) { console.log("Error occurred: " + error); }); Please note that you need to have the necessary permissions and access to send notifications to the specified Teams chat. Additionally, you may need to authenticate and authorize your OfficeScripts to access the Microsoft Teams API. Keep in mind that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.
OfficeScripts function to stocks data from yahoo with link as parameter
To retrieve stock data from Yahoo Finance in OfficeScripts, you can make use of the UrlFetch class to send HTTP requests and fetch the data. Here’s an example of a function that retrieves stock data from Yahoo Finance based on a provided link: function getStockDataFromYahoo(link) { var url = "https://query1.finance.yahoo.com/v7/finance/download" + link; var requestOptions = { method: "GET" }; return ExcelScript.UrlFetch.fetch(url, requestOptions) .then(function (response) { if (response.status === 200) { var content = response.content; // Process the content as needed console.log("Stock data retrieved successfully: " + content); } else { console.log("Error occurred while retrieving stock data. Status: " + response.status); } }) .catch(function (error) { console.log("Error occurred: " + error); }); } In this example, the getStockDataFromYahoo function takes a link parameter, which represents the link to the specific stock data you want to retrieve. The function constructs the URL using the provided link and sends a GET request to Yahoo Finance using ExcelScript.UrlFetch.fetch. Once the response is received, the function checks the status code of the response. If the status code is 200 (indicating a successful response), it processes the content of the response as needed. In this example, it logs the content to the console. If the status code is not 200, it logs an error message with the status code. To use this function, you can call it with the desired link as an argument. For example: getStockDataFromYahoo("/quotes.csv?s=MSFT") .catch(function (error) { console.log("Error occurred: " + error); }); Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
OfficeScripts to list all files properties in OneDrive folder
OfficeScripts does not provide a built-in capability to directly access OneDrive or retrieve a list of files. However, you can use the Microsoft Graph API in conjunction with OfficeScripts to achieve this functionality. The Microsoft Graph API allows you to interact with OneDrive and retrieve file information. Here’s an example of how you can use OfficeScripts and the Microsoft Graph API to loop through all files in OneDrive and list their names and properties: async function listOneDriveFiles(linkOrPath) { try { // Get the access token for authentication const accessToken = await context.runtime.getAccessToken(); // Create the HTTP request to query the Microsoft Graph API const url = "https://graph.microsoft.com/v1.0/me/drive/root" + linkOrPath + "/children?$select=name,createdDateTime,modifiedDateTime,size"; const requestOptions = { method: "GET", headers: { Authorization: "Bearer " + accessToken } }; // Execute the request and get the response const response = await fetch(url, requestOptions); const data = await response.json(); // Process the file information const files = data.value; files.forEach(file => { console.log("File Name: " + file.name); console.log("Created Date: " + file.createdDateTime); console.log("Modified Date: " + file.modifiedDateTime); console.log("Size: " + file.size); console.log("————————"); }); } catch (error) { console.log("Error occurred: " + error); } } In this example, the listOneDriveFiles function takes a linkOrPath parameter, which represents the link or path of the folder within OneDrive that you want to retrieve the files from. The function uses the context.runtime.getAccessToken() method to obtain the access token for authentication with the Microsoft Graph API. The function then creates an HTTP GET request to query the Microsoft Graph API using the provided link or path. The request is authorized using the access token retrieved from OfficeScripts. The response is processed to extract the file information such as name, created date, modified date, and size. The information is then printed to the console. To use this function, you can call it with the desired link or path of the folder in OneDrive as an argument. For example: listOneDriveFiles("/Documents") .catch(function (error) { console.log("Error occurred: " + error); }); Please note that this example assumes that you have the necessary permissions and access to the OneDrive account. Additionally, you need to have the appropriate authentication and authorization set up to access the Microsoft Graph API. Keep in mind that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.