Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TemplateFormulaCell : Possibility to add a template for a custom formula #158

Closed
Siemienik opened this issue Feb 10, 2021 · 4 comments · Fixed by #160
Closed

Add TemplateFormulaCell : Possibility to add a template for a custom formula #158

Siemienik opened this issue Feb 10, 2021 · 4 comments · Fixed by #160
Labels
accepted It is accepted to do important Do it first

Comments

@Siemienik
Copy link
Owner

As @jasonclake requested on a Gitter channel

image

@Siemienik Is there a way to template a custom formula
I have a loop starting on row 2 running to row 6 (see image)

Transform something like

#! =SUMPRODUCT( B${test.__startOutput.r}:B${test.__endOutput.r}, C${test.__endOutput.r}:C${test.__endOutput.r}) / SUM(B${test.__startOutput.r}:B${test.__endOutput.r})

Into:

=SUMPRODUCT(B2:B6,C2:C6)/SUM(B2:B6)
@Siemienik
Copy link
Owner Author

I see common assumptions with another proposal #152

@Siemienik
Copy link
Owner Author

I made the first research about using eval function and it looks promising
image

@Siemienik Siemienik added the important Do it first label Feb 10, 2021
@jasonclake
Copy link

Found this technique on stackoverflow - not as flexible as eval() but would cause less security concern maybe? I have not tested or considered how it would fit in the scheme of things.

https://stackoverflow.com/questions/29182244/convert-a-string-to-a-template-string
From: https://stackoverflow.com/users/560114/matt-browne

const regex = /\${[^{]+}/g;

export default function interpolate(template, variables, fallback) {
    return template.replace(regex, (match) => {
        const path = match.slice(2, -1).trim();
        return getObjPath(path, variables, fallback);
    });
}

//get the specified property or nested property of an object
function getObjPath(path, obj, fallback = '') {
    return path.split('.').reduce((res, key) => res[key] || fallback, obj);
}

Usage

const replacements = {
   name: 'Bob',
   age: 37
}

interpolate('My name is ${name}, and I am ${age}.', replacements)

@Siemienik Siemienik mentioned this issue Feb 11, 2021
10 tasks
@Siemienik Siemienik changed the title [Proposal] Possibility to add a template for a custom formula [Draft] Possibility to add a template for a custom formula Feb 11, 2021
@Siemienik Siemienik changed the title [Draft] Possibility to add a template for a custom formula Possibility to add a template for a custom formula Feb 11, 2021
@Siemienik Siemienik added the accepted It is accepted to do label Feb 11, 2021
@Siemienik Siemienik changed the title Possibility to add a template for a custom formula Add TemplateFormulaCell : Possibility to add a template for a custom formula Feb 11, 2021
@Siemienik
Copy link
Owner Author

I move this issue into issues list and mark as important, that means I will do first in my free time.

The TemplateFormulaCell will be matched by:

#= [FORMULA_TEMPLATE]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted It is accepted to do important Do it first
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants