Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyroberts committed May 30, 2018
0 parents commit 4afc434
Show file tree
Hide file tree
Showing 1,573 changed files with 613,435 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# Maven build output
**/target

# IntelliJ files
*.idea
*.iml
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# jinx-com4j
Excel COM bindings for use with Jinx
Binary file added examples/jinx-com4j-examples.xlsx
Binary file not shown.
21 changes: 21 additions & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.exceljava</groupId>
<artifactId>jinx-com4j-root</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jinx-com4j-examples</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.exceljava</groupId>
<artifactId>jinx-com4j</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.exceljava.com4j.examples;

import com.exceljava.jinx.ExcelAddIn;
import com.exceljava.jinx.ExcelMacro;

import com.exceljava.com4j.JinxBridge;
import com.exceljava.com4j.excel.*;
import com4j.Com4jObject;

/**
* Example macros that use com4j to call back into Excel
* when triggered from an Excel control.
*/
public class MacroFunctions {
private final ExcelAddIn xl;

/**
* Called by Jinx when binding the instance methods to Excel macros.
*
* Using a non-default constructor taking an ExcelAddIn is necessary
* so that we can get hold of the COM Excel Application wrapper later.
*
* @param xl ExcelAddIn instance provided by Jinx.
*/
public MacroFunctions(ExcelAddIn xl) {
this.xl = xl;
}

/**
* Example macro function to be bound to a checkbox.
*
* It sets the value of a named range with the name of the
* checkbox + "_OUTPUT"
*/
@ExcelMacro("jinx.checkbox_example")
public void checkboxExample() {
_Application app = JinxBridge.getApplication(xl);

// get the checkbox that called this macro
_Worksheet sheet = app.getActiveSheet().queryInterface(_Worksheet.class);
CheckBoxes checkboxes = sheet.checkBoxes().queryInterface(CheckBoxes.class);
CheckBox checkbox = checkboxes.item(app.getCaller()).queryInterface(CheckBox.class);

// Find the named range for this checkbox
String name = checkbox.getName();
Range range = sheet.getRange(name + "_OUTPUT");

// Set the cell value based on the checkbox state
Object value = checkbox.getValue();
if (value instanceof Double && (Double)value != 0.0) {
range.setValue("Checked!");
} else {
range.setValue("Click the checkbox");
}
}

/**
* Example macro function to be bound to a scrollbar.
*
* It sets the value of a named range with the name of the
* scrollbar + "_OUTPUT" to the current scrollbar value.
*/
@ExcelMacro("jinx.scrollbar_example")
public void scrollbarExample() {
_Application app = JinxBridge.getApplication(xl);

// Get the scrollbar that called this macro
_Worksheet sheet = app.getActiveSheet().queryInterface(_Worksheet.class);
ScrollBars scrollbars = sheet.scrollBars().queryInterface(ScrollBars.class);
ScrollBar scrollbar = scrollbars.item(app.getCaller()).queryInterface(ScrollBar.class);

// Find the named range for this scrollbar
String name = scrollbar.getName();
Range range = sheet.getRange(name + "_OUTPUT");

// Set the cell value from the scrollbar value
range.setValue(scrollbar.getValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.exceljava.com4j.examples;

import com.exceljava.jinx.ExcelAddIn;
import com.exceljava.jinx.ExcelMenu;

import com.exceljava.com4j.JinxBridge;
import com.exceljava.com4j.excel.*;
import com4j.Com4jObject;

/**
* Example menu functions that use com4j to call back into Excel
* when triggered from an Excel menu.
*/
public class MenuFunctions {
private final ExcelAddIn xl;

/**
* Called by Jinx when binding the instance methods to Excel menus.
*
* Using a non-default constructor taking an ExcelAddIn is necessary
* so that we can get hold of the COM Excel Application wrapper later.
*
* @param xl ExcelAddIn instance provided by Jinx.
*/
public MenuFunctions(ExcelAddIn xl) {
this.xl = xl;
}

/**
* Example menu function that takes the current selection and sets
* the background colors randomly.
*
* This will appear as a menu item under 'Jinx' in the Add-Ins tab
* in Excel.
*/
@ExcelMenu(
value = "Randomise Colors",
subMenu = "Com4J Examples"
)
public void randomiseColors() {
_Application app = JinxBridge.getApplication(xl);

Range active = app.getSelection().queryInterface(Range.class);
for (int row = 1; row <= active.getRows().getCount(); row++) {
for (int col = 1; col <= active.getColumns().getCount(); col++) {
Range cell = ((Com4jObject)active.getItem(row, col)).queryInterface(Range.class);

int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);

int color = red | (green << 8) | (blue << 16);
cell.getInterior().setColor(color);
}
}
}
}
9 changes: 9 additions & 0 deletions examples/src/main/resources/jinx-classes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# Classes to be loaded by the Jinx Add-In
#
# This is an alternative to listing all the classes in the jinx.ini
# config file. Instead, as long as the jar is on the class path
# Jinx will read this resource and load the classes listed below.
#
com.exceljava.com4j.examples.MacroFunctions
com.exceljava.com4j.examples.MenuFunctions
71 changes: 71 additions & 0 deletions jinx-com4j/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.exceljava</groupId>
<artifactId>jinx-com4j-root</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jinx-com4j</artifactId>
<packaging>jar</packaging>

<!-- ==================================================================== -->
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<!-- ==================================================================== -->
<dependencies>
<!-- Jinx -->
<dependency>
<groupId>com.exceljava</groupId>
<artifactId>jinx</artifactId>
<version>[1.0.0,)</version>
</dependency>
<!-- com4j -->
<dependency>
<groupId>com.github.exceljava.com4j</groupId>
<artifactId>com4j</artifactId>
<version>release-20180530</version>
</dependency>
</dependencies>

<!-- ==================================================================== -->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
40 changes: 40 additions & 0 deletions jinx-com4j/src/main/java/com/exceljava/com4j/JinxBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.exceljava.com4j;

import com.exceljava.jinx.ExcelAddIn;
import com.exceljava.com4j.excel._Application;
import com4j.COM4J;
import com4j.Com4jObject;
import com4j.ComThread;
import com4j.ROT;

/**
* Bridge between the Jinx add-in and com4j.
* Used for obtaining com4j COM wrappers from code running in Excel using Jinx.
*/
public class JinxBridge {

private static final ThreadLocal<_Application> xlApp = new ThreadLocal<_Application>() {
public _Application initialValue() {
return null;
}
};

/**
* Gets the Excel Application object for the current Excel process.
*
* This can then be used to call back into Excel using the Excel
* automation API, in the same way as VBA can be used to automate
* Excel.
*
* The _Application object and objects obtained from it should only
* be used from the same thread as it was obtained on. Sharing it
* between threads will cause issues, and may cause Excel to crash.
*
* @param xl The ExcelAddIn object obtained from Jinx.
* @return An Excel Application instance.
*/
public static _Application getApplication(ExcelAddIn xl) {
Com4jObject unk = COM4J.wrapSta(Com4jObject.class, xl.getExcelApplication());
return unk.queryInterface(_Application.class);
}
}
Loading

0 comments on commit 4afc434

Please sign in to comment.