durs / node-activex

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

Read a multi-cell range in Excel #64

Closed somanuell closed 4 years ago

somanuell commented 4 years ago

With the Microsoft JScript Engine, hosted in cscript.exe, the following code works fine:

var x= new ActiveXObject("Excel.Application");

x.visible = true;
x.DisplayAlerts = false;
x.Workbooks.Add();
var w = x.ActiveSheet;

w.Range("A1").Value = "A1";
w.Range("A2").Value = "A2";
w.Range("B1").Value = "B1";
w.Range("B2").Value = "B2";

var Magic = w.Range("A1:B2").Value;
WScript.Echo( 'typeof Magic is ' + typeof Magic );
var MyArray = Magic.toArray();
WScript.Echo( 'toArray result is of type: ' + typeof MyArray + ' with value: ' + MyArray );

The output is:

typeof Magic is unknown toArray result is of type: object with value: A1,A2,B1,B2

With node-activex, I can't read multiple cells in one call. What I have tried:

const ActiveX = require('./build/debug/node_activex.node');

let comExcelApp = new ActiveX.Object("Excel.Application");
comExcelApp.Visible = true;
comExcelApp.DisplayAlerts = false;

//add a workbook
comExcelApp.Workbooks.Add();

let comActiveSheet = comExcelApp.ActiveSheet;
//write single value
comActiveSheet.Range("A1").Value = "A1";
comActiveSheet.Range("A2").Value = "A2";
comActiveSheet.Range("B1").Value = "B1";
comActiveSheet.Range("B2").Value = "B2";

// Read several values does not work
const readSeveralValues = comActiveSheet.Range("A1:B2").Value; // always null!

How to use node_activex to read several cells in a single call?

somanuell commented 4 years ago

Since version 1.16: const readSeveralValues = comActiveSheet.Range("A1:B2").Value.valueOf(); returns a bidimensional JavaScript Array (an array of arrays): rows x columns.

coco-super commented 3 years ago

@somanuell

const readSeveralValues = comActiveSheet.Range("A1:B2").Value.valueOf();

The above code works for me. But in my scene, I want to get the cell content displayed on the screen. So my code looks like this:

const readSeveralValues = comActiveSheet.Range("A1:B2").Text;  // not work

Is there any way to solve it? Thank you very much