我已经尽我所能了。我正在使用Excel处理一些JavaScript代码。我有两个控件:文本框和组合框。文本框接受用户号输入,下拉列表列出要采取的操作(乘、除、加、减)。要点是,我接受当前的选择并通过指定的值执行指定的操作,然后将其写回所选内容。在Excel中,这将与PasteSpecial/运算符相同。
这是我的JavaScript:
$('#mathButton').click(function() {
    callMathify()
        .catch(OfficeHelpers.logError);
});
function callMathify() {
    return Excel.run(function(context) {
        var inputValue = $('#input').val();
        if (inputValue == '') {
            console.log('Please enter a value first!');
        } else {
            var mathType = $('#mathAction').val();
            var selection = context.workbook.getSelectedRange();
            selection.load('values');
            console.log('Input: ' + inputValue + ', Operator: ' + mathType);
            return context.sync()
                .then(function() {
                    var newValues = selection.values.map(row => {
                        if (mathType = 'Multiply') {
                            return row.map(val => {return val * Number($('#input').val())});
                        } else if (mathType = 'Divide') {
                            return row.map(val => {return val / Number(inputValue)});
                        } else if (mathType = 'Add') {
                            return row.map(val => {return val + Number(inputValue)});
                        } else if (mathType = 'Subtract') {
                            return row.map(val => {return val - Number(inputValue)});
                        }
                    });
                    selection.values = newValues;
                    //return context.sync();
            });
        }
        return context.sync();
    });
}这里我有一个简单的HTML端:
<p class="ms-font-m">
    Do some math on all cells in the selection.
    <br>
    Enter a value, choose the action, then click the button.
    <br>
</p>
<table>
    <tr>
        <td><p class="ms-font-m">Value: </p><input type="text" id="input" /></td>
        <td><p class="ms-font-m">Action: </p><select id="mathAction">
            <option value="multiply">Multiply</option>
            <option value="divide">Divide</option>
            <option value="add">Add</option>
            <option value="subtract">Subtract</option>
        </td>
    </tr>
</table>
<br>
<button id="mathButton" class="ms-Button">
    <span class="ms-Button-label">Mathify</span>
</button>我在使用“map”时遇到了一个错误,但现在我没有收到任何错误。有人看到我做错什么了吗?
编辑:用变量替换文本框引用,我忘了在投递之前添加它。
编辑:我根据Jakob的建议对代码进行了调整,代码运行良好。我编辑了选项控件的HTML id,以匹配下拉值(适当的大小写)的大小写敏感性。下面的代码运行良好。
$('#mathButton').click(function() {
    callMathify()
        .catch(OfficeHelpers.logError);
});
function callMathify() {
        return Excel.run(function(context) {
            var inputValue = $('#input').val();
            if (inputValue === '') {
                console.log('Please enter a value first!');
                return context.sync();
            } else {
                var selection = context.workbook.getSelectedRange();
                selection.load('values');
                return context.sync()
                    .then(function() {
                        var mathType = $('#mathAction').val();
                        switch (mathType) {
                            case "Multiply":
                                var newValues = selection.values.map(row => {
                                    return row.map(val => {return Number(val) * Number(inputValue)});
                                });
                                break;
                            case "Divide":
                                var newValues = selection.values.map(row => {
                                    return row.map(val => {return Number(val) / Number(inputValue)});
                                });
                                break;
                            case "Add":
                                var newValues = selection.values.map(row => {
                                    return row.map(val => {return Number(val) + Number(inputValue)});
                                });
                                break;
                            case "Subtract":
                                var newValues = selection.values.map(row => {
                                    return row.map(val => {return Number(val) - Number(inputValue)});
                                });
                                break;
                        }
                        selection.values = newValues;
                        console.log('Mathify operation complete! (' + mathType + ')');
                        return context.sync();
                });
            }
        });
    }发布于 2016-11-11 16:41:26
很高兴你问扎克!
要使代码片段正常工作,需要进行3项更改:
我还注意到,对于其中的3种情况,您使用了inputValue变量,对于第一种情况,使用了$('#input').val()。我修改了您的代码片段,以便在所有情况下都使用inputValue。
    function callMathify() {
        return Excel.run(function(context) {
            var inputValue = $('#input').val();
            if (inputValue == '') {
                console.log('Please enter a value first!');
            } else {
                var mathType = $('#mathAction').val();
                var selection = context.workbook.getSelectedRange();
                selection.load('values');
                console.log('Input: ' + inputValue + ', Operator: ' + mathType);
                return context.sync()
                    .then(function() {
                        var newValues = selection.values.map(row => {
                            if (mathType === 'multiply') {
                                return row.map(val => {return val * Number(inputValue)});
                            } else if (mathType === 'divide') {
                                return row.map(val => {return val / Number(inputValue)});
                            } else if (mathType === 'add') {
                                return row.map(val => {return val + Number(inputValue)});
                            } else if (mathType === 'subtract') {
                                return row.map(val => {return val - Number(inputValue)});
                            }
                        });
                        selection.values = newValues;
                        return context.sync();
                });
            }        
        });
    }您还可以做一些更改,以使代码更优化、更简洁。以下是一些建议:
我会让你去尝试那些改变,因为它超出了你最初的问题。
雅各布
https://stackoverflow.com/questions/40537854
复制相似问题