【问题标题】:Return text from selected shape in Powerpoint with Javascript API使用 Javascript API 从 Powerpoint 中的选定形状返回文本
【发布时间】:2022-06-11 22:43:30
【问题描述】:

我目前正在为 powerpoint 构建一个任务窗格插件,我正在努力使用 Javascript API 提取形状的文本并将其分配给我的this.state.editor。我在网上找到的代码 sn-ps 都看起来像这样 - 不起作用(虽然我没有找到一个确切的文本示例):

  getData = () => {
    Office.context.document.getSelectedDataAsync(Office.CoercionType.Text, function (asyncResult) {
      if (asyncResult.status == Office.AsyncResultStatus.Failed) {
        this.setState({ editor: asyncResult.error.message});
      } else {
        this.setState({ editor: asyncResult.value });
      }
    });
  };

以下函数适用于插入文本

  insertText = () => {
    Office.context.document.setSelectedDataAsync(this.state.editor, function (asyncResult) {
      if (asyncResult.status === Office.AsyncResultStatus.Failed) {
        showNotification("Error", asyncResult.error.message);
      }
    });
  };

不幸的是,我没有在文档中找到getSelectedDataAsync 究竟返回什么的提示。这里有人有想法吗?

编辑:具体来说“不起作用”我的意思是,我的状态(用于填充文本区域)没有更新。以下 sn-p 在脚本实验室工作:

function getSlideMetadata() {
  Office.context.document.getSelectedDataAsync(Office.CoercionType.Text, function(asyncResult) {
    if (asyncResult.status === Office.AsyncResultStatus.Failed) {
      console.error(asyncResult.error.message);
    } else {
      console.log(JSON.stringify(asyncResult.value, null, 4));
    }
  });
}

所以我会假设,返回的值实际上是我想要的。 我目前的问题是,当我尝试编写这样的结果时:

  getSlideMetadata = () => {
    Office.context.document.getSelectedDataAsync(Office.CoercionType.Text, function (asyncResult) {
      if (asyncResult.status === Office.AsyncResultStatus.Failed) {
        console.error(asyncResult.error.message);
      } else {
        this.setState({ selectedText: JSON.stringify(asyncResult.value, null, 4) });
      }
    });
  };

到我的状态:

export default class App extends React.Component {
  constructor(title, isOfficeInitialized) {
    super(title, isOfficeInitialized);

    this.state = {
      selectedPage: 0,
      editor: "",
      history: [],
      loading: false,
      inputFocused: false,
      selectedText: "begin",

      // setting
      modelOptions: [],
      responseLength: 400,
      temp: 0.5,
      model: null,
    };
    this.editorRef = React.createRef();
    this.title = title;
  }

状态似乎没有更新(因为我看不到显示this.state.editor 更新内容的文本区域。当我用像“test”这样的自写字符串替换状态时,它会更新......

【问题讨论】:

  • 请提供比“不起作用”更多的信息。出了什么问题?您期望看到什么以及您实际看到什么?另外,你能创建一个重现问题的Script Labsn-p 吗?
  • 嘿,我编辑了这个问题。问题似乎围绕着将 getSlideMetadata 的结果写入 this.state.editor
  • 好吧,this.setState({ selectedText: JSON.stringify(asyncResult.value, null, 4) }); 将更新 state.selectedText,如果它更新任何内容。因此,预计它不会更新state.editor。我错过了什么吗?
  • 另外,如果您在“else”块中有这 3 行,您会看到什么? console.log(JSON.stringify(asyncResult.value, null, 4)); this.setState({ editor: JSON.stringify(asyncResult.value, null, 4) }); console.log(this.state.editor);
  • 不幸的是,此代码位于 powerpoint 的任务窗格加载项中。 console.log 在那里不起作用......我也很想看到。

标签: javascript powerpoint office-js office-addins powerpoint-addins


【解决方案1】:

所以这最终奏效了。不知道为什么。也许我必须将this绑定到该函数。开发时没有错误消息有点糟糕。

  getSlideMetadata = async () => {
    Office.context.document.getSelectedDataAsync(Office.CoercionType.Text, {}, (asyncResult) => {
      if (asyncResult.status === Office.AsyncResultStatus.Failed) {
        console.error(JSON.stringify(asyncResult.error.message, null, 4));
      } else {
        this.setState({ editor: JSON.stringify(asyncResult.value, null, 4) });
      }
    });
  };

这个话题把我带到了它: Call React setState() function from async callback function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-13
    • 2021-05-16
    • 2018-06-03
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多