Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
Return text from selected shape in Powerpoint with Javascript API

I am currently building a task-pane add in for powerpoint and I am struggling with extracting the text of a shape with the Javascript API and assigning it to my this.state.editor. The code snippets I found online all look something like this - which does not work (although I have not found an exact example for text):

  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 });
      }
    });
  };

The following function however works for INSERTING text

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

Unfortunately I am not finding a hint in the documentation what exactly is being returned by the getSelectedDataAsync. Does anyone here have an idea?

EDIT: to be specific with "doesn't work" I meant, that my state (that is used to fill a text area) is not updating. The following snippet works in script lab:

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));
    }
  });
}

So I would assume,that the returned value is actually, what I want. My current issue with that is, when I try to write the results like this:

  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) });
      }
    });
  };

to my state:

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;
  }

The state doesn't seem to update (as I can't see the text area that shows the content of this.state.editor update. When I replace the state with a self written string like "test" it updates...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

So in the end this worked. Not sure why. Maybe I had to bind thisto the function. Not having error messages while developing kinda sucks.

  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) });
      }
    });
  };

This thread brought me onto it: Call React setState() function from async callback function

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda