Tips - Python Macros: Sample Codes

Several examples of Macro codes in python for calc (just launch the web translator and enjoy):
Part1:
Macroの杜(Python編 / Calc)

Content:

Cell operation

[Basic]

CCB-) [Calc] Enter a value in Cell 1 (character string)
CCB-) [Calc] Enter a value in Cell 2 (number / formula)
CCB-) [Calc] Enter a value in Cell 3 (number / formula / String) [A1 format address]
CCB-) [Calc] Get value from Cell

[Insert ・ Delete.Copy]

CCI-) [Calc] Add Cell (1) [Move existing data downward] [uno.createUnoStruct / insertCells / com.sun.star.sheet.CellInsertMode.DOWN]
CCI-) [Calc] Add Cell ( CCI-) [Calc] Add Cell ( CCI-) [Calc] Add Cell 2) [Existing data moves to the right] [com.sun.star.sheet.CellInsertMode.RIGHT]
CCI-) [Calc] Add Cell (3) [Moves the entire row downward] [com.sun. star.sheet.CellInsertMode.ROWS]
CCI-) [Calc] Add Cell (4) [Move entire column to the right] [com.sun.star.sheet.CellInsertMode.COLUMNS]
CCI-) [Calc] Cell Add (5) [Move right / bottom / entire row / column] [ [.uno: InsertCell]
CCI-) [Calc] Delete Cell (1) [Move existing data upward] [removeRange / com. sun.star.sheet.CellDeleteMode.UP]
CCI-) [Calc] Delete Cell (2) [Move existing data to the left] [com.sun.star.sheet.CellDeleteMode.LEFT]
CCI-) [Calc] Delete Cell (3) [Whole row upward Move] [com.sun.star.sheet.CellDeleteMode.ROWS]
CCI-) [Calc] Delete Cell (4) [Move entire column to the left] [com.sun.star.sheet.CellDeleteMode.COLUMNS]
CCI -) [Calc] Delete Cell (move left / top / entire row / entire column) [.uno: DeleteCell]
CCI-) [Calc] Cell COPY [copyRange]
CCI-) [Calc] Cell COPY2 [getData ] () / setData]
CCI-) [Calc] COPY3 in Cell range [getDataArray / setDataArray]
CCI-) [Calc] Select and paste format[.uno: InsertContents]
CCI-) [Calc] Copy to another Document [.getComponents / os.path.basename (.Location ()) / .createEnumeration () / .hasMoreElements () / .nextElement () / .hasLocation ()]

[Property (Formatting Cell)]

CCProp-) [Calc] Unformat directly set [.uno: ResetAttributes]

[Format]

CCPF-) [Calc] Get Cell Display Format Key No. [NumberFormats / queryKey]
CCPF-) [Calc] Set Cell Numerical Display Format 1 [NumberFormats / queryKey]
CCPF-) [Calc] Cell Numerical Display Format Setting 2 [NumberFormat]
CCPF-) [Calc] Cell Set the display format of numbers 3 [.uno: NumberFormatValue]
CCPF-) [Calc] Content Type of Cell [com.sun.star.CellContentType]
CCPF-) [Calc] User Defined Attributes [com.sun.star.table.CellProperties Service]
CCPF-) [Calc] Unique Format Range [getCellFormatRanges () / getRangeAddress () / getRangeAddressesAsString ()]

[Font]

CCPFt-) [Calc] Line breaks according to Cell width (1) [.IsTextWrapped]
CCPFt-) [Calc] Line breaks according to Cell width (2) [.uno: WrapText]
CCPFt-) [Calc] Character-related Property List [from com.sun.star.awt.FontSlant import (NONE, ITALIC, REVERSE_ITALIC, OBLIQUE,) / .CharFontName / .CharFontNameAsian / .CharPosture / CharPostureAsian / .CharPostureAsian / .CharHeight / .CharHeightAsian / .createTextCursor () /. setPropertyValue ('CharContoured',) / .setPropertyValue ('CharCrossedOut,) / .setPropertyValue ('CharEmphasis',) / .setPropertyValue ('CharUnderlineColor',) / .setPropertyValue ('CharUnderline',) / .setPropertyValue ('CharRelief',) ) / .SetPropertyValue ('CharShadowed',
)] CCPFt-) [Calc]Make the right character of the character string a subscript character [CharEscapement / CharEscapementHeight]
CCPFt-) [Calc] Cell background color setting (1) [.CellBackColor]
CCPFt-) [Calc] Cell background color setting (2) [ .uno: BackgroundPattern / BackgroundPattern.BackColor]
CCPFt-) [Calc] Character Angle (1) [.RotateAngle]
CCPFt-) [Calc] Character Color / Character Size / Character Weight Setting [CharColor / CharHight / CharWeight]
CCPFt- ) [Calc] Vertical writing [.uno: TextdirectionTopToBottom]

