首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取输入和组合框值,并将值映射到新数组,写入Excel

获取输入和组合框值,并将值映射到新数组,写入Excel
EN

Stack Overflow用户
提问于 2016-11-10 22:06:11
回答 1查看 201关注 0票数 0

我已经尽我所能了。我正在使用Excel处理一些JavaScript代码。我有两个控件:文本框和组合框。文本框接受用户号输入,下拉列表列出要采取的操作(乘、除、加、减)。要点是,我接受当前的选择并通过指定的值执行指定的操作,然后将其写回所选内容。在Excel中,这将与PasteSpecial/运算符相同。

这是我的JavaScript:

代码语言: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端:

代码语言:javascript
运行
复制
<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,以匹配下拉值(适当的大小写)的大小写敏感性。下面的代码运行良好。

代码语言: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!');
                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();
                });
            }
        });
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-11 16:41:26

很高兴你问扎克!

要使代码片段正常工作,需要进行3项更改:

  • 需要取消对最内部的context.sync()块中的.then()进行注释。这将确保选择(范围)对象与Excel同步,并将新值应用于选定的单元格。
  • 当您将下拉列表的值与字符串进行比较时,需要使用'===‘运算符。
  • 下拉列表控件中的值应该与您在比较中使用的值相匹配,因为它区分大小写。

我还注意到,对于其中的3种情况,您使用了inputValue变量,对于第一种情况,使用了$('#input').val()。我修改了您的代码片段,以便在所有情况下都使用inputValue。

代码语言:javascript
运行
复制
    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();
                });
            }        
        });
    }

您还可以做一些更改,以使代码更优化、更简洁。以下是一些建议:

  • 通过声明一个函数,然后在循环中使用它,对.map循环之外的操作进行比较。
  • 使用开关语句而不是链式if语句。

我会让你去尝试那些改变,因为它超出了你最初的问题。

雅各布

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40537854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档