diff --git a/fastods/src/main/java/com/github/jferard/fastods/AnonymousOdsDocument.java b/fastods/src/main/java/com/github/jferard/fastods/AnonymousOdsDocument.java index ea137223..02b6198e 100644 --- a/fastods/src/main/java/com/github/jferard/fastods/AnonymousOdsDocument.java +++ b/fastods/src/main/java/com/github/jferard/fastods/AnonymousOdsDocument.java @@ -144,6 +144,11 @@ public void addAutofilter(final Table table, final int r1, final int c1, final i this.commonOdsDocument.addAutofilter(table, r1, c1, r2, c2); } + @Override + public void freezeCells(final Table table, final int rowCount, final int colCount) { + this.commonOdsDocument.freezeCells(table, rowCount, colCount); + } + /** * Saves a file. * Do not close the writer (see https://github.com/jferard/fastods/issues/138) diff --git a/fastods/src/main/java/com/github/jferard/fastods/CommonOdsDocument.java b/fastods/src/main/java/com/github/jferard/fastods/CommonOdsDocument.java index 6243ad2b..36e2c2e5 100644 --- a/fastods/src/main/java/com/github/jferard/fastods/CommonOdsDocument.java +++ b/fastods/src/main/java/com/github/jferard/fastods/CommonOdsDocument.java @@ -159,4 +159,9 @@ public void addAutofilter(final Table table, final int r1, final int c1, final i final int c2) { this.odsElements.addAutofilter(table, r1, c1, r2, c2); } + + @Override + public void freezeCells(final Table table, final int rowCount, final int colCount) { + this.odsElements.freezeCells(table, rowCount, colCount); + } } diff --git a/fastods/src/main/java/com/github/jferard/fastods/NamedOdsDocument.java b/fastods/src/main/java/com/github/jferard/fastods/NamedOdsDocument.java index 2d526dea..de02fe14 100644 --- a/fastods/src/main/java/com/github/jferard/fastods/NamedOdsDocument.java +++ b/fastods/src/main/java/com/github/jferard/fastods/NamedOdsDocument.java @@ -144,6 +144,11 @@ public void addAutofilter(final Table table, final int r1, final int c1, final i this.commonOdsDocument.addAutofilter(table, r1, c1, r2, c2); } + @Override + public void freezeCells(final Table table, final int rowCount, final int colCount) { + this.commonOdsDocument.freezeCells(table, rowCount, colCount); + } + /** * Add a cell style for a given data type. Use only if you want to flush data before the end * of the document diff --git a/fastods/src/main/java/com/github/jferard/fastods/OdsDocument.java b/fastods/src/main/java/com/github/jferard/fastods/OdsDocument.java index 4f5d5b43..955be6ab 100644 --- a/fastods/src/main/java/com/github/jferard/fastods/OdsDocument.java +++ b/fastods/src/main/java/com/github/jferard/fastods/OdsDocument.java @@ -138,4 +138,6 @@ public interface OdsDocument { * @param c2 the right column */ void addAutofilter(Table table, int r1, int c1, int r2, int c2); + + void freezeCells(Table table, int rowCount, int colCount); } diff --git a/fastods/src/main/java/com/github/jferard/fastods/TableBuilder.java b/fastods/src/main/java/com/github/jferard/fastods/TableBuilder.java index 0de3df84..b6daf337 100644 --- a/fastods/src/main/java/com/github/jferard/fastods/TableBuilder.java +++ b/fastods/src/main/java/com/github/jferard/fastods/TableBuilder.java @@ -24,6 +24,7 @@ package com.github.jferard.fastods; import com.github.jferard.fastods.datastyle.DataStyles; +import com.github.jferard.fastods.odselement.OdsElements; import com.github.jferard.fastods.odselement.StylesContainer; import com.github.jferard.fastods.odselement.config.ConfigItem; import com.github.jferard.fastods.odselement.config.ConfigItemMapEntry; @@ -98,8 +99,8 @@ public static TableBuilder create(final PositionUtil positionUtil, final WriteUt final ConfigItemMapEntrySet configEntry = ConfigItemMapEntrySet.createSet(name); configEntry.add(new ConfigItem("CursorPositionX", "int", "0")); configEntry.add(new ConfigItem("CursorPositionY", "int", "0")); - configEntry.add(new ConfigItem("HorizontalSplitMode", "short", "0")); - configEntry.add(new ConfigItem("VerticalSplitMode", "short", "0")); + configEntry.add(new ConfigItem("HorizontalSplitMode", "short", OdsElements.SC_SPLIT_NORMAL)); + configEntry.add(new ConfigItem("VerticalSplitMode", "short", OdsElements.SC_SPLIT_NORMAL)); configEntry.add(new ConfigItem("HorizontalSplitPosition", "int", "0")); configEntry.add(new ConfigItem("VerticalSplitPosition", "int", "0")); configEntry.add(new ConfigItem("ActiveSplitRange", "short", "2")); diff --git a/fastods/src/main/java/com/github/jferard/fastods/odselement/OdsElements.java b/fastods/src/main/java/com/github/jferard/fastods/odselement/OdsElements.java index c6db78dc..172e3761 100644 --- a/fastods/src/main/java/com/github/jferard/fastods/odselement/OdsElements.java +++ b/fastods/src/main/java/com/github/jferard/fastods/odselement/OdsElements.java @@ -31,6 +31,8 @@ import com.github.jferard.fastods.TableCell; import com.github.jferard.fastods.datastyle.DataStyle; import com.github.jferard.fastods.datastyle.DataStyles; +import com.github.jferard.fastods.odselement.config.ConfigItem; +import com.github.jferard.fastods.odselement.config.ConfigItemMapEntry; import com.github.jferard.fastods.style.MasterPageStyle; import com.github.jferard.fastods.style.ObjectStyle; import com.github.jferard.fastods.style.PageLayoutStyle; @@ -59,6 +61,10 @@ public class OdsElements { "Configurations2/popupmenu/", "Configurations2/progressbar/", "Configurations2/statusbar/", "Configurations2/toolbar/"}; + // LO only + public static final String SC_SPLIT_NORMAL = "0"; + public static final String SC_SPLIT_FIX = "2"; + /** * @param positionUtil an util for cell addresses (e.g. "A1") * @param xmlUtil an XML util @@ -225,6 +231,21 @@ public Table addTableToContent(final String name, final int rowCapacity, return table; } + /** + * Freeze cells. See https://help.libreoffice.org/Calc/Freezing_Rows_or_Columns_as_Headers + * + * @param table the table to freeze + * @param rowCount the number of rows to freeze (e.g. 1 -> freeze the first row) + * @param colCount the number of cols to freeze. + */ + public void freezeCells(final Table table, final int rowCount, final int colCount) { + final ConfigItemMapEntry tableConfig = table.getConfigEntry(); + tableConfig.put(new ConfigItem("HorizontalSplitMode", "short", SC_SPLIT_FIX)); + tableConfig.put(new ConfigItem("VerticalSplitMode", "short", SC_SPLIT_FIX)); + tableConfig.put(new ConfigItem("HorizontalSplitPosition", "int", String.valueOf(rowCount))); + tableConfig.put(new ConfigItem("VerticalSplitPosition", "int", String.valueOf(colCount))); + } + /** * Create empty elements for package. Used on save or by the ImmutableElementsFlusher. * diff --git a/fastods/src/main/java/com/github/jferard/fastods/odselement/SettingsElement.java b/fastods/src/main/java/com/github/jferard/fastods/odselement/SettingsElement.java index 6160e842..e3bd9cb2 100644 --- a/fastods/src/main/java/com/github/jferard/fastods/odselement/SettingsElement.java +++ b/fastods/src/main/java/com/github/jferard/fastods/odselement/SettingsElement.java @@ -84,7 +84,7 @@ static SettingsElement create() { } /** - * Add a table config + * Add a table config. * @param configEntry the config */ public void addTableConfig(final ConfigItemMapEntry configEntry) { diff --git a/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntry.java b/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntry.java index 0ee57363..6b3f0576 100644 --- a/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntry.java +++ b/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntry.java @@ -38,6 +38,14 @@ public interface ConfigItemMapEntry extends ConfigItemCollection { */ boolean add(ConfigBlock block); + /** + * Put a block in this entry. Replace existing block with the same name. + * @param block the block to add to this entry. May throw + * @return the previous block or null. + * @throws UnsupportedOperationException if the entry is a singleton or a list + */ + ConfigBlock put(ConfigBlock block); + /** * Add an ConfigItem to this entry. Shortcut for {@code add(new ConfigItem(name, type, value))}. * @param name the name of the item diff --git a/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntrySequence.java b/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntrySequence.java index ee9f9ee0..7d6d471c 100644 --- a/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntrySequence.java +++ b/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntrySequence.java @@ -84,6 +84,11 @@ public boolean add(final ConfigBlock block) { return this.blocks.add(block); } + @Override + public ConfigBlock put(final ConfigBlock block) { + throw new UnsupportedOperationException(); + } + /** * Remove the block at a given index. * @param i the index. diff --git a/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntrySet.java b/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntrySet.java index 1108d236..205bb04a 100644 --- a/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntrySet.java +++ b/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntrySet.java @@ -108,6 +108,12 @@ public boolean add(final ConfigBlock block) { return true; } + @Override + public ConfigBlock put(final ConfigBlock block) { + final String name = block.getName(); + return this.blockByName.put(name, block); + } + /** * @param o the block to remove */ diff --git a/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntrySingleton.java b/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntrySingleton.java index ffb8edd3..b33a8194 100644 --- a/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntrySingleton.java +++ b/fastods/src/main/java/com/github/jferard/fastods/odselement/config/ConfigItemMapEntrySingleton.java @@ -86,6 +86,11 @@ public boolean add(final ConfigBlock block) { throw new UnsupportedOperationException(); } + @Override + public ConfigBlock put(final ConfigBlock block) { + throw new UnsupportedOperationException(); + } + /** * @param name the name * @return true if the unique ConfigBlock has that name