durs / node-activex

Node.JS Implementaion of ActiveXObject
MIT License
329 stars 62 forks source link

Variant2Array: Calling a new dedicated function (Variant2Array2) #68

Closed somanuell closed 4 years ago

somanuell commented 4 years ago

Hi, when trying to automate Excel, it is very handy, performance-wise, to be able to read large numbers of cells, using a rectangular "Range".

This PR calls a dedicated function when Variant2Array detects that the SAFEARRAY is a bidimentional one. The new function (called Variant2Array2) is very largely a variation of Variant2Array, except that there is two loops instead of one. The return value is a JavaScript Array of JavaScript Arrays. The most significant dimension is conventionally the rightmost in the SAFEARRAY.

With this PR, the following JavaScript code works:

const ActiveX = require('./build/debug/node_activex.node');
let comExcelApp = new ActiveX.Object("Excel.Application");
comExcelApp.Visible = true;
comExcelApp.Workbooks.Add();
let w = comExcelApp.ActiveSheet;
w.Range("A1").Value = "A1";
w.Range("A2").Value = "A2";
w.Range("A3").Value = "A3";
w.Range("B1").Value = "B1";
w.Range("B2").Value = "B2";
w.Range("B3").Value = "B3";
let Rect = w.Range("A1:B3").Value.__value;
console.log( 'A1:B3 Value is ' + Rect );

The output is: A1:B3 Value is A1,B1,A2,B2,A3,B3 and the Visual Code debugger shows that Rect is indeed a native JavaScript Array of 3 "rows" each of them being a JavaScript Array of 2 columns.

Please let me know what you think about that code.

durs commented 4 years ago

it's all good, but still need to be implemented (Value2Variant) w.Range("A1:B3").Value = [ ["A1", "B1"], ["A2", "B2"], ["A3", "B3"] ];

somanuell commented 4 years ago

I will try to do it. (and thanks for merging the PR, I know some users of your addon who are super happy)