Is there a way to programmatically check (with java) if a calc document has macros (starting point XSpreadsheetDocument) ? And if there are protected macros among them ?
Thanks in advance !
(I use Open Office 3.3 on Windows XP SP3 and Java 1.6.0_26.)
[Solved] Check if a Calc document has macros in Java
[Solved] Check if a Calc document has macros in Java
Last edited by Hagar Delest on Thu Sep 08, 2011 5:28 pm, edited 1 time in total.
Reason: tagged [Solved].
Reason: tagged [Solved].
OpenOffice 3.3 on Windows XP SP3
Re: Check if a calc document has macros in Java
Possible solutions:
Code: Select all
public boolean hasMacro(XSpreadsheetDocument xSpreadsheetDocument) {
boolean ret = false;
XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xSpreadsheetDocument);
XLibraryContainer xLibraryContainer;
try {
xLibraryContainer = (XLibraryContainer)
UnoRuntime.queryInterface(XLibraryContainer.class, xPropertySet.getPropertyValue("BasicLibraries"));
if (xLibraryContainer.getElementNames().length > 1) {
ret = true;
} else if (xLibraryContainer.getElementNames().length == 1) { // Standard library
Object standardLibrary;
try {
standardLibrary = xLibraryContainer.getByName(xLibraryContainer.getElementNames()[0]);
XNameAccess xNameAccess = (XNameAccess)
UnoRuntime.queryInterface(XNameAccess.class, standardLibrary);
ret = xNameAccess.hasElements();
} catch (NoSuchElementException e) {
...
} catch (WrappedTargetException e) {
...
}
}
} catch (UnknownPropertyException e1) {
...
} catch (WrappedTargetException e1) {
...
}
return ret;
}
Code: Select all
public boolean hasProtectedMacro(XSpreadsheetDocument xSpreadsheetDocument) {
boolean ret = false;
XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xSpreadsheetDocument);
XLibraryContainer xLibraryContainer = null;
try {
xLibraryContainer = (XLibraryContainer)
UnoRuntime.queryInterface(XLibraryContainer.class, xPropertySet.getPropertyValue("BasicLibraries"));
} catch (UnknownPropertyException e) {
...
return false;
} catch (WrappedTargetException e) {
...
return false;
}
XLibraryContainerPassword xLibraryContainerPassword =
(XLibraryContainerPassword) UnoRuntime.queryInterface(XLibraryContainerPassword.class, xLibraryContainer);
if (xLibraryContainer != null) {
int noOfLibraries = xLibraryContainer.getElementNames().length;
int i = 0;
while (!ret && i < noOfLibraries) {
String libraryName = xLibraryContainer.getElementNames()[i];
try {
ret = xLibraryContainerPassword.isLibraryPasswordProtected(libraryName);
} catch (NoSuchElementException e) {
...
}
i++;
}
}
return ret;
}
OpenOffice 3.3 on Windows XP SP3