[Font Effet] [Refer to “Font / Character-related Property List”]

CCFEt-) [Calc] Various UnderLine settings [.CharUnderline / from com.sun.star.awt.FontUnderline import (NONE, SINGLE, DOUBLE, DOTTED, DONTKNOW, DASH, LONGDASH, DASHDOT, DASHDOTDOT, SMALLWAVE, WAVE, DOUBLEWAVE, BOLD , BOLDDOTTED, BOLDDASH, BOLDLONGDASH, BOLDDASHDOT, BOLDDASHDOTDOT, BOLDWAVE,)]
CCFEt-) [Calc] Underline color / Color of Underline [.CharUnderlineColor / .CharUnderlineHasColor]
CCFEt-) [Calc] Shadowed characters (1) [.CharShadowed]
CCFEt-) [Calc] Shadowed characters (2) [.uno: Shadowed]
CCFEt-) [Calc] Various strikethroughs (1) [.CharHeight / .CharHeightAsian / .CharStrikeout]
CCFEt-) [Calc]Various
strikeouts (2) [.uno: Strikeout / Strikeout.Kind] CCFEt-) [Calc] Various OverLine [.setPropertyValue ('CharEmphasis',)]
CCFEt-) [Calc] Embossed / embossed characters (1) [.setPropertyValue ( 'CharRelief',)]
CCFEt-) [Calc] embossed / embossed characters (2) [.Uno: CharacterRelief]
CCFEt-) [Calc] hollowed character (1) [.CharContoured]
CCFEt-) [Calc] hollowed Characters (2) [.uno: OutlineFont]
CCFEt-) [Calc] []

[Position / Size]

CCPoSz-) [Calc] Position settings in Cell (1) [Vertical] [.VertJustify / from com.sun.star.table.CellVertJustify import (STANDARD, TOP, CENTER, BOTTOM)]
CCPoSz-) [Calc] Cell Position settings in (2) [Horizontal] [.HoriJustify / from com.sun.star.table.CellHoriJustify import (STANDARD, LEFT, CENTER, RIGHT, BLOCK, REPEAT)]
CCPoSz-) [Calc] Position in Cell Settings (3) [ Vertical / Horizontal] [.uno: VerticalJustification / .uno: HorizontalJustification]
CCPoSz-) [Calc] []

[Color]

CCCo-) [Calc] Character Color ⇔ Character color [.CharColor]
CCCo-) [Calc ] Make only the amount part (right part) of the character string red ⇔ Color of current symbol [createTextCursor () / setPropertyValue (,) / CharColor]

[Clear]

CCClr-) [Calc] Claer contents (1) [from com.sun.star.sheet.CellFlags import (VALUE, DATETIME, STRING, ANNOTATION, FORMULA, HARDATTR, STYLES, OBJECTS, EDITATTR, FORMATTED,) / .clearContents]
CCClr -) [Calc] Column Selection (1) ⇔ Select Columns (1) [.select / getColumns ()]

[Selection]

CCClr-) [Calc] Cell Selection (1) ⇔ Select Cells (1) [.select]
CCClr-) [Calc] Column Selection (1) ⇔ Select Columns (1) [.select / getColumns ()]

[Address]

CCAd-) [Calc] Get Current Cell Address (1) [com.sun.star.sheet.SheetCellRange / .getRangeAddress ()]
CCAd-) [Calc] Get Last Row No. (1)
CCAd-) [Calc ] Get last line number (2) [.uno: GoUpToStartOfData]

Part 2 - Content:
Macroの杜(Python編 / Calc)

Cell operation (2)

[Column・Row(row・column)]

CCR-) Insert [Calc] Row (1) []
CCR-) Insert [Calc] Column (1) []
CCR-) Delete [Calc] Row (1) []
CCR-) In [Calc] Column Delete (1) []
CCR-) [Calc] Get Row Height & Column Width []
CCR-) [Calc] Set Row Height & Column Width (1) []
CCR-) [Calc] Get Row Height & Column Width & Optimize column width (1) []
CCR-) [Calc] Show / hide rows / columns (1) []
CCR-) [Calc] Show / hide multiple rows / columns (1) []

