Skip to content

Commit 4afc434

Browse files
committed
Initial commit
0 parents  commit 4afc434

File tree

1,573 files changed

+613435
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,573 files changed

+613435
-0
lines changed

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*
24+
25+
# Maven build output
26+
**/target
27+
28+
# IntelliJ files
29+
*.idea
30+
*.iml

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# jinx-com4j
2+
Excel COM bindings for use with Jinx

examples/jinx-com4j-examples.xlsx

13.5 KB
Binary file not shown.

examples/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>com.exceljava</groupId>
7+
<artifactId>jinx-com4j-root</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>..</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
<artifactId>jinx-com4j-examples</artifactId>
13+
<packaging>jar</packaging>
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.exceljava</groupId>
17+
<artifactId>jinx-com4j</artifactId>
18+
<version>1.0-SNAPSHOT</version>
19+
</dependency>
20+
</dependencies>
21+
</project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.exceljava.com4j.examples;
2+
3+
import com.exceljava.jinx.ExcelAddIn;
4+
import com.exceljava.jinx.ExcelMacro;
5+
6+
import com.exceljava.com4j.JinxBridge;
7+
import com.exceljava.com4j.excel.*;
8+
import com4j.Com4jObject;
9+
10+
/**
11+
* Example macros that use com4j to call back into Excel
12+
* when triggered from an Excel control.
13+
*/
14+
public class MacroFunctions {
15+
private final ExcelAddIn xl;
16+
17+
/**
18+
* Called by Jinx when binding the instance methods to Excel macros.
19+
*
20+
* Using a non-default constructor taking an ExcelAddIn is necessary
21+
* so that we can get hold of the COM Excel Application wrapper later.
22+
*
23+
* @param xl ExcelAddIn instance provided by Jinx.
24+
*/
25+
public MacroFunctions(ExcelAddIn xl) {
26+
this.xl = xl;
27+
}
28+
29+
/**
30+
* Example macro function to be bound to a checkbox.
31+
*
32+
* It sets the value of a named range with the name of the
33+
* checkbox + "_OUTPUT"
34+
*/
35+
@ExcelMacro("jinx.checkbox_example")
36+
public void checkboxExample() {
37+
_Application app = JinxBridge.getApplication(xl);
38+
39+
// get the checkbox that called this macro
40+
_Worksheet sheet = app.getActiveSheet().queryInterface(_Worksheet.class);
41+
CheckBoxes checkboxes = sheet.checkBoxes().queryInterface(CheckBoxes.class);
42+
CheckBox checkbox = checkboxes.item(app.getCaller()).queryInterface(CheckBox.class);
43+
44+
// Find the named range for this checkbox
45+
String name = checkbox.getName();
46+
Range range = sheet.getRange(name + "_OUTPUT");
47+
48+
// Set the cell value based on the checkbox state
49+
Object value = checkbox.getValue();
50+
if (value instanceof Double && (Double)value != 0.0) {
51+
range.setValue("Checked!");
52+
} else {
53+
range.setValue("Click the checkbox");
54+
}
55+
}
56+
57+
/**
58+
* Example macro function to be bound to a scrollbar.
59+
*
60+
* It sets the value of a named range with the name of the
61+
* scrollbar + "_OUTPUT" to the current scrollbar value.
62+
*/
63+
@ExcelMacro("jinx.scrollbar_example")
64+
public void scrollbarExample() {
65+
_Application app = JinxBridge.getApplication(xl);
66+
67+
// Get the scrollbar that called this macro
68+
_Worksheet sheet = app.getActiveSheet().queryInterface(_Worksheet.class);
69+
ScrollBars scrollbars = sheet.scrollBars().queryInterface(ScrollBars.class);
70+
ScrollBar scrollbar = scrollbars.item(app.getCaller()).queryInterface(ScrollBar.class);
71+
72+
// Find the named range for this scrollbar
73+
String name = scrollbar.getName();
74+
Range range = sheet.getRange(name + "_OUTPUT");
75+
76+
// Set the cell value from the scrollbar value
77+
range.setValue(scrollbar.getValue());
78+
}
79+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.exceljava.com4j.examples;
2+
3+
import com.exceljava.jinx.ExcelAddIn;
4+
import com.exceljava.jinx.ExcelMenu;
5+
6+
import com.exceljava.com4j.JinxBridge;
7+
import com.exceljava.com4j.excel.*;
8+
import com4j.Com4jObject;
9+
10+
/**
11+
* Example menu functions that use com4j to call back into Excel
12+
* when triggered from an Excel menu.
13+
*/
14+
public class MenuFunctions {
15+
private final ExcelAddIn xl;
16+
17+
/**
18+
* Called by Jinx when binding the instance methods to Excel menus.
19+
*
20+
* Using a non-default constructor taking an ExcelAddIn is necessary
21+
* so that we can get hold of the COM Excel Application wrapper later.
22+
*
23+
* @param xl ExcelAddIn instance provided by Jinx.
24+
*/
25+
public MenuFunctions(ExcelAddIn xl) {
26+
this.xl = xl;
27+
}
28+
29+
/**
30+
* Example menu function that takes the current selection and sets
31+
* the background colors randomly.
32+
*
33+
* This will appear as a menu item under 'Jinx' in the Add-Ins tab
34+
* in Excel.
35+
*/
36+
@ExcelMenu(
37+
value = "Randomise Colors",
38+
subMenu = "Com4J Examples"
39+
)
40+
public void randomiseColors() {
41+
_Application app = JinxBridge.getApplication(xl);
42+
43+
Range active = app.getSelection().queryInterface(Range.class);
44+
for (int row = 1; row <= active.getRows().getCount(); row++) {
45+
for (int col = 1; col <= active.getColumns().getCount(); col++) {
46+
Range cell = ((Com4jObject)active.getItem(row, col)).queryInterface(Range.class);
47+
48+
int red = (int)(Math.random() * 255);
49+
int green = (int)(Math.random() * 255);
50+
int blue = (int)(Math.random() * 255);
51+
52+
int color = red | (green << 8) | (blue << 16);
53+
cell.getInterior().setColor(color);
54+
}
55+
}
56+
}
57+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# Classes to be loaded by the Jinx Add-In
3+
#
4+
# This is an alternative to listing all the classes in the jinx.ini
5+
# config file. Instead, as long as the jar is on the class path
6+
# Jinx will read this resource and load the classes listed below.
7+
#
8+
com.exceljava.com4j.examples.MacroFunctions
9+
com.exceljava.com4j.examples.MenuFunctions

jinx-com4j/pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>com.exceljava</groupId>
7+
<artifactId>jinx-com4j-root</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>..</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
<artifactId>jinx-com4j</artifactId>
13+
<packaging>jar</packaging>
14+
15+
<!-- ==================================================================== -->
16+
<repositories>
17+
<repository>
18+
<id>jitpack.io</id>
19+
<url>https://jitpack.io</url>
20+
</repository>
21+
</repositories>
22+
23+
<!-- ==================================================================== -->
24+
<dependencies>
25+
<!-- Jinx -->
26+
<dependency>
27+
<groupId>com.exceljava</groupId>
28+
<artifactId>jinx</artifactId>
29+
<version>[1.0.0,)</version>
30+
</dependency>
31+
<!-- com4j -->
32+
<dependency>
33+
<groupId>com.github.exceljava.com4j</groupId>
34+
<artifactId>com4j</artifactId>
35+
<version>release-20180530</version>
36+
</dependency>
37+
</dependencies>
38+
39+
<!-- ==================================================================== -->
40+
<build>
41+
<resources>
42+
<resource>
43+
<directory>src/main/resources</directory>
44+
</resource>
45+
</resources>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<configuration>
51+
<source>1.8</source>
52+
<target>1.8</target>
53+
</configuration>
54+
</plugin>
55+
<plugin>
56+
<artifactId>maven-dependency-plugin</artifactId>
57+
<executions>
58+
<execution>
59+
<phase>package</phase>
60+
<goals>
61+
<goal>copy-dependencies</goal>
62+
</goals>
63+
<configuration>
64+
<outputDirectory>${project.build.directory}</outputDirectory>
65+
</configuration>
66+
</execution>
67+
</executions>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.exceljava.com4j;
2+
3+
import com.exceljava.jinx.ExcelAddIn;
4+
import com.exceljava.com4j.excel._Application;
5+
import com4j.COM4J;
6+
import com4j.Com4jObject;
7+
import com4j.ComThread;
8+
import com4j.ROT;
9+
10+
/**
11+
* Bridge between the Jinx add-in and com4j.
12+
* Used for obtaining com4j COM wrappers from code running in Excel using Jinx.
13+
*/
14+
public class JinxBridge {
15+
16+
private static final ThreadLocal<_Application> xlApp = new ThreadLocal<_Application>() {
17+
public _Application initialValue() {
18+
return null;
19+
}
20+
};
21+
22+
/**
23+
* Gets the Excel Application object for the current Excel process.
24+
*
25+
* This can then be used to call back into Excel using the Excel
26+
* automation API, in the same way as VBA can be used to automate
27+
* Excel.
28+
*
29+
* The _Application object and objects obtained from it should only
30+
* be used from the same thread as it was obtained on. Sharing it
31+
* between threads will cause issues, and may cause Excel to crash.
32+
*
33+
* @param xl The ExcelAddIn object obtained from Jinx.
34+
* @return An Excel Application instance.
35+
*/
36+
public static _Application getApplication(ExcelAddIn xl) {
37+
Com4jObject unk = COM4J.wrapSta(Com4jObject.class, xl.getExcelApplication());
38+
return unk.queryInterface(_Application.class);
39+
}
40+
}

0 commit comments

Comments
 (0)