[Solved] Check if a Calc document has macros in Java

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
bcris
Posts: 21
Joined: Wed Aug 17, 2011 3:51 pm

[Solved] Check if a Calc document has macros in Java

Post by bcris »

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.)
Last edited by Hagar Delest on Thu Sep 08, 2011 5:28 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 3.3 on Windows XP SP3
bcris
Posts: 21
Joined: Wed Aug 17, 2011 3:51 pm

Re: Check if a calc document has macros in Java

Post by bcris »

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
Post Reply