Error Message in Excel Online Office Scripts: "The function namedRanges.findAsync is not recognized" occurs while executing script with Microsoft Flow

When I run a script to consolidate data in Excel Online, it works perfectly within Excel Online itself. However, when I try to execute the same script using Microsoft Flow, I encounter an error message:

The script cannot be executed. Please attempt it again. Runtime error: Line 19: namedRanges.findAsync is not a function clientRequestId: e0d62dd6-eada-4d04-b081-13e7c5400748

Below is the script being used:

function main(workbook: ExcelScript.Workbook) {
    const names = ["EVMSChart", "AC", "EV", "PV"];
    let outputRow = 0;
    let outputWs = workbook.getWorksheet("Graph Data");

    if (!outputWs) {
        outputWs = workbook.addWorksheet("Graph Data");
    } else {
        outputWs.getRange("A:BZ").clear();
    }

    outputWs.getRange("A1:E1").setValues([["Sheet Name", "EVMSChart", "AC", "EV", "PV"]]);

    workbook.getWorksheets().forEach((ws) => {
        let max = 0;
        let hasData = false;
        for (let i = 0; i < names.length; i++) {
            const namedRanges = ws.getNames();
            const namedRange = namedRanges.find(nr => nr.getName() === names[i]);//Line 19
            if (namedRange) {
                const range = namedRange.getRange();
                const data = range.getValues();
                let outputData: (string[][]) = [];
                for (let j = 0; j < data.length; j++) {
                    const row = data[j];
                    for (let k = 0; k < row.length; k++) {
                        const value = row[k].toString();
                        outputData.push([value]);
                        max = Math.max(max, k);
                    }
                }
                if (outputData.length > 0) {
                    outputWs.getRangeByIndexes(outputRow + 1, i + 1, outputData.length, 1).setValues(outputData);
                    hasData = true;
                }
            }
        }
        if (hasData) {
            outputWs.getRangeByIndexes(outputRow + 1, 0, max + 1, 1).setValues(Array(max + 1).fill([ws.getName()]));
            outputRow += max + 1;
        }
    });

    const tblDataRange = outputWs.getRange("A1").getSurroundingRegion();
    const tbl = outputWs.addTable(tblDataRange, true);
    tbl.setName("data");

    console.log("Done!");
}

An error occurs on the line where 'namedRanges.find(...)' is used. There seems to be no 'findAsync' method in the Office Scripts API, causing this issue. I'm unsure of the reason behind this error.

I would appreciate any assistance in understanding what's causing the problem and how to resolve it.

Answer №1

Upon thorough examination of the error message in Power Automate, it became evident that the issue could be attributed to how Power Automate processes the JavaScript find() function. To avoid any potential confusion arising from this, I made the decision to replace find() with a forEach() loop for array iteration. This alternative method proved to be successful.

Below is the revised script:

