无法使用 React JS 通过 MS office TaskPane 将状态保存到文件中

问题描述 投票:0回答:1

作为编写 MS Word 模板的用户,在“添加字段”选项卡上添加了一些手动字段后,我希望能够将此状态保存到磁盘的配置文件中。按下保存按钮时,应打开一个对话框,允许用户选择文件名并将文件保存到磁盘。该文件应包含 JSON 格式的 this.state.addedFields 中的数据,但使用扩展名 .dg 以便我们可以轻松区分 docgen 文件。 简单地说,单击保存按钮打开对话框以选择在 MS word TaskPane 中保存 .dg 文件的位置。

这段代码在浏览器上运行良好,但是当涉及到 MS TaskPane 时,它不会触发任何东西。

const handleSave = () => {
  const { addedFields } = this.state;
  const data = JSON.stringify(addedFields);
  const filename = "document.dg";

  const blob = new Blob([data], { type: "text/plain;charset=utf-8" });
  await saveAs(blob, filename);
}

混合了

electron
fs
的代码返回无法解析
fs
模块的错误。似乎在反应或使用错误的导入和错误的实现中不起作用

function onSaveButtonClick() {
  // Show the save dialog
  dialog.showSaveDialog({
    defaultPath: 'untitled.dg',
    filters: [{ name: 'DG Files', extensions: ['dg'] }]
  }).then(result => {
    if (result.filePath) {
      // Write the file
      const fileContent = 'Your file contents here'; // Replace with your actual file content
      fs.writeFile(result.filePath, fileContent, 'utf-8', err => {
        if (err) {
          console.error('An error occurred while saving the file:', err);
        } else {
          console.log('The file was saved successfully!');
        }
      });
    } else {
      console.log('The user did not select a file location');
    }
  }).catch(err => {
    console.error('An error occurred while showing the save dialog:', err);
  });
reactjs ms-word office-js office-addins word-web-addins
1个回答
0
投票

我希望能够在配置文件中将此状态保存到磁盘。按下保存按钮时,应打开一个对话框,允许用户选择文件名并将文件保存到磁盘。

出于安全原因,您无法访问最终用户计算机上的本地文件系统。请参阅保留加载项状态和设置,了解 Web 加载项中的可能选项。

使用 Office JavaScript API 的成员将数据存储为:

  • 存储在取决于加载项类型的位置的属性包中的名称/值对。
  • 文档中存储的自定义 XML。

您还可以考虑使用底层浏览器控件提供的技术:浏览器 cookie 或 HTML5 网络存储(localStoragesessionStorage)。但是,某些浏览器或用户的浏览器设置可能会阻止基于浏览器的存储技术。您应该按照 Using the Web Storage API 中的记录测试可用性。

© www.soinside.com 2019 - 2024. All rights reserved.