Ask Your Question
1

Macro for saving sheet as single image in Calc

asked 2017-12-30 13:39:32 +0200

tagussan gravatar image

Hi!

I have an ods file, which has only one sheet. I'd like to generate PNG file of the sheet. However, if I use "File -> Export...", I got PNG files which is splitted into page. What I need is a PNG file as single image, not the splitted one.

Then I came up with an idea. First, I select the necessary area, copy, and paste it to Writer, like "(Right click on page) -> Paste spetial -> More options -> Bitmap". Then I right-clicked the pasted bitmap object and did "Save" in the context menu. With this method I was succeeded in generating the single PNG image manually, so I tried to automate this procedure, using Macro.

I used "Record Macro", and found that saving Bitmap object corresponds to ".uno:SaveGraphic"; however, whatever kind of args did I passed to uno:SaveGraphic, a save dialogue window appears.

Are there any way to dispatch ".uno:SaveGraphic" without creating save dialogue? Or, are there any other simple way to get single PNG image?

Thank you

edit retag flag offensive close merge delete

Comments

I made another follow-up question. For further discussion, please use this.

I'm sorry for inconvinience.

tagussan gravatar imagetagussan ( 2017-12-31 07:41:21 +0200 )edit

Not inconvenient at all. It is often better to ask multiple smaller questions rather than one large question.

Jim K gravatar imageJim K ( 2017-12-31 08:18:47 +0200 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2017-12-30 19:34:54 +0200

Jim K gravatar image

updated 2017-12-30 20:32:04 +0200

Your approach seems to be the only good way to do it. However, use Draw instead of Writer. The following code does not require user interaction or pop up any dialogs.

Sub SaveSheetAsBitmap
    'Copy from Calc.'
    oSpreadsheet = ThisComponent
    oSpreadsheetController = ThisComponent.getCurrentController()
    oSheet = oSpreadsheetController.getActiveSheet()
    'oRange = oSheet.getCellRangeByname("A1:G100")'
    'oSpreadsheetController.select(oRange)'
    oSpreadsheetController.select(oSheet)
    oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    oSpreadsheetFrame = oSpreadsheetController.getFrame()
    oDispatcher.executeDispatch(oSpreadsheetFrame, ".uno:Copy", "", 0, Array())

    'Paste bitmap into Draw.'
    oDoc = StarDesktop.loadComponentFromUrl(_
        "private:factory/sdraw", "_blank", 0, Array())
    oDocController = oDoc.getCurrentController()
    oDocFrame = oDocController().getFrame()
    Dim props(0) As New com.sun.star.beans.PropertyValue
    props(0).Name = "SelectedFormat"
    props(0).Value = 2  'Apparently this means bitmap format'
    oDispatcher.executeDispatch(oDocFrame, ".uno:ClipboardFormatItems", "", 0, props())

    'Export to PNG file.'
    Dim aExportProps(1) as new com.sun.star.beans.PropertyValue
    aExportProps(0).Name = "URL"
    aExportProps(0).Value = "file:///path/to/test.png"
    aExportProps(1).Name = "MimeType"
    aExportProps(1).Value = "image/png"
    oExporter = createUnoService("com.sun.star.drawing.GraphicExportFilter")
    oExporter.SetSourceDocument(oDocController.Selection)
    oExporter.Filter(aExportProps)
End Sub
edit flag offensive delete link more

Comments

Thank you for the answer! I confirmed that this macro does what I wanted to. However, if I run this macro from shell, like "soffice --headless --norestore --nofirststartwizard --invisible "macro:///Standard.Module1.SaveSheetAsBitmap()" test.ods", the soffice command does not return.

I thought that I should close the documents, so I added "oDoc.close(True)" and "oSpreadsheet.close(True)" at the end of "SaveSheetAsBitmap". But the situation does not change.

What else do I need?

tagussan gravatar imagetagussan ( 2017-12-31 06:10:01 +0200 )edit

Running headless is a different story. To ask about that, post a new follow-up question with a link to this one.

Jim K gravatar imageJim K ( 2017-12-31 06:34:07 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-12-30 13:39:32 +0200

Seen: 192 times

Last updated: Dec 30 '17