JS中的备份工作表,删除全部并最终还原

发布于 2025-02-13 12:28:58 字数 832 浏览 0 评论 0原文

目前,我要找到一个解决方案,以帮助我将工作表的内容存储在例如LocalStorage等某个地方。

async backupSheets() {
 await Excel.run(async (context) => {
  var worksheets = context.workbook.worksheets;
  worksheets.load('items');
  await context.sync();
  let wsBackup = worksheets;
  
  
  for (var i = 0; i < worksheets.items.length; i++) {
    console.log(worksheets.items[i].toJSON());
    worksheets.items[i].delete();
  }
  //There will be some code to restore the workshets currently with:
  worksheets = wsBackup;
  //But it doesn't works.

  await context.sync();
 });
}

预期的行为是当我进行评估工作表= wsbackup时,它还原了我在删除循环之前保存的工作表。 目前,这不起作用,我正在为此找到解决方法。

Excel方案应该是.. ..

  • 在一个称为wsbackup删除所有工作表的变量中
  • 所有工作表
  • 复制
  • 工作表应在工作簿中正确恢复,

我得到的实际行为是除了恢复的工作表之外的所有步骤。

Currently I'm triying to find a solution that helps me to storage the content of the worksheets in some place like localstorage for example.

async backupSheets() {
 await Excel.run(async (context) => {
  var worksheets = context.workbook.worksheets;
  worksheets.load('items');
  await context.sync();
  let wsBackup = worksheets;
  
  
  for (var i = 0; i < worksheets.items.length; i++) {
    console.log(worksheets.items[i].toJSON());
    worksheets.items[i].delete();
  }
  //There will be some code to restore the workshets currently with:
  worksheets = wsBackup;
  //But it doesn't works.

  await context.sync();
 });
}

The expected behavior is when I do the assigment worksheets = wsBackup it restore the worksheets that I saved before the delete loop.
Currently this isn't working and I'm triying to find a workaround for this.

An Excel scenario should be..

  • Copy all the worksheets in a variable called wsBackup
  • Delete all worksheets except the last
  • The proxy object that I used for it should be restored with the assigment of the backup wsBackup
  • The worksheets should be restored properly in the workbook

The actual behavior that I got is all the steps except the restored worksheets.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫烠⑼条掵仅有一顆心 2025-02-20 12:28:58

当您将JavaScript对象分配给另一个变量时,您是按参考分配,而不是分配逐值。因此,两个工作表wsbackup都指向内存中的同一对象。删除worksheets.items属性的成员时,您还将删除wsbackup.items属性的成员,因为Workseets and code> and 代码> wsbackup 是同一对象。有关更多信息,请参见此堆栈答案: javascript的分配操作是复制参考

尝试替换行让WSBACKUP = Worksheets;用行:

let wsbackup = context.workbook.worksheets;
wsbackup.load('items');

这些行将创建一个全新的代理对象并加载其项目。

When you assign a JavaScript object to another variable, you are assigning-by-reference, not assigning-by-value. So both worksheets and wsbackup are pointing to the same object in memory. When you delete the members of the worksheets.items property, you are also deleting the members of the wsbackup.items property, since worksheets and wsbackup are the same object. See this Stack answer for more: JavaScript's assignment operation is to copy references.

Try replacing the line let wsBackup = worksheets; with the lines:

let wsbackup = context.workbook.worksheets;
wsbackup.load('items');

These lines will create an entirely new proxy object and load it's items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文