【问题标题】:Error in copy-pasting(in loop) to multiple Excel ranges online, using office.js使用office.js在线复制粘贴(循环)到多个Excel范围时出错
【发布时间】:2020-10-29 15:45:23
【问题描述】:

我正在尝试使用公式填充多个范围,然后使用粘贴作为值将范围转换为值。 Office 加载项正在用于 web 的 Sharepoint Excel。该代码通常可以工作,但有时我会收到“Rich API:发生内部错误”错误,因此公式不会被值替换。第一次之后,每次后续尝试都会发生错误,它会因“Rich API: Timeout”错误而崩溃。大约有 300 种尺寸范围,尺寸约为 25x25。

代码:

async function loadValues() {
        //This function is exectued to fill some ranges after data is retrived from server and pasted in a bacckend table
        await Excel.run(async function main(context) {
            context.workbook.application.calculationMode = "Manual";
            let names = context.workbook.names
            context.application.suspendScreenUpdatingUntilNextSync();
            var rng = names.getItem("controlsToUse").getRange();
            rng.load("values");
            await context.sync();
            context.application.suspendScreenUpdatingUntilNextSync();
            // Controls to use contains the name of the ranges in which data has to be loaded and the ranges from which formula to load data has to be copied
            var controlsToUse = rng.values;
            for (i = 0; i < controlsToUse.length; i++) {
                // str is the range in which data has to be pasted and str1 is the range from which formula has to be copied
                var str = controlsToUse[i][0];
                var str1 = controlsToUse[i][1];
                var range1 = names.getItem(str).getRange();
                range1.copyFrom(str1, Excel.RangeCopyType.formulas);
                range1.untrack();
            }
            await context.sync();
            context.workbook.application.calculate();
            await context.sync();
            context.application.suspendScreenUpdatingUntilNextSync();
            for (i = 0; i < controlsToUse.length; i++) {
                var str = controlsToUse[i][0];
                var range1 = names.getItem(str).getRange();
                range1.copyFrom(str, Excel.RangeCopyType.values);
                range1.untrack();
            }
            await context.sync();
            context.workbook.application.calculationMode = "Automatic";
            await context.sync();
        }).catch(errorHandler)
    }

【问题讨论】:

    标签: excel office-js office-addins office365api excel-web-addins


    【解决方案1】:

    你能把context.runtime.enableEvents = false;注释掉,看看它是否有效吗?

    【讨论】:

    • 嗨@J.Felix Bosco 我试着评论它,但它没有帮助我仍然偶尔收到错误。
    • 我在context.workbook.application 中没有看到calculate() 方法,请检查Microsoft Docs 我想如果你注释掉context.workbook.application.calculate(); 它会正常工作。
    • 感谢您的建议,但该方法存在于 context.workbook.application 中,您可以在此处找到它的文档:docs.microsoft.com/en-us/javascript/api/excel/… 错误也是非常随机的,代码在大多数情况下都有效,但每隔一次虽然它给出了 RichAPI.error
    • 抱歉,谢谢,我没有注意到。
    • 是否可以分享您的 excel 示例数据来帮助重现问题?遇到问题时您使用哪种浏览器?
    猜你喜欢
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 2020-03-22
    相关资源
    最近更新 更多