保存工作簿的初始状态 [英] Save the initial state of a workbook

查看:73
本文介绍了保存工作簿的初始状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一开始保存工作簿(或Excel应用程序)的初始状态,以便无论我的加载项对工作簿进行了什么修改,我都可以随时恢复到原来的状态.

I would like to save the initial state of a workbook (or an Excel application) in the beginning, so that I could always get back to it regardless of modification on the workbook by my Add-in.

我在Home.js中尝试了以下一些代码:

I tried some following code in Home.js:

(function() {
    "use strict";

    Office.initialize = function(reason) {
        $(document).ready(function() {
            app.initialize();
            initial();
            $('#getInitial').click(getInitial);
        });
    };

    var ctxInitial;

    function initial () {
        ctxInitial = new Excel.RequestContext();
    }

    function getInitial() {
        Excel.run(function () { 
            var wSheetName = 'Sheet1';
            var worksheet = ctxInitial.workbook.worksheets.getItem(wSheetName);
            var usedRange = worksheet.getUsedRange();
            usedRange.load(["values"]);
            return ctxInitial.sync().then(function() {
                document.getElementById("area").value += usedRange.values.toString();
            });
        });
    }
})();

从一开始,我的测试就很好地打印了工作表的初始值.但是,在对某些单元格值进行一些手动修改后,getInitial打印工作表的当前状态,而不是初始值.

In the very beginning, my tests prints well the initial values of the worksheet. However, after some manual modification on some cell values, getInitial prints the current state of the worksheet rather than the initial values.

有人知道实现这一目标的最佳实践是什么吗?

Does anyone know what's the best practice to realise this?

推荐答案

上下文不存储工作簿状态(这怎么可能?它是一个JavaScript对象,与文档位于一个完全独立的世界中).上下文仅仅是要分派哪些操作的管道(或者,如果您愿意,它是命令的累加器).

A context does not store workbook state (How could it? It's a JavaScript object that lives in a completely separate world from the document). A context is merely a pipeline (or, if you will, an accumulator of commands) of what actions to dispatch.

没有任何真正的理由挂在上下文对象上,只是将其用作从同一上下文创建两个对象的一种方式(例如,range1.getIntersection(range2),因为对象必须来自同一上下文才能进行交互) ).但是除此之外,上下文的生命可以(并且通常应该)尽可能快.这就是为什么在Excel.run中,我们总是为您创建一个新的上下文,并在最后处理它.

There is no real reason to hang on to a context object, except for using it as a way of creating two objects from the same context (e.g., range1.getIntersection(range2), since objects must be from the same context in order to interact). But beyond that, the context's life can (and generally should) be as quick as possible. That's why in Excel.run we always create a new context for you, and dispose of it at the end.

在相关说明上,出于相同的原因,执行Excel.run并且不使用其提供的上下文(或使用示例中的其他上下文)也没有意义.您可以轻松地在没有Excel.run的情况下运行代码,如果您要重用现有上下文,那么将其包含在Excel.run块中并不会带来任何好处(并且请注意,您不会获得自动对象跟踪)干净的Excel.run).

On a related note, and for the same reasoning, it makes no sense to do an Excel.run and NOT use the context that it provides (or use a different context, as you do in your example). You could just as easily run your code without an Excel.run, it gains you nothing to have it be in an Excel.run block if you're reusing an existing context (and note that you won't get the automatic object-tracking that you would have with a clean Excel.run).

希望这会有所帮助!

〜MSFT Office可扩展性团队的开发人员Michael Zlatkovsky

~ Michael Zlatkovsky, developer on Office Extensibility team, MSFT

这篇关于保存工作簿的初始状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