[ Hyper Link ]

CHy-) [Calc]Hyper Link[ com.sun.star.text.textfield.URL / .URL / .Representation / .TargetFrame/ .insertTextContent() ]

[ Array ]

CCD-) [Calc] Get all the data in the specified range [.getDataArray ()]
CCD-) [Calc] []

[Sheet]

CS-) [Calc] Get Sheet Name
CS-) [Calc] Change Sheet Name
CS-) [Calc] Get all Sheet names
CS-) [Calc] Check if there is a Sheet name

[File]

CF-) [Calc] New Calc file to start
CF-) [Calc] Specify Calc file to start
CF-) [Calc] Specify Calc file to save (save in 上书き)

[Graph Chart creation]

CGhCt-) [Calc]各種Graph作成(0)[ .getCharts() / .hasByName() / .RemoveByName() / addNewByName(,,,,) / .embeddedObject ]
CGhCt-) [Calc]各種Graph作成(3)[ .getFirstDiagram() / .getChartTypeManager() / com.sun.star.chart2.template.ScatterLineSymbol / .changeDiagram() ]
CGhCt-) [Calc]Chart AreaのProperties[ .getArea() / .FillStyle / .FillBackground / .FillColor / .FillTransparence / .LineStyle / .LineWidth / .LineColor / .LineTransparence / .getDiagram() / getWall() ]
CGhCt-) [Calc]Data SeriesのProperties[ .getFirstDiagram() / .getCoordinateSystems() / .getChartTypes() / .getDataSeries() / .Color ]
CGhCt-) [Calc][ ]

[Printing operation]

CP-) [Calc] Insert / Release Page Break (1) [IsStartOfNewPage]
CP-) [Calc] Insert / Cancel Page Break (2) [IsStartOfNewPage]
CP-) [Calc] Cancel All Page Breaks []
CP -) [Calc] Set / Get print range [setPrintAreas () / getPrintAreas () / hasUnoInterfaces ()]
CP-) [Calc] Define / change / add / delete print range [.uno: DefinePrintArea / .uno: ChangePrintArea / .uno: AddPrintArea / .uno: DeletePrintArea]
CP-) [Calc] Get PageStyle (margin, header, footer) []
CP-) [Calc] [] 

Home page in Japanese:

https://openoffice3.web.fc2.com/index.html

Tip: Try not to use the DispatchHelper service if there are direct UNO methods. In your examples, in most cases the DispatchHelper is not needed.
In these situations, Macro Recorder, alas, is not an assistant.
By the way, there is a great site in Japanese about LO.

2 Likes

Yes. There are many examples of working with dialog windows.
And oddly enough, for a long time I don’t care what language the site is in. :slightly_smiling_face:

Luckily we have a multilingualist named Google (, …) Translate. :slightly_smiling_face:

btw. its Hanyas Site, the Author of MRI, and initial contributor to APSO !!

Disclaimer: I’m not engaged in any way to that Company but I want to remark this excellent Tanslationservice

Thank you, I will definitely take a look. Probably, the quality still depends on the language into which (from which) the translation is made.

Didnt test with Japanese, (because I can not judge the result in any way) but the Translation round some European-languages works well.

After some time (a year?) I will report on the Russian language (If someone does not do it earlier). :slightly_smiling_face:

MRI is really a brilliant program! Among other things, I was struck by the fact that the author meticulously reproduced the algorithm for generating URIs for links to the description of properties and methods of UNO objects in the SDK API Reference.

throw russian on it and see the result in english.

I did it the other direction:
переведите на русский язык и посмотрите результат на английском.

Deepl:

If you know what I mean, it works the other way around, too.

Google:

If you understand what I am writing about, then the translation also works in the opposite direction.

Original:

Если Вы понимаете, о чем я пишу, то в обратном направлении перевод тоже работает.

My (non-professional) opinion: Google won this round.

En: This issue counts. Ru: Вопрос имеет значение.
Translate back and forth and you’ll never get the original version. In fact, I check the translator. Sometimes it distorts the meaning, but in general it works acceptably.

E.g.
En: Google drops articles more often than Yandex. //drops or omits: ‘a’, ‘an’, ‘the’
Ru: Google чаще опускает артикли, чем Yandex.
And it doesn’t translate…