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...
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