We have a JavaScript Office add-in. We are making an installable and executable add-in on top of it by VSTO and WebView2.
I just realize that it's not obvious to pass an array value from C# to JavaScript. For instance, on the side of C#, I have the following code:
public object test()
{
int[,] xx = { { 1, 2, 3 }, { 3, 4, 5 } };
return xx;
}
In JavaScript, I have
test () {
return new Promise(async (resolve, reject) => {
try {
let result = await window.chrome.webview.hostObjects.control.test();
console.log(result)
console.log(JSON.stringify(result))
resolve(result)
} catch (error) {
reject(error)
}
})
}
Then, in the console of JavaScript, it shows Array(0)
and []
. If I pass a string value from C# to JavaScript, it works well.
Does anyone know how to pass an array from C# to JavaScript?
You must serialize objects and arrays to any format that suits your needs. But I think for JavaScript the most suitable format would be a JSON string, so you could get a ready made object in JavaScript. See Serialize c# array of strings to JSON array for more information.