0

Is it possible to insert save a formula into a column within a list in list view using jQuery or JavaScript?

2

In English language environments no problem,
in other languages I can not get it to work, no one has answered my question yet:
Save Calculated Column Formula in Language Localized notation using REST or JSOM?

Scriptfile

Extract the JSOM code (the REST version is way more code) you are after or

execute in an existing Formula (FldEdit.aspx page)
as Chrome snippet or as a Bookmarklet or as a GreaseMonkey/TamperMonkey script or stick it into your MasterPage.

/*global SP,document,STSHtmlDecode,clearTimeout,setTimeout,_spPageContextInfo*/
SP.SOD.executeFunc('sp.js', 'SP.ClientContext',
    function () {
        var textarea = document.getElementById('onetidIODefTextValue1'),
            status = function (text, color) {
                SP.UI.Status.removeAllStatus(true);
                SP.UI.Status.setStatusPriColor(SP.UI.Status.addStatus(text), color);
            };
        status('CalcMaster loaded', 'yellow');
        textarea.onkeyup = function (event) {
            if ([37, 38, 39, 40].indexOf(event.keyCode) === -1) { //ignore arrow keys
                clearTimeout(typing);
                var typing = setTimeout(function () {
                    var guid = _spPageContextInfo.pageListId.slice(1, 37),
                        title = document.getElementById('idColName').value,
                        formula = STSHtmlDecode(textarea.value),
                        ctx = SP.ClientContext.get_current(),
                        fields = ctx.get_web().get_lists().getById(guid).get_fields(),
                        field = ctx.castTo(fields.getByInternalNameOrTitle(title), SP.FieldCalculated);
                    //formula = ('=IF(TRUE,1,2)');
                    field.set_formula(formula);
                    field.update();
                    status('Saving: ' + title, 'blue');
                    ctx.executeQueryAsync(function () {
                        status('Formula saved!', 'green');
                    }, function (sender, args) {
                        status(args.get_message(), 'red');
                    });
                }, 500); //do not save while typing
            }
        };
    }
);

BTW. It is not jQuery OR JavaScript....

jQuery

Originates from 2006, a time when each Browser builder had a different take on HTML and JavaScript(=ECMAScript).
jQuery really added 'standardized' value to a front-end developers toolbelt.

When JavaScript evolved, and more and more browsers standardized on ES5 (from 2009 onwards) only IE8 was the odd one out which still validated the use of jQuery.

The biggest problem is when you search for solutions you get that 10 years of jQuery history where, most often, nowadays native JavaScript can do the job as easy as jQuery (and faster)

No need for jQuery?

Not many know Microsoft (in SP2013) added a trimmed down flavour of jQuery called mQuery (mobile Query)

With modern browsers you can do jQuery operations just as easily with native JavaScript code.

Good blogs:

My advice on jQuery

Modern browsers and languages ES2015 (formerly known as ES6 and was ratified in June 2015) are replacing the need for jQuery.
Learn new technologies, or at least why you will not apply them yet
Or are you still coding in HTML 1.0 ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.