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?
<% JavaScriptCode %>. Have you tried converting to JSON?dynamicwas introduced for. But @AlexT. is right. More information is needed. I will note however that JavaScript does not support multi-dimensional arrays. It supports jagged arrays. Use a jagged array on the C# side as well if you're interoperating with the language that has no concept of a multi-dimensional array.