function main(workbook: ExcelScript.Workbook) {
    const names = ["EVMSChart", "AC", "EV", "PV"];
    let outputRow = 0;
    let outputWs = workbook.getWorksheet("Graph Data");

    if (!outputWs) {
        outputWs = workbook.addWorksheet("Graph Data");
    } else {
        outputWs.getRange("A:BZ").clear();
    }

    outputWs.getRange("A1:E1").setValues([["Sheet Name", "EVMSChart", "AC", "EV", "PV"]]);

    workbook.getWorksheets().forEach((ws) => {
        let max = 0;
        let hasData = false;
        for (let i = 0; i < names.length; i++) {
            const namedRanges = ws.getNames();

          namedRanges.forEach(nr => {
            if (nr.getName() === names[i]) {
              const range = nr.getRange();
              const data = range.getValues();
              let outputData: (string[][]) = [];
              for (let j = 0; j < data.length; j++) {
                const row = data[j];
                for (let k = 0; k < row.length; k++) {
                  const value = row[k].toString();
                  // Write only the value to the outputData array
                  outputData.push([value]);
                  max = Math.max(max, k);
                }
              }
               if (outputData.length > 0) {
                outputWs.getRangeByIndexes(outputRow + 1, i + 1, outputData.length, 1).setValues(outputData);
                hasData = true;
              }
            }
          });
        }
        if (hasData) {
            outputWs.getRangeByIndexes(outputRow + 1, 0, max + 1, 1).setValues(Array(max + 1).fill([ws.getName()]));
            outputRow += max + 1;
        }
    });

    const tblDataRange = outputWs.getRange("A1").getSurroundingRegion();
    const tbl = outputWs.addTable(tblDataRange, true);
    tbl.setName("data");

    console.log("Done!");
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to verify the identity of a user using an external authentication service

One of my microservices deals with user login and registration. Upon making a request to localhost:8080 with the body { "username": "test", "password":"test"}, I receive an authentication token like this: { "tok ...

Issue with Material Sort functionality when null objects are present

I've encountered an issue with my code. I created a feature that adds empty rows if there are less than 5 rows, but now the sort function is no longer functioning properly. Strangely, when I remove the for loop responsible for adding empty rows, the s ...

What are some ways to resolve this console error: "TS2307: Could not locate module '@components/common/ButtonBlock' or its corresponding type declarations."

While the project is running smoothly, I am noticing a multitude of errors appearing in the console of VS Code. How can I eliminate these error messages? It seems to be related to TypeScript. Additionally, I am encountering an error in the browser as well ...

Can you explain the concept of F-Bounded Polymorphism in TypeScript?

Version 1.8 of TypeScript caught my attention because it now supports F-Bounded Polymorphism. Can you help me understand what this feature is in simple terms and how it can be beneficial? I assume that its early inclusion signifies its significance. ...

Angular - Tips for effectively positioning a fixed navbar while scrolling to sections sharing the same ID

In my Angular project, I've set up different sections on a page, each with its own unique ID. There's also a fixed-position navbar for easy navigation between these sections. My main objective is to ensure that when a user clicks on a menu item ( ...

Integrating jquery into an angular project

I am facing an issue setting up jquery in an angular 6 project. When I try to import it in the ts file, I encounter the following error: Error: This module can only be referenced with ECMAScript imports/exports by turning on the 'allowSyntheticDe ...

Steps to set angular for all items in the dropdown menu:

I am currently working on implementing a dropdown feature within my Angular application. The dropdown will display a list of shops, and when a shop is selected, it will show the content related to that particular shop. I need to add a new item called "ALL ...

Connecting RxJS Observables with HTTP requests in Angular 2 using TypeScript

Currently on the journey of teaching myself Angular2 and TypeScript after enjoying 4 years of working with AngularJS 1.*. It's been challenging, but I know that breakthrough moment is just around the corner. In my practice app, I've created a ser ...

Unexpected token in catch clause in React Native TypeScript

Despite having a fully configured React Native Typescript project that is functioning as expected, I have encountered a peculiar issue: Within all of my catch blocks, due to the strict mode being enabled, typescript errors are appearing like this one: htt ...

Guide on implementing conditional return types in React Query

In my approach, I have a method that dynamically uses either useQuery or useMutation based on the HTTP method passed as a prop. However, the return type of this method contains 'QueryObserverRefetchErrorResult<any, Error>', which lacks meth ...

Rollup bundling with Typescript and troublesome rollup-plugin-typescript2 experience

I'm currently facing some unexpected challenges while attempting to extract a small portion of a monorepo into a web client library. The issue seems to be related to the configuration of Rollup, as shown below: import resolve from "rollup-plugin-node ...

How to Utilize findIndex to Validate the Presence of Elements in an Array of Objects using TypeScript

I need assistance in checking which properties from an array are present in another array of objects and which ones are not. My object structure is as follows: var tempObj=[{id: '1', color: 'red, blue, green', age: 27},{id: '2& ...

Having trouble accessing numerical data from a Microsoft Excel file using Selenium WebDriver in Java

Could someone assist with the error message "cannot get text value from numeric cell" in my Java code below. Additionally, I am having trouble aligning my output properly. The desired output should be: Username Password john 123 rambo 456 However, ...

How do I retrieve a specific svg element in Angular among multiple elements?

I recently delved into learning Angular for a new project. One of my main objectives was finding a way to dynamically alter the styles of SVG elements. This led me to utilizing ViewChild and ElementRef. Here is an example from the HTML: <svg><g ...

Customizable mongoDB database collection

Is there a more efficient way to make calls to different collections based on a function parameter? I'm exploring the possibility and if it's not feasible, I'll handle it case by case. Currently, I have this code and the goal is to have a u ...

The best approach for setting a select value and managing state in React using TypeScript

Currently, I am in the process of familiarizing myself with TypeScript within my React projects. I have defined a type for the expected data structure (consisting of name and url). type PokedexType = { name: string; url: string; } The API respon ...

Is there a way to define a return type conditionally depending on an input parameter in typing?

I need help ensuring that a function in TypeScript returns a specific type based on a parameter. How can I make TypeScript understand my intention in this scenario? type X = 'x' type Y = 'y' const customFunc = <Type extends X | Y> ...

Transferring cell formats from Excel to a range is a time-consuming process

When I copy one Excel row format (template row) to a range of rows filled with data using the code below, it starts to slow down significantly as the number of rows increases. In my current test, with around 33,000 lines in the target range, the process be ...

What is the best way to showcase the information retrieved from my API?

I am attempting to display the ID and Document number that are retrieved from an array. Data Returned However, I am not seeing any results in return. You can view the application results here. I have tried using string interpolation like {{document.id}} ...

React JS hosted externally along with just plain Javascript and HTML page

We are looking to implement a common widget on multiple services using ReactJS. The goal is to write client-side code for this widget as an external hosted JavaScript file that can be included in pages across different frameworks such as Angular, Inferno, ...