首页 > 解决方案 > 使用office.js在线复制粘贴(循环)到多个Excel范围时出错

问题描述

我正在尝试使用公式填充多个范围,然后使用粘贴作为值将范围转换为值。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)
    }

标签: exceloffice-jsoffice-addinsoffice365apiexcel-web-addins

解决方案


你能评论一下context.runtime.enableEvents = false;并检查它是否有效吗?


推荐阅读