cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
tfa21
New Member

Evaluate a math formula in a string like 2+((23/2)*56)/8 etc return result to canvas

Hi, 
I'm trying to calculate in a canvas app a math equation in a string like 2+((23/2)*56)/8 etc (created by labels.text) 
1 I created an Iframe with https://api.mathjs.org/v4/?expr=2*(7-3), but I cannot get the result to the canvas.
2  Pcf with eval() but also i can't get the result to the canvas.

 

I understand with iframe is impossible,
Has anyone some pcf similar to this, or something with an output to the canvas?

 

thanks!!!!!

2 ACCEPTED SOLUTIONS

Accepted Solutions
ciaran3001
Frequent Visitor

Hey there! 

You could use Power automate to pass a string to that API, and pass the response back to the powerapp.

View solution in original post

v-bofeng-msft
Community Support
Community Support

Hi @tfa21 ,

 

You could create a custom connector , I've made a test for your reference:

vbofengmsft_0-1661243153511.png

vbofengmsft_1-1661243179188.png

vbofengmsft_2-1661243202564.png

vbofengmsft_3-1661243218855.png

 

vbofengmsft_4-1661243233187.png

vbofengmsft_5-1661243260338.jpeg

vbofengmsft_6-1661243273723.png

vbofengmsft_7-1661243349583.png

 

public class script: ScriptBase

{

 

public override async Task < HttpResponseMessage > ExecuteAsync()

{

    // Check which operation ID was used

    if (this.Context.OperationId == "Caculate") 

    {

        return await this.ConvertAndTransformOperation().ConfigureAwait(false);

    }

 

    // Handle an invalid operation ID

    HttpResponseMessage response = new HttpResponseMessage(

        HttpStatusCode.BadRequest

    );

    response.Content = CreateJsonContent(

        $"Unknown operation ID '{this.Context.OperationId}'"

    );

    return response;

}

 

private async Task < HttpResponseMessage > ConvertAndTransformOperation()

{

    // Use the context to forward/send an HTTP request

    HttpResponseMessage response = await this.Context.SendAsync(

        this.Context.Request,

        this.CancellationToken

    ).ConfigureAwait(continueOnCapturedContext: false);

 

    // Do the transformation if the response was successful

    if (response.IsSuccessStatusCode)

    {

        var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(

        continueOnCapturedContext: false

        );

 



        // Wrap the original JSON object into a new JSON object

        var newResult = new JObject

        {

            ["timestamp"] = responseString,

 

        };

 

        response.Content = CreateJsonContent(newResult.ToString());

    }

     return response;

}

 

}

 

vbofengmsft_8-1661243393965.jpeg

 

Best Regards,

Bof

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

View solution in original post

2 REPLIES 2
ciaran3001
Frequent Visitor

Hey there! 

You could use Power automate to pass a string to that API, and pass the response back to the powerapp.

v-bofeng-msft
Community Support
Community Support

Hi @tfa21 ,

 

You could create a custom connector , I've made a test for your reference:

vbofengmsft_0-1661243153511.png

vbofengmsft_1-1661243179188.png

vbofengmsft_2-1661243202564.png

vbofengmsft_3-1661243218855.png

 

vbofengmsft_4-1661243233187.png

vbofengmsft_5-1661243260338.jpeg

vbofengmsft_6-1661243273723.png

vbofengmsft_7-1661243349583.png

 

public class script: ScriptBase

{

 

public override async Task < HttpResponseMessage > ExecuteAsync()

{

    // Check which operation ID was used

    if (this.Context.OperationId == "Caculate") 

    {

        return await this.ConvertAndTransformOperation().ConfigureAwait(false);

    }

 

    // Handle an invalid operation ID

    HttpResponseMessage response = new HttpResponseMessage(

        HttpStatusCode.BadRequest

    );

    response.Content = CreateJsonContent(

        $"Unknown operation ID '{this.Context.OperationId}'"

    );

    return response;

}

 

private async Task < HttpResponseMessage > ConvertAndTransformOperation()

{

    // Use the context to forward/send an HTTP request

    HttpResponseMessage response = await this.Context.SendAsync(

        this.Context.Request,

        this.CancellationToken

    ).ConfigureAwait(continueOnCapturedContext: false);

 

    // Do the transformation if the response was successful

    if (response.IsSuccessStatusCode)

    {

        var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(

        continueOnCapturedContext: false

        );

 



        // Wrap the original JSON object into a new JSON object

        var newResult = new JObject

        {

            ["timestamp"] = responseString,

 

        };

 

        response.Content = CreateJsonContent(newResult.ToString());

    }

     return response;

}

 

}

 

vbofengmsft_8-1661243393965.jpeg

 

Best Regards,

Bof

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Users online (3,933)